Skip to content

Commit

Permalink
update 2017/01/31
Browse files Browse the repository at this point in the history
  • Loading branch information
asamaru7 committed Jan 31, 2017
1 parent 026b9b0 commit 2de105b
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 184 deletions.
4 changes: 2 additions & 2 deletions LICENSE
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013, orangehill
Copyright (c) 2013, Orange Hill Development
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -20,4 +20,4 @@ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@

[![Build Status](https://travis-ci.org/orangehill/iseed.png)](http://travis-ci.org/orangehill/iseed)
[![Latest Stable Version](https://poser.pugx.org/orangehill/iseed/v/stable.png)](https://packagist.org/packages/orangehill/iseed) [![Total Downloads](https://poser.pugx.org/orangehill/iseed/downloads.png)](https://packagist.org/packages/orangehill/iseed)
[![Analytics](https://ga-beacon.appspot.com/UA-1936460-35/iseed?useReferrer&flat)](https://github.com/igrigorik/ga-beacon)

## Installation

1) For Laravel 5 instalation edit your project's `composer.json` file to require `orangehill/iseed`.
1) Add `orangehill/iseed` to your composer file.

#### Laravel 5
For Laravel 5 installation edit your project's `composer.json` file to require `orangehill/iseed`.

"require": {
"orangehill/iseed": "dev-master"
}

#### Laravel 5 versions less than 5.3.8
For Laravel 5 versions that are less than 5.3.8 edit your project's `composer.json` file to require `2.2` version:

If you wish to install it on Laravel 4 you should require 1.1 version:
"require": {
"orangehill/iseed": "2.2"
}

#### Laravel 4
If you wish to install it on Laravel 4 you should require `1.1` version:

"require": {
"orangehill/iseed": "1.1"
}

2) Add the service provider by opening a `app/config/app.php` file, and adding a new item to the `providers` array.
2) Update Composer from the CLI:

'Orangehill\Iseed\IseedServiceProvider'
composer update

3) Update Composer from the CLI:
3) Add the service provider by opening a `app/config/app.php` file, and adding a new item to the `providers` array.

composer update
Orangehill\Iseed\IseedServiceProvider::class

## Artisan command options

Expand All @@ -48,6 +60,14 @@ The following command will overwrite `UsersTableSeeder.php` if it already exists
php artisan iseed users --force
```

### dumpauto
Optional boolean parameter that controls the execution of `composer dump-autoload` command. Defaults to true.

Example that will stop `composer dump-autoload` from execution:
```
php artisan iseed users --dumpauto=false
```

### clean
Optional parameter which will clean `app/database/seeds/DatabaseSeeder.php` before creating new seed class.

Expand All @@ -72,6 +92,15 @@ Example:
artisan iseed users --max=10
```

### exclude
Optional parameter which accepts comma separated list of columns that you'd like to exclude from tables that are being exported. In case of multiple tables, exclusion will be applied to all of them.

Example:
```
artisan iseed users --exclude=id
artisan iseed users --exclude=id,created_at,updated_at
```

### prerun
Optional parameter which assigns a laravel event name to be fired before seeding takes place. If an event listener returns `false`, seed will fail automatically.
You can assign multiple preruns for multiple table names by passing an array of comma separated DB names and respectively passing a comma separated array of prerun event names.
Expand Down
13 changes: 10 additions & 3 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
}
],
"require": {
"php": ">=5.4.0"
"php": ">=5.4.0",
"illuminate/support": "^5.3.8"
},
"require-dev": {
"mockery/mockery": "dev-master",
"illuminate/filesystem": "5.x"
"illuminate/filesystem": "5.x",
"laravel/framework": "^5.3.8"
},
"autoload": {
"psr-0": {
Expand All @@ -24,5 +26,10 @@
"src/Orangehill/Iseed/Exceptions.php"
]
},
"autoload-dev": {
"psr-4": {
"Orangehill\\Iseed\\Tests\\": "tests"
}
},
"minimum-stability": "dev"
}
}
3 changes: 2 additions & 1 deletion phpunit.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
Empty file modified src/Orangehill/Iseed/Exceptions.php
100644 → 100755
Empty file.
Empty file modified src/Orangehill/Iseed/Facades/Iseed.php
100644 → 100755
Empty file.
102 changes: 53 additions & 49 deletions src/Orangehill/Iseed/Iseed.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Orangehill\Iseed;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Support\Facades\Config;

