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
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
22
|
|
|
$basePath = $isModule ? $this->app->path($package) |
|
|
|
|
23
|
|
|
: $this->app->basePath('vendor/'.$package); |
24
|
|
|
|
25
|
|
|
if (file_exists($path = $basePath.'/database/migrations')) { |
26
|
|
|
$stubs = $this->app['files']->glob($path.'/*.php'); |
27
|
|
|
$existing = $this->app['files']->glob($this->app->databasePath('migrations/'.$package.'/*.php')); |
28
|
|
|
|
29
|
|
|
$migrations = collect($stubs)->flatMap(function ($migration) use ($existing, $package) { |
30
|
|
|
$sequence = mb_substr(basename($migration), 0, 17); |
31
|
|
|
$match = collect($existing)->first(function ($item, $key) use ($migration, $sequence) { |
|
|
|
|
32
|
|
|
return mb_strpos($item, str_replace($sequence, '', basename($migration))) !== false; |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
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))))]; |
36
|
|
|
})->toArray(); |
37
|
|
|
|
38
|
|
|
$this->publishes($migrations, $namespace.'-migrations'); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Publish package config. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
protected function publishesConfig(string $package, bool $isModule = false): void |
48
|
|
|
{ |
49
|
|
|
if ($this->publishesResources()) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$namespace = str_replace('laravel-', '', $package); |
54
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
55
|
|
|
$basePath = $isModule ? $this->app->path($package) |
56
|
|
|
: $this->app->basePath('vendor/'.$package); |
57
|
|
|
|
58
|
|
|
if (file_exists($path = $basePath.'/config/config.php')) { |
59
|
|
|
$this->publishes([$path => $this->app->configPath(str_replace('-', '.', $namespace).'.php')], $namespace.'-config'); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Publish package views. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
protected function publishesViews(string $package, bool $isModule = false): void |
69
|
|
|
{ |
70
|
|
|
if ($this->publishesResources()) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$namespace = str_replace('laravel-', '', $package); |
75
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
76
|
|
|
$basePath = $isModule ? $this->app->path($package) |
77
|
|
|
: $this->app->basePath('vendor/'.$package); |
78
|
|
|
|
79
|
|
|
if (file_exists($path = $basePath.'/resources/views')) { |
80
|
|
|
$this->publishes([$path => $this->app->resourcePath('views/vendor/'.$package)], $namespace.'-views'); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Publish package lang. |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function publishesLang(string $package, bool $isModule = false): void |
90
|
|
|
{ |
91
|
|
|
if ($this->publishesResources()) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$namespace = str_replace('laravel-', '', $package); |
96
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
97
|
|
|
$basePath = $isModule ? $this->app->path($package) |
98
|
|
|
: $this->app->basePath('vendor/'.$package); |
99
|
|
|
|
100
|
|
|
if (file_exists($path = $basePath.'/resources/lang')) { |
101
|
|
|
$this->publishes([$path => $this->app->resourcePath('lang/vendor/'.$package)], $namespace.'-lang'); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Register console commands. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
protected function registerCommands(): void |
111
|
|
|
{ |
112
|
|
|
// Register artisan commands |
113
|
|
|
foreach ($this->commands as $key => $value) { |
|
|
|
|
114
|
|
|
$this->app->singleton($value, $key); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->commands(array_values($this->commands)); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Can publish resources. |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function publishesResources(): bool |
126
|
|
|
{ |
127
|
|
|
return ! $this->app->environment('production'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Can autoload migrations. |
132
|
|
|
* |
133
|
|
|
* @param string $config |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
protected function autoloadMigrations(string $config): bool |
138
|
|
|
{ |
139
|
|
|
return ! $this->app->environment('production') && $this->app['config'][$config.'.autoload_migrations']; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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: