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