class Iseed
Expand All @@ -20,7 +21,7 @@ class Iseed
*
* @var string
*/
private $newLineCharacter = "\r\n";
private $newLineCharacter = PHP_EOL;

/**
* Desired indent for the code.
Expand All @@ -31,9 +32,21 @@ class Iseed
*/
private $indentCharacter = " ";

public function __construct(Filesystem $filesystem = null)
/**
* @var Composer
*/
private $composer;

public function __construct(Filesystem $filesystem = null, Composer $composer = null)
{
$this->files = $filesystem ?: new Filesystem;
$this->composer = $composer ?: new Composer($this->files);
}

public function readStubFile($file)
{
$this->files = $filesystem ? : new Filesystem;
$buffer = file($file, FILE_IGNORE_NEW_LINES);
return implode(PHP_EOL, $buffer);
}

/**
Expand All @@ -46,7 +59,7 @@ public function __construct(Filesystem $filesystem = null)
* @return bool
* @throws Orangehill\Iseed\TableNotFoundException
*/
public function generateSeed($table, $database = null, $max = 0, $prerunEvent = null, $postrunEvent = null)
public function generateSeed($table, $database = null, $max = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true)
{
if (!$database) {
$database = config('database.default');
Expand All @@ -60,7 +73,7 @@ public function generateSeed($table, $database = null, $max = 0, $prerunEvent =
}

// Get the data
$data = $this->getData($table, $max);
$data = $this->getData($table, $max, $exclude);

// Repack the data
$dataArray = $this->repackSeedData($data);
Expand All @@ -69,7 +82,7 @@ public function generateSeed($table, $database = null, $max = 0, $prerunEvent =
$className = $this->generateClassName($table);

// Get template for a seed file contents
$stub = $this->files->get($this->getStubPath().'/seed.stub');
$stub = $this->readStubFile($this->getStubPath() . '/seed.stub');

// Get a seed folder path
$seedPath = $this->getSeedPath();
Expand All @@ -91,6 +104,11 @@ public function generateSeed($table, $database = null, $max = 0, $prerunEvent =
// Save a populated stub
$this->files->put($seedsPath, $seedContent);

// Run composer dump-auto
if ($dumpAuto) {
$this->composer->dumpAutoloads();
}

// Update the DatabaseSeeder.php file
return $this->updateDatabaseSeederRunMethod($className) !== false;
}
Expand All @@ -101,21 +119,28 @@ public function generateSeed($table, $database = null, $max = 0, $prerunEvent =
*/
public function getSeedPath()
{
return base_path().config('iseed::config.path');
return base_path() . config('iseed::config.path');
}

/**
* Get the Data
* @param string $table
* @return Array
*/
public function getData($table, $max)
public function getData($table, $max, $exclude = null)
{
if (!$max) {
return \DB::connection($this->databaseName)->table($table)->get();
$result = \DB::connection($this->databaseName)->table($table);

if (!empty($exclude)) {
$allColumns = \DB::connection($this->databaseName)->getSchemaBuilder()->getColumnListing($table);
$result = $result->select(array_diff($allColumns, $exclude));
}

return \DB::connection($this->databaseName)->table($table)->limit($max)->get();
if ($max) {
$result = $result->limit($max);
}

return $result->get();
}

/**
Expand All @@ -125,8 +150,11 @@ public function getData($table, $max)
*/
public function repackSeedData($data)
{
if (!is_array($data)) {
$data = $data->toArray();
}
$dataArray = array();
if (is_array($data)) {
if (!empty($data)) {
foreach ($data as $row) {
$rowArray = array();
foreach ($row as $columnName => $columnValue) {
Expand All @@ -138,28 +166,6 @@ public function repackSeedData($data)
return $dataArray;
}

/**
* Get all tables names in array
*
* @return array
*/
public function getAllTableName ($databaseName){

$tables = \DB::connection($databaseName)->select('SHOW TABLES');

$databaseName = \Config::get('database.connections.'.$databaseName.'.database');

$tablesFormatted = [];

foreach ($tables as $table){
$field = 'Tables_in_'.$databaseName;
$tablesFormatted [] = $table->$field;
}

return $tablesFormatted;

}

/**
* Checks if a database table exists
* @param string $table
Expand All @@ -178,11 +184,11 @@ public function hasTable($table)
public function generateClassName($table)
{
$tableString = '';
$tableName = explode('_', $table);
$tableName = explode('_', $table);
foreach ($tableName as $tableNameExploded) {
$tableString .= ucfirst($tableNameExploded);
}
return ucfirst($tableString).'TableSeeder';
return ucfirst($tableString) . 'TableSeeder';
}

/**
Expand All @@ -191,7 +197,7 @@ public function generateClassName($table)
*/
public function getStubPath()
{
return __DIR__.DIRECTORY_SEPARATOR.'Stubs';
return __DIR__ . DIRECTORY_SEPARATOR . 'Stubs';
}

/**
Expand All @@ -207,14 +213,14 @@ public function getStubPath()
*/
public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null)
{
$chunkSize = $chunkSize ? : config('iseed::config.chunk_size');
$inserts = '';
$chunks = array_chunk($data, $chunkSize);
$chunkSize = $chunkSize ?: config('iseed::config.chunk_size');
$inserts = '';
$chunks = array_chunk($data, $chunkSize);
foreach ($chunks as $chunk) {
$this->addNewLines($inserts);
$this->addIndent($inserts, 2);
$inserts .= sprintf(
"\DB::connection('".$this->databaseName."')->table('%s')->insert(%s);",
"\DB::table('%s')->insert(%s);",
$table,
$this->prettifyArray($chunk)
);
Expand Down Expand Up @@ -244,8 +250,6 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p
$stub = str_replace('{{table}}', $table, $stub);
}

$stub = str_replace('{{database_name}}', $this->databaseName, $stub);

$postrunEventInsert = '';
if ($postrunEvent) {
$postrunEventInsert .= "\$response = Event::until(new $postrunEvent());";
Expand Down Expand Up @@ -277,7 +281,7 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p
*/
public function getPath($name, $path)
{
return $path.'/'.$name.'.php';
return $path . '/' . $name . '.php';
}

/**
Expand Down Expand Up @@ -364,7 +368,7 @@ private function addIndent(&$content, $numberOfIndents = 1)
*/
public function cleanSection()
{
$databaseSeederPath = base_path().config('iseed::config.path').'/DatabaseSeeder.php';
$databaseSeederPath = base_path() . config('iseed::config.path') . '/DatabaseSeeder.php';

$content = $this->files->get($databaseSeederPath);

Expand All @@ -381,18 +385,18 @@ public function cleanSection()
*/
public function updateDatabaseSeederRunMethod($className)
{
$databaseSeederPath = base_path().config('iseed::config.path').'/DatabaseSeeder.php';
$databaseSeederPath = base_path() . config('iseed::config.path') . '/DatabaseSeeder.php';

$content = $this->files->get($databaseSeederPath);
if (strpos($content, "\$this->call('{$className}')") === false) {
if (strpos($content, "\$this->call({$className}::class)") === false) {
if (
strpos($content, '#iseed_start') &&
strpos($content, '#iseed_end') &&
strpos($content, '#iseed_start') < strpos($content, '#iseed_end')
) {
$content = preg_replace("/(\#iseed_start.+?)(\#iseed_end)/us", "$1\$this->call('{$className}');{$this->newLineCharacter}{$this->indentCharacter}{$this->indentCharacter}$2", $content);
$content = preg_replace("/(\#iseed_start.+?)(\#iseed_end)/us", "$1\$this->call({$className}::class);{$this->newLineCharacter}{$this->indentCharacter}{$this->indentCharacter}$2", $content);
} else {
$content = preg_replace("/(run\(\).+?)}/us", "$1{$this->indentCharacter}\$this->call('{$className}');{$this->newLineCharacter}{$this->indentCharacter}}", $content);
$content = preg_replace("/(run\(\).+?)}/us", "$1{$this->indentCharacter}\$this->call({$className}::class);{$this->newLineCharacter}{$this->indentCharacter}}", $content);
}
}

Expand Down
Loading

0 comments on commit 2de105b

Please sign in to comment.