Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stubsdir CLI option #238

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion src/Orangehill/Iseed/Iseed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
28 changes: 17 additions & 11 deletions src/Orangehill/Iseed/IseedCommand.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@ 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();
} else {
// 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'));
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -141,7 +146,7 @@ public function fire()
}
}
}


/**
* Get the console command arguments.
Expand Down Expand Up @@ -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'),
);
}

Expand Down