Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit c4b2555

Browse files
authored
Merge pull request #24 from alexgiuvara/autoload_service_migrations
Autoload service migrations
2 parents 9b9aacc + 94d9242 commit c4b2555

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/Generators/ServiceGenerator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ServiceGenerator extends Generator
2828
protected $directories = [
2929
'Console/',
3030
'database/',
31+
'database/factories/',
3132
'database/migrations/',
3233
'database/seeds/',
3334
'Http/',
@@ -70,6 +71,8 @@ public function generate($name)
7071

7172
$this->addWelcomeViewFile($path);
7273

74+
$this->addModelFactory($path);
75+
7376
return new Service(
7477
$name,
7578
$slug,
@@ -197,4 +200,17 @@ protected function getStub()
197200
{
198201
return __DIR__.'/stubs/service.stub';
199202
}
203+
204+
/**
205+
* Add the ModelFactory file.
206+
*
207+
* @param string $path
208+
*/
209+
public function addModelFactory($path)
210+
{
211+
$modelFactory = file_get_contents(__DIR__ . '/stubs/model-factory.stub');
212+
$this->createFile($path . '/database/factories/ModelFactory.php', $modelFactory);
213+
214+
unset($modelFactory);
215+
}
200216
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Model Factories
6+
|--------------------------------------------------------------------------
7+
|
8+
| Here you may define all of your model factories. Model factories give
9+
| you a convenient way to create models for testing and seeding your
10+
| database. Just tell the factory how a default model should look.
11+
|
12+
*/
13+
14+
$factory->define('User namespace here', function (Faker\Generator $faker) {
15+
/**
16+
* @var string
17+
*/
18+
static $password;
19+
20+
return [
21+
'name' => $faker->name,
22+
'email' => $faker->safeEmail,
23+
'password' => $password ?: $password = bcrypt('secret'),
24+
'remember_token' => str_random(10)
25+
];
26+
});

src/Generators/stubs/serviceprovider.stub

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,35 @@ namespace {{namespace}};
33

44
use View;
55
use Lang;
6+
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
67
use Illuminate\Support\ServiceProvider;
78
use {{namespace}}\RouteServiceProvider;
89
use Illuminate\Translation\TranslationServiceProvider;
910

1011
class {{name}}ServiceProvider extends ServiceProvider
1112
{
13+
/**
14+
* Bootstrap migrations and factories for:
15+
* - `php artisan migrate` command.
16+
* - factory() helper.
17+
*
18+
* Previous usage:
19+
* php artisan migrate --path=src/Services/{{name}}/database/migrations
20+
* Now:
21+
* php artisan migrate
22+
*
23+
* @return void
24+
*/
25+
public function boot()
26+
{
27+
$this->loadMigrationsFrom([
28+
realpath(__DIR__ . '/../database/migrations')
29+
]);
30+
31+
$this->app->make(EloquentFactory::class)
32+
->load(realpath(__DIR__ . '/../database/factories'));
33+
}
34+
1235
/**
1336
* Register the {{name}} service provider.
1437
*

0 commit comments

Comments
 (0)