1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules; |
4
|
|
|
|
5
|
|
|
use Collective\Html\FormFacade; |
6
|
|
|
use Collective\Html\HtmlFacade; |
7
|
|
|
use Collective\Html\HtmlServiceProvider; |
8
|
|
|
use Illuminate\Foundation\AliasLoader; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
use Nwidart\Modules\Facades\Module; |
|
|
|
|
11
|
|
|
use Nwidart\Modules\Providers\BootstrapServiceProvider; |
12
|
|
|
use Nwidart\Modules\Providers\ConsoleServiceProvider; |
13
|
|
|
use Nwidart\Modules\Providers\ContractsServiceProvider; |
14
|
|
|
use Pingpong\Support\Stub; |
15
|
|
|
|
16
|
|
|
class LaravelModulesServiceProvider extends ServiceProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Indicates if loading of the provider is deferred. |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $defer = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Booting the package. |
27
|
|
|
*/ |
28
|
55 |
|
public function boot() |
29
|
|
|
{ |
30
|
55 |
|
$this->registerNamespaces(); |
31
|
|
|
|
32
|
55 |
|
$this->registerModules(); |
33
|
55 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Register all modules. |
37
|
|
|
*/ |
38
|
55 |
|
protected function registerModules() |
39
|
|
|
{ |
40
|
55 |
|
$this->app->register(BootstrapServiceProvider::class); |
41
|
55 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Register the service provider. |
45
|
|
|
*/ |
46
|
55 |
|
public function register() |
47
|
|
|
{ |
48
|
55 |
|
$this->registerServices(); |
49
|
55 |
|
$this->setupStubPath(); |
50
|
55 |
|
$this->registerProviders(); |
51
|
55 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Setup stub path. |
55
|
|
|
*/ |
56
|
55 |
|
public function setupStubPath() |
57
|
|
|
{ |
58
|
|
|
$this->app->booted(function ($app) { |
59
|
55 |
|
Stub::setBasePath(__DIR__.'/Commands/stubs'); |
60
|
|
|
|
61
|
55 |
|
if ($app['modules']->config('stubs.enabled') === true) { |
62
|
|
|
Stub::setBasePath($app['modules']->config('stubs.path')); |
63
|
|
|
} |
64
|
55 |
|
}); |
65
|
55 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Register package's namespaces. |
69
|
|
|
*/ |
70
|
55 |
|
protected function registerNamespaces() |
71
|
|
|
{ |
72
|
55 |
|
$configPath = __DIR__ . '/../config/config.php'; |
73
|
55 |
|
$this->mergeConfigFrom($configPath, 'modules'); |
74
|
55 |
|
$this->publishes([ |
75
|
55 |
|
$configPath => config_path('modules.php') |
76
|
55 |
|
], 'config'); |
77
|
55 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Register laravel html package. |
81
|
|
|
*/ |
82
|
|
|
protected function registerHtml() |
83
|
|
|
{ |
84
|
|
|
$this->app->register(HtmlServiceProvider::class); |
85
|
|
|
|
86
|
|
|
$aliases = [ |
87
|
|
|
'HTML' => HtmlFacade::class, |
88
|
|
|
'Form' => FormFacade::class, |
89
|
|
|
'Module' => Module::class, |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
AliasLoader::getInstance($aliases)->register(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Register the service provider. |
97
|
|
|
*/ |
98
|
|
|
protected function registerServices() |
99
|
|
|
{ |
100
|
55 |
|
$this->app->singleton('modules', function ($app) { |
101
|
55 |
|
$path = $app['config']->get('modules.paths.modules'); |
102
|
|
|
|
103
|
55 |
|
return new Repository($app, $path); |
104
|
55 |
|
}); |
105
|
55 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the services provided by the provider. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function provides() |
113
|
|
|
{ |
114
|
|
|
return ['modules']; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Register providers. |
119
|
|
|
*/ |
120
|
55 |
|
protected function registerProviders() |
121
|
|
|
{ |
122
|
55 |
|
$this->app->register(ConsoleServiceProvider::class); |
123
|
55 |
|
$this->app->register(ContractsServiceProvider::class); |
124
|
55 |
|
} |
125
|
|
|
} |
126
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: