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
|
|
|
* @param array $commands |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
protected function registerCommands(array $commands): void |
122
|
|
|
{ |
123
|
|
|
if (! $this->app->runningInConsole() && ! $this->runningInDevzone()) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
foreach ($commands as $key => $value) { |
128
|
|
|
$this->app->singleton($value, $key); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->commands(array_values($commands)); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Can publish resources. |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
protected function publishesResources(): bool |
140
|
|
|
{ |
141
|
|
|
return ! $this->app->environment('production') || $this->app->runningInConsole() || $this->runningInDevzone(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Can autoload migrations. |
146
|
|
|
* |
147
|
|
|
* @param string $config |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
protected function autoloadMigrations(string $config): bool |
152
|
|
|
{ |
153
|
|
|
return $this->publishesResources() && $this->app['config'][str_replace(['laravel-', '/'], ['', '.'], $config).'.autoload_migrations']; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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: