|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Support\Traits; |
|
6
|
|
|
|
|
7
|
|
|
trait ConsoleTools |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Publish package migrations. |
|
11
|
|
|
* |
|
12
|
|
|
* @return void |
|
13
|
|
|
*/ |
|
14
|
|
|
protected function publishesMigrations(string $package, bool $isModule = false): void |
|
15
|
|
|
{ |
|
16
|
|
|
if (! $this->publishesResources()) { |
|
17
|
|
|
return; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
21
|
|
|
$basePath = $isModule ? $this->app->path($package) |
|
|
|
|
|
|
22
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
23
|
|
|
|
|
24
|
|
|
if (file_exists($path = $basePath.'/database/migrations')) { |
|
25
|
|
|
$stubs = $this->app['files']->glob($path.'/*.php'); |
|
26
|
|
|
$existing = $this->app['files']->glob($this->app->databasePath('migrations/'.$package.'/*.php')); |
|
27
|
|
|
|
|
28
|
|
|
$migrations = collect($stubs)->flatMap(function ($migration) use ($existing, $package) { |
|
29
|
|
|
$sequence = mb_substr(basename($migration), 0, 17); |
|
30
|
|
|
$match = collect($existing)->first(function ($item, $key) use ($migration, $sequence) { |
|
|
|
|
|
|
31
|
|
|
return mb_strpos($item, str_replace($sequence, '', basename($migration))) !== false; |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
return [$migration => $this->app->databasePath('migrations/'.$package.'/'.($match ? basename($match) : date('Y_m_d_His', time() + mb_substr($sequence, -6)).str_replace($sequence, '', basename($migration))))]; |
|
35
|
|
|
})->toArray(); |
|
36
|
|
|
|
|
37
|
|
|
$this->publishes($migrations, $namespace.'::migrations'); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Publish package config. |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function publishesConfig(string $package, bool $isModule = false): void |
|
47
|
|
|
{ |
|
48
|
|
|
if (! $this->publishesResources()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
53
|
|
|
$basePath = $isModule ? $this->app->path($package) |
|
54
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
55
|
|
|
|
|
56
|
|
|
if (file_exists($path = $basePath.'/config/config.php')) { |
|
57
|
|
|
$this->publishes([$path => $this->app->configPath(str_replace('/', '.', $namespace).'.php')], $namespace.'::config'); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Publish package views. |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function publishesViews(string $package, bool $isModule = false): void |
|
67
|
|
|
{ |
|
68
|
|
|
if (! $this->publishesResources()) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
73
|
|
|
$basePath = $isModule ? $this->app->path($package) |
|
74
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
75
|
|
|
|
|
76
|
|
|
if (file_exists($path = $basePath.'/resources/views')) { |
|
77
|
|
|
$this->publishes([$path => $this->app->resourcePath('views/vendor/'.$package)], $namespace.'::views'); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Publish package lang. |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function publishesLang(string $package, bool $isModule = false): void |
|
87
|
|
|
{ |
|
88
|
|
|
if (! $this->publishesResources()) { |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
93
|
|
|
$basePath = $isModule ? $this->app->path($package) |
|
94
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
95
|
|
|
|
|
96
|
|
|
if (file_exists($path = $basePath.'/resources/lang')) { |
|
97
|
|
|
$this->publishes([$path => $this->app->resourcePath('lang/vendor/'.$package)], $namespace.'::lang'); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Determine if the application is running in the console. |
|
103
|
|
|
* |
|
104
|
|
|
* @TODO: Implement this method to detect if we're in active dev zone or not! |
|
105
|
|
|
* Ex: running inside cortex/console action |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function runningInDevzone() |
|
110
|
|
|
{ |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Register console commands. |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function registerCommands(): void |
|
120
|
|
|
{ |
|
121
|
|
|
if (! $this->app->runningInConsole() && ! $this->runningInDevzone()) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (! $this->app->environment('production') && property_exists($this, 'devCommands')) { |
|
126
|
|
|
// Register artisan devCommands |
|
127
|
|
|
foreach ($this->devCommands as $key => $value) { |
|
|
|
|
|
|
128
|
|
|
$this->app->singleton($value, $key); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->commands(array_values($this->devCommands)); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (property_exists($this, 'commands')) { |
|
135
|
|
|
// Register artisan commands |
|
136
|
|
|
foreach ($this->commands as $key => $value) { |
|
|
|
|
|
|
137
|
|
|
$this->app->singleton($value, $key); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->commands(array_values($this->commands)); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Can publish resources. |
|
146
|
|
|
* |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function publishesResources(): bool |
|
150
|
|
|
{ |
|
151
|
|
|
return ! $this->app->environment('production') || $this->app->runningInConsole() || $this->runningInDevzone(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Can autoload migrations. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $config |
|
158
|
|
|
* |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function autoloadMigrations(string $config): bool |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->publishesResources() && $this->app['config'][str_replace(['laravel-', '/'], ['', '.'], $config).'.autoload_migrations']; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: