|
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 publishMigrations(string $package): void |
|
15
|
|
|
{ |
|
16
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
17
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
|
18
|
|
|
$basePath = starts_with($package, 'cortex') ? $this->app->path($package) |
|
|
|
|
|
|
19
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
20
|
|
|
|
|
21
|
|
|
if (file_exists($path = $basePath.'/database/migrations')) { |
|
22
|
|
|
$stubs = $this->app['files']->glob($path.'/*.php.stub'); |
|
23
|
|
|
$existing = $this->app['files']->glob($this->app->databasePath('migrations/'.$package.'/*.php')); |
|
24
|
|
|
|
|
25
|
|
|
$migrations = collect($stubs)->flatMap(function ($migration) use ($existing, $package) { |
|
26
|
|
|
$sequence = substr(basename($migration), 0, 2); |
|
27
|
|
|
$match = collect($existing)->first(function ($item, $key) use ($migration, $sequence) { |
|
|
|
|
|
|
28
|
|
|
return strpos($item, str_replace(['.stub', $sequence], '', basename($migration))) !== false; |
|
29
|
|
|
}); |
|
30
|
|
|
|
|
31
|
|
|
return [$migration => $this->app->databasePath('migrations/'.$package.'/'.($match ? basename($match) : date('Y_m_d_His', time() + $sequence).str_replace(['.stub', $sequence], '', basename($migration))))]; |
|
|
|
|
|
|
32
|
|
|
})->toArray(); |
|
33
|
|
|
|
|
34
|
|
|
$this->publishes($migrations, $namespace.'-migrations'); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Publish package config. |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function publishConfig(string $package): void |
|
44
|
|
|
{ |
|
45
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
46
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
|
47
|
|
|
$basePath = starts_with($package, 'cortex') ? $this->app->path($package) |
|
|
|
|
|
|
48
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
49
|
|
|
|
|
50
|
|
|
if (file_exists($path = $basePath.'/config/config.php')) { |
|
51
|
|
|
$this->publishes([$path => $this->app->configPath(str_replace('-', '.', $namespace).'.php')], $namespace.'-config'); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Publish package views. |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function publishViews(string $package): void |
|
61
|
|
|
{ |
|
62
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
63
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
|
64
|
|
|
$basePath = starts_with($package, 'cortex') ? $this->app->path($package) |
|
|
|
|
|
|
65
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
66
|
|
|
|
|
67
|
|
|
if (file_exists($path = $basePath.'/resources/views')) { |
|
68
|
|
|
$this->publishes([$path => $this->app->resourcePath('views/vendor/'.$package)], $namespace.'-views'); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Publish package lang. |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function publishLang(string $package): void |
|
78
|
|
|
{ |
|
79
|
|
|
$namespace = str_replace('laravel-', '', $package); |
|
80
|
|
|
$namespace = str_replace(['/', '\\', '.', '_'], '-', $namespace); |
|
81
|
|
|
$basePath = starts_with($package, 'cortex') ? $this->app->path($package) |
|
|
|
|
|
|
82
|
|
|
: $this->app->basePath('vendor/'.$package); |
|
83
|
|
|
|
|
84
|
|
|
if (file_exists($path = $basePath.'/resources/lang')) { |
|
85
|
|
|
$this->publishes([$path => $this->app->resourcePath('lang/vendor/'.$package)], $namespace.'-lang'); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Register console commands. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function registerCommands(): void |
|
95
|
|
|
{ |
|
96
|
|
|
// Register artisan commands |
|
97
|
|
|
foreach ($this->commands as $key => $value) { |
|
|
|
|
|
|
98
|
|
|
$this->app->singleton($value, $key); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->commands(array_values($this->commands)); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
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: