1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Ship\Engine\Loaders; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use App\Ship\Engine\Butlers\Facades\ShipButler; |
7
|
|
|
use App\Ship\Engine\Butlers\Facades\LoaderButler; |
8
|
|
|
use File; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ProvidersLoaderTrait. |
12
|
|
|
* |
13
|
|
|
* @author Mahmoud Zalt <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
trait ProvidersLoaderTrait |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param $containerName |
20
|
|
|
*/ |
21
|
|
|
public function loadProvidersFromContainers($containerName) |
22
|
|
|
{ |
23
|
|
|
$containerProvidersDirectory = base_path('app/Containers/' . $containerName . '/Providers'); |
24
|
|
|
|
25
|
|
|
$this->loadProviders($containerProvidersDirectory); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* loadProvidersFromShip |
30
|
|
|
*/ |
31
|
|
|
public function loadProvidersFromShip() |
32
|
|
|
{ |
33
|
|
|
foreach ($this->serviceProviders as $providerClass) { |
|
|
|
|
34
|
|
|
$this->loadProvider($providerClass); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $directory |
40
|
|
|
*/ |
41
|
|
|
private function loadProviders($directory) |
42
|
|
|
{ |
43
|
|
|
if (File::isDirectory($directory)) { |
44
|
|
|
|
45
|
|
|
$files = File::allFiles($directory); |
46
|
|
|
|
47
|
|
|
$mainServiceProviderNameStartWith = 'Main'; |
48
|
|
|
|
49
|
|
|
foreach ($files as $file) { |
50
|
|
|
|
51
|
|
|
if (File::isFile($file)) { |
52
|
|
|
|
53
|
|
|
// Check if this is the Main Service Provider |
54
|
|
|
if (ShipButler::stringStartsWith($file->getFilename(), $mainServiceProviderNameStartWith)) { |
55
|
|
|
|
56
|
|
|
$serviceProviderClass = LoaderButler::getClassFullNameFromFile($file->getPathname()); |
57
|
|
|
|
58
|
|
|
$this->loadProvider($serviceProviderClass); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/* |
67
|
|
|
* loadProvider |
68
|
|
|
*/ |
69
|
|
|
private function loadProvider($provider) |
70
|
|
|
{ |
71
|
|
|
App::register($provider); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* loadContainersInternalProviders |
76
|
|
|
*/ |
77
|
|
|
public function loadContainersInternalProviders() |
78
|
|
|
{ |
79
|
|
|
foreach ($this->containerServiceProviders as $provider) { |
|
|
|
|
80
|
|
|
$this->loadProvider($provider); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
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: