1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cervo package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2010-2019 Nevraxe inc. & Marc André Audet <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* @package Cervo |
9
|
|
|
* @author Marc André Audet <[email protected]> |
10
|
|
|
* @copyright 2010 - 2019 Nevraxe inc. & Marc André Audet |
11
|
|
|
* @license See LICENSE.md MIT |
12
|
|
|
* @link https://github.com/Nevraxe/Cervo |
13
|
|
|
* @since 5.0.0 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
namespace Cervo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Modules manager for Cervo. |
22
|
|
|
* |
23
|
|
|
* @author Marc André Audet <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class ModulesManager |
26
|
|
|
{ |
27
|
|
|
private $vendors = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Add a new path and extract the modules from it |
31
|
|
|
* |
32
|
|
|
* @param string $path The path to the Vendors directory |
33
|
|
|
* |
34
|
|
|
* @return ModulesManager |
35
|
|
|
*/ |
36
|
|
|
public function addPath(string $path): self |
37
|
|
|
{ |
38
|
|
|
$path = realpath($path); |
39
|
|
|
$pathLen = strlen($path); |
40
|
|
|
|
41
|
|
|
foreach (glob($path . \DIRECTORY_SEPARATOR . '*', \GLOB_NOSORT | \GLOB_NOESCAPE) as $file) { |
42
|
|
|
|
43
|
|
|
if (is_dir($file)) { |
44
|
|
|
$this->addVendor(substr($file, $pathLen + 1), $file); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Add a new path and extract the modules from it, using a default Vendor name. |
54
|
|
|
* |
55
|
|
|
* @param string $vendorName The Vendor name |
56
|
|
|
* @param string $path The path to the Modules directory |
57
|
|
|
* |
58
|
|
|
* @return ModulesManager |
59
|
|
|
*/ |
60
|
|
|
public function addVendor(string $vendorName, string $path): self |
61
|
|
|
{ |
62
|
|
|
$pathLen = strlen($path); |
63
|
|
|
|
64
|
|
|
foreach (glob($path . \DIRECTORY_SEPARATOR . '*', \GLOB_NOSORT | \GLOB_NOESCAPE) as $file) { |
65
|
|
|
|
66
|
|
|
if (is_dir($file)) { |
67
|
|
|
$this->addModule($vendorName, substr($file, $pathLen + 1), $file); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add a new path and extract the module, using a default Vendor and Module name. |
77
|
|
|
* |
78
|
|
|
* @param string $vendorName The Vendor name |
79
|
|
|
* @param string $moduleName The Module name |
80
|
|
|
* @param string $path The path to the Module's directory |
81
|
|
|
* |
82
|
|
|
* @return ModulesManager |
83
|
|
|
*/ |
84
|
|
|
public function addModule(string $vendorName, string $moduleName, string $path): self |
85
|
|
|
{ |
86
|
|
|
$this->vendors[$vendorName][$moduleName] = $path; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get an array of all the modules listed under a Vendor. |
92
|
|
|
* |
93
|
|
|
* @param string $vendorName The Vendor name |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getVendorModules(string $vendorName): array |
98
|
|
|
{ |
99
|
|
|
return $this->vendors[$vendorName]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the root path of a module. |
104
|
|
|
* |
105
|
|
|
* @param string $vendorName The Vendor's name |
106
|
|
|
* @param string $moduleName The Module's name |
107
|
|
|
* |
108
|
|
|
* @return null|string |
109
|
|
|
*/ |
110
|
|
|
public function getModulePath(string $vendorName, string $moduleName): ?string |
111
|
|
|
{ |
112
|
|
|
return $this->vendors[$vendorName][$moduleName]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return a generator of every modules. |
117
|
|
|
* Each iterations are formatted like this: |
118
|
|
|
* [$vendor_name, $module_name, $path] |
119
|
|
|
* |
120
|
|
|
* You should use a list() to extract the data. |
121
|
|
|
* |
122
|
|
|
* @return \Generator |
123
|
|
|
*/ |
124
|
|
|
public function getAllModules(): \Generator |
125
|
|
|
{ |
126
|
|
|
foreach ($this->vendors as $vendorName => $modules) { |
127
|
|
|
foreach ($modules as $moduleName => $path) { |
128
|
|
|
yield [$vendorName, $moduleName, $path]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|