diff --git a/README.md b/README.md index d7b5cbd..55b0ce2 100755 --- a/README.md +++ b/README.md @@ -214,6 +214,17 @@ php artisan iseed users --where="role = 'admin'" --max=10 --orderby=created_at - **Note**: When using complex WHERE clauses with special characters or spaces, make sure to properly escape and quote the condition string according to your shell's requirements. +### stubsdir +Optional parameter which allows specifying the stubs directory, instead of the default one. +The following stubs will be picked up from that directory: + +- `seed.stub`: Outlines a generated seeder. + +Example: +``` +php artisan iseed users --stubsdir=stubs/iseed +``` + ## Usage To generate a seed file for your users table simply call: `\Iseed::generateSeed('users', 'connectionName', 'numOfRows');`. `connectionName` and `numOfRows` are not required arguments. diff --git a/src/Orangehill/Iseed/Iseed.php b/src/Orangehill/Iseed/Iseed.php index 5b7ae45..684444a 100644 --- a/src/Orangehill/Iseed/Iseed.php +++ b/src/Orangehill/Iseed/Iseed.php @@ -37,8 +37,11 @@ class Iseed */ private $composer; + private $stubsDir; + public function __construct(Filesystem $filesystem = null, Composer $composer = null) { + $this->stubsDir = __DIR__ . DIRECTORY_SEPARATOR . 'stubs'; $this->files = $filesystem ?: new Filesystem; $this->composer = $composer ?: new Composer($this->files); } @@ -210,7 +213,17 @@ public function generateClassName($table, $prefix=null, $suffix=null) */ public function getStubPath() { - return __DIR__ . DIRECTORY_SEPARATOR . 'stubs'; + return $this->stubsDir; + } + + /** + * Set the path to the stub file. + */ + public function setStubPath(string $dir) + { + $this->stubsDir = $dir; + + return $this; } /** diff --git a/src/Orangehill/Iseed/IseedCommand.php b/src/Orangehill/Iseed/IseedCommand.php old mode 100755 new mode 100644 index 1d2e38e..fa7c017 --- a/src/Orangehill/Iseed/IseedCommand.php +++ b/src/Orangehill/Iseed/IseedCommand.php @@ -47,13 +47,15 @@ public function handle() * * @return void */ - + public function fire() { + $iseed = app('iseed'); + // Retrieve the tables argument. Since we want to allow a default of "all tables", // make sure the tables argument is optional in getArguments() (see below). $tablesArg = $this->argument('tables'); - + if (empty($tablesArg)) { // Get all table names from the database $tables = app('iseed')->getAllTableNames(); @@ -61,7 +63,7 @@ public function fire() // Otherwise, split the provided comma-separated table names $tables = explode(',', $tablesArg); } - + // Convert other options as needed $max = intval($this->option('max')); $chunkSize = intval($this->option('chunksize')); @@ -75,28 +77,31 @@ public function fire() $prefix = $this->option('classnameprefix'); $suffix = $this->option('classnamesuffix'); $whereClause = $this->option('where'); - + $stubsDir = $this->option('stubsdir'); + + $iseed->setStubPath($stubsDir); + if ($max < 1) { $max = null; } if ($chunkSize < 1) { $chunkSize = null; } - + $tableIncrement = 0; foreach ($tables as $table) { $table = trim($table); $prerunEvent = isset($prerunEvents[$tableIncrement]) ? trim($prerunEvents[$tableIncrement]) : null; $postrunEvent = isset($postrunEvents[$tableIncrement]) ? trim($postrunEvents[$tableIncrement]) : null; $tableIncrement++; - + // generate file and class name based on name of the table list($fileName, $className) = $this->generateFileName($table, $prefix, $suffix); - + // if file does not exist or force option is turned on, generate seeder if (!\File::exists($fileName) || $this->option('force')) { $this->printResult( - app('iseed')->generateSeed( + $iseed->generateSeed( $table, $prefix, $suffix, @@ -116,11 +121,11 @@ public function fire() ); continue; } - + if ($this->confirm('File ' . $className . ' already exists. Do you wish to override it? [yes|no]')) { // Overwrite old seeder if confirmed $this->printResult( - app('iseed')->generateSeed( + $iseed->generateSeed( $table, $prefix, $suffix, @@ -141,7 +146,7 @@ public function fire() } } } - + /** * Get the console command arguments. @@ -178,6 +183,7 @@ protected function getOptions() array('classnameprefix', null, InputOption::VALUE_OPTIONAL, 'prefix for class and file name', null), array('classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null), array('where', null, InputOption::VALUE_OPTIONAL, 'where clause to filter records', null), + array('stubsdir', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', __DIR__ . DIRECTORY_SEPARATOR . 'stubs'), ); }