1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Laravel Zero. |
7
|
|
|
* |
8
|
|
|
* (c) Nuno Maduro <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace LaravelZero\Framework\Components\Database; |
15
|
|
|
|
16
|
|
|
use function collect; |
17
|
|
|
use function is_array; |
18
|
|
|
use function class_exists; |
19
|
|
|
use Illuminate\Support\Facades\File; |
20
|
|
|
use LaravelZero\Framework\Components\AbstractComponentProvider; |
21
|
|
|
|
22
|
|
|
class Provider extends AbstractComponentProvider |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
29 |
|
public function isAvailable(): bool |
28
|
|
|
{ |
29
|
29 |
|
return class_exists(\Illuminate\Database\DatabaseServiceProvider::class) && is_array( |
30
|
29 |
|
$this->app['config']->get('database', false) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
29 |
|
public function register(): void |
38
|
|
|
{ |
39
|
29 |
|
if (File::exists($this->app->configPath('database.php'))) { |
40
|
29 |
|
$this->mergeConfigFrom($this->app->configPath('database.php'), 'database'); |
41
|
|
|
} |
42
|
|
|
|
43
|
29 |
|
$this->registerDatabaseService(); |
44
|
|
|
|
45
|
29 |
|
$this->registerMigrationService(); |
46
|
29 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
29 |
|
public function boot(): void |
52
|
|
|
{ |
53
|
29 |
|
if ($this->app->environment() !== 'production') { |
54
|
28 |
|
$this->commands( |
55
|
|
|
[ |
56
|
28 |
|
\Illuminate\Database\Console\Migrations\MigrateMakeCommand::class, |
57
|
|
|
\Illuminate\Database\Console\Seeds\SeederMakeCommand::class, |
58
|
|
|
\Illuminate\Foundation\Console\ModelMakeCommand::class, |
59
|
|
|
\Illuminate\Database\Console\Seeds\SeedCommand::class, |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
29 |
|
$this->commands( |
65
|
|
|
[ |
66
|
29 |
|
\Illuminate\Database\Console\Migrations\FreshCommand::class, |
67
|
|
|
\Illuminate\Database\Console\Migrations\InstallCommand::class, |
68
|
|
|
\Illuminate\Database\Console\Migrations\MigrateCommand::class, |
69
|
|
|
\Illuminate\Database\Console\Migrations\RefreshCommand::class, |
70
|
|
|
\Illuminate\Database\Console\Migrations\ResetCommand::class, |
71
|
|
|
\Illuminate\Database\Console\Migrations\RollbackCommand::class, |
72
|
|
|
\Illuminate\Database\Console\Migrations\StatusCommand::class, |
73
|
|
|
] |
74
|
|
|
); |
75
|
29 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Registers the database service. |
79
|
|
|
* |
80
|
|
|
* Makes this Capsule Instance available globally via static methods. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
29 |
|
protected function registerDatabaseService(): void |
85
|
|
|
{ |
86
|
29 |
|
$this->app->alias('db', \Illuminate\Database\ConnectionResolverInterface::class); |
87
|
29 |
|
$this->app->register(\Illuminate\Database\DatabaseServiceProvider::class); |
88
|
|
|
|
89
|
29 |
|
if (File::exists($this->app->databasePath('seeds'))) { |
90
|
17 |
|
collect(File::files($this->app->databasePath('seeds')))->each( |
91
|
|
|
function ($file) { |
92
|
|
|
File::requireOnce($file); |
93
|
17 |
|
} |
94
|
|
|
); |
95
|
|
|
} |
96
|
29 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Registers the migration service. |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
29 |
|
protected function registerMigrationService(): void |
104
|
|
|
{ |
105
|
29 |
|
$config = $this->app['config']; |
106
|
29 |
|
$config->set('database.migrations', $config->get('database.migrations') ?: 'migrations'); |
107
|
29 |
|
$this->app->register(\Illuminate\Database\MigrationServiceProvider::class); |
108
|
|
|
|
109
|
29 |
|
$this->app->alias( |
110
|
29 |
|
'migration.repository', |
111
|
29 |
|
\Illuminate\Database\Migrations\MigrationRepositoryInterface::class |
112
|
|
|
); |
113
|
|
|
|
114
|
29 |
|
$this->app->singleton( |
115
|
29 |
|
'migrator', |
116
|
|
|
function ($app) { |
117
|
29 |
|
$repository = $app['migration.repository']; |
118
|
|
|
|
119
|
29 |
|
return new Migrator($repository, $app['db'], $app['files']); |
120
|
29 |
|
} |
121
|
|
|
); |
122
|
|
|
|
123
|
29 |
|
$this->app->alias( |
124
|
29 |
|
'migrator', |
125
|
29 |
|
\Illuminate\Database\Migrations\Migrator::class |
126
|
|
|
); |
127
|
29 |
|
} |
128
|
|
|
} |
129
|
|
|
|