1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Ship\Engine\Butlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class LoaderButler |
10
|
|
|
* |
11
|
|
|
* Helper Class to serve the Components loaders only. |
12
|
|
|
* |
13
|
|
|
* @author Mahmoud Zalt <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class LoaderButler |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the containers namespace value from the containers config file |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function getContainersNamespace() |
24
|
|
|
{ |
25
|
|
|
return Config::get('hello.containers.namespace'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the containers names |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function getContainersNames() |
34
|
|
|
{ |
35
|
|
|
$containersNames = []; |
36
|
|
|
|
37
|
|
|
foreach ($this->getContainersPaths() as $containersPath) { |
38
|
|
|
$containersNames[] = basename($containersPath); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $containersNames; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the port folders names |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getShipFoldersNames() |
50
|
|
|
{ |
51
|
|
|
$portFoldersNames = []; |
52
|
|
|
|
53
|
|
|
foreach ($this->getShipPath() as $portFoldersPath) { |
54
|
|
|
$portFoldersNames[] = basename($portFoldersPath); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $portFoldersNames; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* get containers directories paths |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getContainersPaths() |
66
|
|
|
{ |
67
|
|
|
$containersPaths = File::directories(app_path('Containers')); |
68
|
|
|
|
69
|
|
|
// TODO: preparing some code to implement the Ships concept to Shipo |
70
|
|
|
|
71
|
|
|
// // Search for Ships "_SHIP" of Containers |
|
|
|
|
72
|
|
|
// foreach ($containersPaths as $containersPath) { |
73
|
|
|
// |
74
|
|
|
// if (preg_match('/_SHIP/',$containersPath)){ |
75
|
|
|
// |
76
|
|
|
// // append the Ship directories to the Containers directory, since it will contain containers |
77
|
|
|
// $shipContainers = File::directories($containersPath); |
78
|
|
|
// $containersPaths = array_merge($containersPaths, $shipContainers); |
79
|
|
|
// } |
80
|
|
|
// } |
81
|
|
|
|
82
|
|
|
return $containersPaths; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function getShipPath() |
89
|
|
|
{ |
90
|
|
|
return File::directories(app_path('Ship')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* build and return an object of a class from its file path |
95
|
|
|
* |
96
|
|
|
* @param $filePathName |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function getClassObjectFromFile($filePathName) |
101
|
|
|
{ |
102
|
|
|
$classString = $this->getClassFullNameFromFile($filePathName); |
103
|
|
|
|
104
|
|
|
$object = new $classString; |
105
|
|
|
|
106
|
|
|
return $object; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* get the full name (name \ namespace) of a class from its file path |
111
|
|
|
* result example: (string) "I\Am\The\Namespace\Of\This\Class" |
112
|
|
|
* |
113
|
|
|
* @param $filePathName |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getClassFullNameFromFile($filePathName) |
118
|
|
|
{ |
119
|
|
|
return $this->getClassNamespaceFromFile($filePathName) . '\\' . $this->getClassNameFromFile($filePathName); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* get the class namespace form file path using token |
124
|
|
|
* |
125
|
|
|
* @param $filePathName |
126
|
|
|
* |
127
|
|
|
* @return null|string |
128
|
|
|
*/ |
129
|
|
|
protected function getClassNamespaceFromFile($filePathName) |
130
|
|
|
{ |
131
|
|
|
$src = file_get_contents($filePathName); |
132
|
|
|
|
133
|
|
|
$tokens = token_get_all($src); |
134
|
|
|
$count = count($tokens); |
135
|
|
|
$i = 0; |
136
|
|
|
$namespace = ''; |
137
|
|
|
$namespace_ok = false; |
138
|
|
|
while ($i < $count) { |
139
|
|
|
$token = $tokens[$i]; |
140
|
|
|
if (is_array($token) && $token[0] === T_NAMESPACE) { |
141
|
|
|
// Found namespace declaration |
142
|
|
|
while (++$i < $count) { |
143
|
|
|
if ($tokens[$i] === ';') { |
144
|
|
|
$namespace_ok = true; |
145
|
|
|
$namespace = trim($namespace); |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
$namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]; |
149
|
|
|
} |
150
|
|
|
break; |
151
|
|
|
} |
152
|
|
|
$i++; |
153
|
|
|
} |
154
|
|
|
if (!$namespace_ok) { |
155
|
|
|
return null; |
156
|
|
|
} else { |
157
|
|
|
return $namespace; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* get the class name form file path using token |
163
|
|
|
* |
164
|
|
|
* @param $filePathName |
165
|
|
|
* |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
protected function getClassNameFromFile($filePathName) |
169
|
|
|
{ |
170
|
|
|
$php_code = file_get_contents($filePathName); |
171
|
|
|
|
172
|
|
|
$classes = array(); |
173
|
|
|
$tokens = token_get_all($php_code); |
174
|
|
|
$count = count($tokens); |
175
|
|
|
for ($i = 2; $i < $count; $i++) { |
176
|
|
|
if ($tokens[$i - 2][0] == T_CLASS |
177
|
|
|
&& $tokens[$i - 1][0] == T_WHITESPACE |
178
|
|
|
&& $tokens[$i][0] == T_STRING |
179
|
|
|
) { |
180
|
|
|
|
181
|
|
|
$class_name = $tokens[$i][1]; |
182
|
|
|
$classes[] = $class_name; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $classes[0]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.