generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
PackageServiceProvider.php
158 lines (120 loc) · 4.94 KB
/
PackageServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Spatie\LaravelPackageTools;
use Carbon\Carbon;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use ReflectionClass;
use Spatie\LaravelPackageTools\Exceptions\InvalidPackage;
abstract class PackageServiceProvider extends ServiceProvider
{
protected Package $package;
abstract public function configurePackage(Package $package): void;
public function register()
{
$this->registeringPackage();
$this->package = new Package();
$this->package->setBasePath($this->getPackageBaseDir());
$this->configurePackage($this->package);
if (empty($this->package->name)) {
throw InvalidPackage::nameIsRequired();
}
if ($configFileName = $this->package->configFileName) {
$this->mergeConfigFrom($this->package->basePath("/../config/{$configFileName}.php"), $configFileName);
}
$this->packageRegistered();
return $this;
}
public function boot()
{
$this->bootingPackage();
if ($this->app->runningInConsole()) {
if ($configFileName = $this->package->configFileName) {
$this->publishes([
$this->package->basePath("/../config/{$configFileName}.php") => config_path("{$configFileName}.php"),
], "{$this->package->shortName()}-config");
}
if ($this->package->hasViews) {
$this->publishes([
$this->package->basePath('/../resources/views') => base_path("resources/views/vendor/{$this->package->shortName()}"),
], "{$this->package->shortName()}-views");
}
$now = Carbon::now();
foreach ($this->package->migrationFileNames as $migrationFileName) {
if (! $this->migrationFileExists($migrationFileName)) {
$this->publishes([
$this->package->basePath("/../database/migrations/{$migrationFileName}.php.stub") => database_path('migrations/' . $now->addSecond()->format('Y_m_d_His') . '_' . Str::finish($migrationFileName, '.php')),
], "{$this->package->shortName()}-migrations");
}
}
if ($this->package->hasTranslations) {
$this->publishes([
$this->package->basePath('/../resources/lang') => resource_path("lang/vendor/{$this->package->shortName()}"),
], "{$this->package->shortName()}-translations");
}
if ($this->package->hasAssets) {
$this->publishes([
$this->package->basePath('/../resources/dist') => public_path("vendor/{$this->package->shortName()}"),
], "{$this->package->shortName()}-assets");
}
}
if (! empty($this->package->commands)) {
$this->commands($this->package->commands);
}
if ($this->package->hasTranslations) {
$this->loadTranslationsFrom(
$this->package->basePath('/../resources/lang/'),
$this->package->shortName()
);
}
if ($this->package->hasViews) {
$this->loadViewsFrom($this->package->basePath('/../resources/views'), $this->package->shortName());
}
foreach ($this->package->viewComponents as $componentClass => $prefix) {
$this->loadViewComponentsAs($prefix, [$componentClass]);
}
if (count($this->package->viewComponents)) {
$this->publishes([
$this->package->basePath('/../Components') => base_path("app/View/Components/vendor/{$this->package->shortName()}"),
], "{$this->package->name}-components");
}
foreach ($this->package->routeFileNames as $routeFileName) {
$this->loadRoutesFrom("{$this->package->basePath('/../routes/')}{$routeFileName}.php");
}
foreach ($this->package->sharedViewData as $name => $value) {
View::share($name, $value);
}
foreach ($this->package->viewComposers as $viewName => $viewComposer) {
View::composer($viewName, $viewComposer);
}
$this->packageBooted();
return $this;
}
public static function migrationFileExists(string $migrationFileName): bool
{
$len = strlen($migrationFileName) + 4;
foreach (glob(database_path("migrations/*.php")) as $filename) {
if ((substr($filename, -$len) === $migrationFileName . '.php')) {
return true;
}
}
return false;
}
public function registeringPackage()
{
}
public function packageRegistered()
{
}
public function bootingPackage()
{
}
public function packageBooted()
{
}
protected function getPackageBaseDir(): string
{
$reflector = new ReflectionClass(get_class($this));
return dirname($reflector->getFileName());
}
}