|
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
|
69 |
|
public function boot() |
|
29
|
|
|
{ |
|
30
|
69 |
|
$this->registerNamespaces(); |
|
31
|
|
|
|
|
32
|
69 |
|
$this->registerModules(); |
|
33
|
69 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Register all modules. |
|
37
|
|
|
*/ |
|
38
|
69 |
|
protected function registerModules() |
|
39
|
|
|
{ |
|
40
|
69 |
|
$this->app->register(BootstrapServiceProvider::class); |
|
41
|
69 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Register the service provider. |
|
45
|
|
|
*/ |
|
46
|
69 |
|
public function register() |
|
47
|
|
|
{ |
|
48
|
69 |
|
$this->registerServices(); |
|
49
|
69 |
|
$this->setupStubPath(); |
|
50
|
69 |
|
$this->registerProviders(); |
|
51
|
69 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Setup stub path. |
|
55
|
|
|
*/ |
|
56
|
69 |
|
public function setupStubPath() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->app->booted(function ($app) { |
|
59
|
69 |
|
Stub::setBasePath(__DIR__ . '/Commands/stubs'); |
|
60
|
|
|
|
|
61
|
69 |
|
if ($app['modules']->config('stubs.enabled') === true) { |
|
62
|
|
|
Stub::setBasePath($app['modules']->config('stubs.path')); |
|
63
|
|
|
} |
|
64
|
69 |
|
}); |
|
65
|
69 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Register package's namespaces. |
|
69
|
|
|
*/ |
|
70
|
69 |
|
protected function registerNamespaces() |
|
71
|
|
|
{ |
|
72
|
69 |
|
$configPath = __DIR__ . '/../config/config.php'; |
|
73
|
69 |
|
$this->mergeConfigFrom($configPath, 'modules'); |
|
74
|
69 |
|
$this->publishes([ |
|
75
|
69 |
|
$configPath => config_path('modules.php'), |
|
76
|
69 |
|
], 'config'); |
|
77
|
69 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Register the service provider. |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function registerServices() |
|
83
|
|
|
{ |
|
84
|
69 |
|
$this->app->singleton('modules', function ($app) { |
|
85
|
69 |
|
$path = $app['config']->get('modules.paths.modules'); |
|
86
|
|
|
|
|
87
|
69 |
|
return new Repository($app, $path); |
|
88
|
69 |
|
}); |
|
89
|
69 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the services provided by the provider. |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function provides() |
|
97
|
|
|
{ |
|
98
|
|
|
return ['modules']; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Register providers. |
|
103
|
|
|
*/ |
|
104
|
69 |
|
protected function registerProviders() |
|
105
|
|
|
{ |
|
106
|
69 |
|
$this->app->register(ConsoleServiceProvider::class); |
|
107
|
69 |
|
$this->app->register(ContractsServiceProvider::class); |
|
108
|
69 |
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: