1
|
|
|
<?php |
2
|
|
|
namespace CodexShaper\DBM\Commands; |
3
|
|
|
|
4
|
|
|
use CodexShaper\DBM\ManagerServiceProvider; |
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class InstallDatabaseManager extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'dbm:install {mongodb?} {--force=}'; |
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Install the Laravel Database Manager'; |
24
|
|
|
/** |
25
|
|
|
* The database Seeder Path. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $seedersPath = __DIR__ . '/../../database/seeds/'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get Option |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
protected function getOptions() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production', null], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the composer command for the environment. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function findComposer() |
49
|
|
|
{ |
50
|
|
|
if (file_exists(getcwd() . '/composer.phar')) { |
51
|
|
|
return '"' . PHP_BINARY . '" ' . getcwd() . '/composer.phar'; |
52
|
|
|
} |
53
|
|
|
return 'composer'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Execute the console command. |
58
|
|
|
* |
59
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function handle(Filesystem $filesystem) |
64
|
|
|
{ |
65
|
|
|
$composer = $this->findComposer(); |
66
|
|
|
if ($this->argument('mongodb') == 'mongodb') { |
67
|
|
|
$this->info('Installing MongoDB package'); |
68
|
|
|
$process = new Process($composer . ' require jenssegers/mongodb'); |
|
|
|
|
69
|
|
|
$process->setTimeout(null); // Setting timeout to null to prevent installation from stopping at a certain point in time |
70
|
|
|
$process->setWorkingDirectory(base_path())->run(); |
71
|
|
|
} |
72
|
|
|
$this->info('Publishing the Database Manager assets, database, and config files'); |
73
|
|
|
// Publish only relevant resources on install |
74
|
|
|
$tags = ['dbm.config']; |
75
|
|
|
$this->call('vendor:publish', ['--provider' => ManagerServiceProvider::class, '--tag' => $tags]); |
76
|
|
|
// Generate Storage Link |
77
|
|
|
$this->info('Generate storage symblink'); |
78
|
|
|
$this->call('storage:link'); |
79
|
|
|
// Dump autoload |
80
|
|
|
$this->info('Dumping the autoloaded files and reloading all new files'); |
81
|
|
|
$process = new Process($composer . ' dump-autoload'); |
82
|
|
|
$process->setTimeout(null); // Setting timeout to null to prevent installation from stopping at a certain point in time |
83
|
|
|
$process->setWorkingDirectory(base_path())->run(); |
84
|
|
|
// Migrate database |
85
|
|
|
$this->info('Migrating the database tables into your application'); |
86
|
|
|
$this->call('migrate', ['--force' => $this->option('force')]); |
87
|
|
|
// Install laravel passport |
88
|
|
|
$this->info('Install Passport'); |
89
|
|
|
$this->call('passport:install', ['--force' => $this->option('force')]); |
90
|
|
|
// Load Custom Database Manager routes |
91
|
|
|
$this->info('Adding Database Manager routes'); |
92
|
|
|
$web_routes_contents = $filesystem->get(base_path('routes/web.php')); |
93
|
|
|
$api_routes_contents = $filesystem->get(base_path('routes/api.php')); |
94
|
|
|
if (false === strpos($web_routes_contents, 'DBM::webRoutes();')) { |
95
|
|
|
$filesystem->append( |
96
|
|
|
base_path('routes/web.php'), |
97
|
|
|
"\n\nDBM::webRoutes();\n" |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
if (false === strpos($api_routes_contents, 'DBM::apiRoutes();')) { |
101
|
|
|
$filesystem->append( |
102
|
|
|
base_path('routes/api.php'), |
103
|
|
|
"\n\nDBM::apiRoutes();\n" |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
// Database seeder |
107
|
|
|
$this->info('Seeding...'); |
108
|
|
|
$class = 'DatabaseManagerSeeder'; |
109
|
|
|
$file = $this->seedersPath . $class . '.php'; |
110
|
|
|
if (file_exists($file) && !class_exists($class)) { |
111
|
|
|
require_once $file; |
112
|
|
|
} |
113
|
|
|
with(new $class())->run(); |
114
|
|
|
$this->info('Seeding Completed'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|