|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <[email protected]>. All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are |
|
9
|
|
|
* permitted provided that the following conditions are met: |
|
10
|
|
|
* |
|
11
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of |
|
12
|
|
|
* conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
15
|
|
|
* of conditions and the following disclaimer in the documentation and/or other materials |
|
16
|
|
|
* provided with the distribution. |
|
17
|
|
|
* |
|
18
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
19
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
21
|
|
|
* DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY |
|
22
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
23
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
24
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
25
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
26
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
27
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
namespace Cervo; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Modules manager for Cervo. |
|
37
|
|
|
* |
|
38
|
|
|
* @author Marc André Audet <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
final class ModulesManager |
|
41
|
|
|
{ |
|
42
|
|
|
private $vendors = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add a new path and extract the modules from it |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $path The path to the Vendors directory |
|
48
|
|
|
* |
|
49
|
|
|
* @return ModulesManager |
|
50
|
|
|
*/ |
|
51
|
|
|
public function addPath(string $path) : self |
|
52
|
|
|
{ |
|
53
|
|
|
$path = realpath($path); |
|
54
|
|
|
$path_len = strlen($path); |
|
55
|
|
|
|
|
56
|
|
|
foreach (glob($path. \DIRECTORY_SEPARATOR . '*', \GLOB_NOSORT | \GLOB_NOESCAPE) as $file) { |
|
57
|
|
|
|
|
58
|
|
|
if (is_dir($file)) { |
|
59
|
|
|
$this->addVendor(substr($file, $path_len + 1), $file); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add a new path and extract the modules from it, using a default Vendor name. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $vendor_name The Vendor name |
|
71
|
|
|
* @param string $path The path to the Modules directory |
|
72
|
|
|
* |
|
73
|
|
|
* @return ModulesManager |
|
74
|
|
|
*/ |
|
75
|
|
|
public function addVendor(string $vendor_name, string $path) : self |
|
76
|
|
|
{ |
|
77
|
|
|
$path_len = strlen($path); |
|
78
|
|
|
|
|
79
|
|
|
foreach (glob($path. \DIRECTORY_SEPARATOR . '*', \GLOB_NOSORT | \GLOB_NOESCAPE) as $file) { |
|
80
|
|
|
|
|
81
|
|
|
if (is_dir($file)) { |
|
82
|
|
|
$this->addModule($vendor_name, substr($file, $path_len + 1), $file); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Add a new path and extract the module, using a default Vendor and Module name. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $vendor_name The Vendor name |
|
94
|
|
|
* @param string $module_name The Module name |
|
95
|
|
|
* @param string $path The path to the Module's directory |
|
96
|
|
|
* |
|
97
|
|
|
* @return ModulesManager |
|
98
|
|
|
*/ |
|
99
|
|
|
public function addModule(string $vendor_name, string $module_name, string $path) : self |
|
100
|
|
|
{ |
|
101
|
|
|
$this->vendors[$vendor_name][$module_name] = $path; |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get an array of all the modules listed under a Vendor. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $vendor_name The Vendor name |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getVendorModules(string $vendor_name) : array |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->vendors[$vendor_name]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the root path of a module. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $vendor_name The Vendor's name |
|
121
|
|
|
* @param string $module_name The Module's name |
|
122
|
|
|
* |
|
123
|
|
|
* @return null|string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getModulePath(string $vendor_name, string $module_name) : ?string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->vendors[$vendor_name][$module_name]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Return a generator of every modules. |
|
132
|
|
|
* Each iterations are formatted like this: |
|
133
|
|
|
* [$vendor_name, $module_name, $path] |
|
134
|
|
|
* |
|
135
|
|
|
* You should use a list() to extract the data. |
|
136
|
|
|
* |
|
137
|
|
|
* @return \Generator |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getAllModules() : \Generator |
|
140
|
|
|
{ |
|
141
|
|
|
foreach ($this->vendors as $vendor_name => $modules) { |
|
142
|
|
|
foreach ($modules as $module_name => $path) { |
|
143
|
|
|
yield [$vendor_name, $module_name, $path]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|