From 0d1e80f2636b4a8a7a010fa05c6445ec0a0f42b1 Mon Sep 17 00:00:00 2001 From: Anton Ukhanev Date: Wed, 29 May 2024 21:51:01 +0200 Subject: [PATCH] Add `stubsdir` CLI option This allows changing the directory where stubs will be used from. --- README.md | 11 +++++++++++ src/Orangehill/Iseed/Iseed.php | 15 ++++++++++++++- src/Orangehill/Iseed/IseedCommand.php | 8 ++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) mode change 100755 => 100644 src/Orangehill/Iseed/IseedCommand.php diff --git a/README.md b/README.md index 110491f..35d2eb7 100755 --- a/README.md +++ b/README.md @@ -186,6 +186,17 @@ Example: php artisan iseed users --noindex ``` +### 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 20e5c8e..67aec6a 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); } @@ -206,7 +209,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 58517ec..efd533e --- a/src/Orangehill/Iseed/IseedCommand.php +++ b/src/Orangehill/Iseed/IseedCommand.php @@ -66,6 +66,9 @@ public function fire() $direction = $this->option('direction'); $prefix = $this->option('classnameprefix'); $suffix = $this->option('classnamesuffix'); + $stubsDir = $this->option('stubsdir'); + + $iseed = app('iseed')->setStubPath($stubsDir); if ($max < 1) { $max = null; @@ -94,7 +97,7 @@ public function fire() // 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, @@ -117,7 +120,7 @@ public function fire() if ($this->confirm('File ' . $className . ' already exist. Do you wish to override it? [yes|no]')) { // if user said yes overwrite old seeder $this->printResult( - app('iseed')->generateSeed( + $iseed->generateSeed( $table, $prefix, $suffix, @@ -172,6 +175,7 @@ protected function getOptions() array('direction', null, InputOption::VALUE_OPTIONAL, 'orderby direction', null), 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('stubsdir', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', __DIR__ . DIRECTORY_SEPARATOR . 'stubs'), ); }