Skip to content

Commit

Permalink
Merge pull request #370 from arthvrian/factory-autoload
Browse files Browse the repository at this point in the history
Factories autoload
  • Loading branch information
kaidesu authored Oct 24, 2018
2 parents 21b85f0 + 6c4b5e2 commit 4315f28
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions resources/stubs/module/Providers/ModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function boot()
$this->loadViewsFrom(__DIR__.'/../Resources/Views', 'DummySlug');
$this->loadMigrationsFrom(__DIR__.'/../Database/Migrations', 'DummySlug');
$this->loadConfigsFrom(__DIR__.'/../config');
$this->loadFactoriesFrom(__DIR__.'/../Database/Factories');
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Console/Commands/ModuleSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function seed($slug)
$namespacePath = $this->module->getNamespace();
$rootSeeder = $module['basename'].'DatabaseSeeder';
$fullPath = $namespacePath.'\\'.$module['basename'].'\Database\Seeds\\'.$rootSeeder;
$factoriesPath = module_path($slug).DIRECTORY_SEPARATOR.'Database'.DIRECTORY_SEPARATOR.'Factories'.DIRECTORY_SEPARATOR;

if (class_exists($fullPath)) {
if ($this->option('class')) {
Expand All @@ -104,7 +105,9 @@ protected function seed($slug)
$params['--force'] = $option;
}

$this->call('db:seed', $params);
if (count(glob($factoriesPath.'*.php')) > 0) {
$this->call('db:seed', $params);
}

event($slug.'.module.seeded', [$module, $this->option()]);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,18 @@ protected function loadConfigsFrom($path)
$this->app['config']->set($key, array_merge_recursive(config($key, []), require $file));
}
}

/**
* Load all module factories
*
* @param string $path
*
* @return void
*/
protected function loadFactoriesFrom($path)
{
foreach (glob($path.'/*.php') as $file) {
require $file;
}
}
}

0 comments on commit 4315f28

Please sign in to comment.