1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PPI Framework. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2011-2013 Paul Dragoonis <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* |
8
|
|
|
* @link http://www.ppi.io |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PPI\Framework\Module; |
12
|
|
|
|
13
|
|
|
use Zend\ModuleManager\ModuleManager as BaseModuleManager; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ModuleManager. |
17
|
|
|
* |
18
|
|
|
* @author Paul Dragoonis <[email protected]> |
19
|
|
|
* @author Vítor Brandão <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ModuleManager extends BaseModuleManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns an array of paths to modules. |
25
|
|
|
* |
26
|
|
|
* @return array An array of paths to each loaded module |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function getModulesPath() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$paths = array(); |
31
|
|
|
foreach ($this->getLoadedModules(true) as $module) { |
32
|
|
|
$paths[$module->getName()] = $module->getPath(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $paths; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the file path for a given resource. |
40
|
|
|
* |
41
|
|
|
* A Resource can be a file or a directory. |
42
|
|
|
* |
43
|
|
|
* The resource name must follow the following pattern: |
44
|
|
|
* |
45
|
|
|
* @<ModuleName>/path/to/a/file.something |
46
|
|
|
* |
47
|
|
|
* where ModuleName is the name of the module |
48
|
|
|
* and the remaining part is the relative path in the module. |
49
|
|
|
* |
50
|
|
|
* If $dir is passed, and the first segment of the path is "Resources", |
51
|
|
|
* this method will look for a file named: |
52
|
|
|
* |
53
|
|
|
* $dir/<ModuleName>/path/without/Resources |
54
|
|
|
* |
55
|
|
|
* before looking in the module resource folder. |
56
|
|
|
* |
57
|
|
|
* @param string $name A resource name to locate |
58
|
|
|
* @param string $dir A directory where to look for the resource first |
59
|
|
|
* @param Boolean $first Whether to return the first path or paths for all matching modules |
60
|
|
|
* |
61
|
|
|
* @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
62
|
|
|
* @throws \RuntimeException if the name contains invalid/unsafe |
63
|
|
|
* @throws \RuntimeException if a custom resource is hidden by a resource in a derived module |
64
|
|
|
* |
65
|
|
|
* @return string|array The absolute path of the resource or an array if $first is false |
66
|
|
|
* |
67
|
|
|
* @api |
68
|
|
|
*/ |
69
|
|
|
public function locateResource($name, $dir = null, $first = true) |
70
|
|
|
{ |
71
|
|
|
if ('@' !== $name[0]) { |
72
|
|
|
throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (false !== strpos($name, '..')) { |
76
|
|
|
throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); |
77
|
|
|
} |
78
|
|
|
$moduleName = substr($name, 1); |
79
|
|
|
$path = ''; |
80
|
|
|
if (false !== strpos($moduleName, '/')) { |
81
|
|
|
list($moduleName, $path) = explode('/', $moduleName, 2); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
$isResource = 0 === strpos($path, 'Resources') && null !== $dir; |
86
|
|
|
$overridePath = substr($path, 9); |
87
|
|
|
$resourceModule = null; |
88
|
|
|
$modules = array($this->getModule($moduleName)); |
89
|
|
|
$files = array(); |
90
|
|
|
|
91
|
|
|
foreach ($modules as $module) { |
92
|
|
|
if ($isResource && file_exists($file = $dir . '/' . $module->getName() . $overridePath)) { |
93
|
|
|
if (null !== $resourceModule) { |
94
|
|
|
throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived module. Create a "%s" file to override the module resource.', |
95
|
|
|
$file, |
96
|
|
|
$resourceModule, |
97
|
|
|
$dir . '/' . $modules[0]->getName() . $overridePath |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($first) { |
102
|
|
|
return $file; |
103
|
|
|
} |
104
|
|
|
$files[] = $file; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (file_exists($file = $this->getResourcesPath($module) . '/' . $path)) { |
108
|
|
|
if ($first && !$isResource) { |
109
|
|
|
return $file; |
110
|
|
|
} |
111
|
|
|
$files[] = $file; |
112
|
|
|
$resourceModule = $module->getName(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (count($files) > 0) { |
117
|
|
|
return $first && $isResource ? $files[0] : $files; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getResourcesPath($module) |
124
|
|
|
{ |
125
|
|
|
if(is_callable(array($module, 'getResourcesPath'))) { |
126
|
|
|
$resourcesPath = $module->getResourcesPath(); |
127
|
|
|
} else { |
128
|
|
|
$resourcesPath = $module->getPath(); |
129
|
|
|
} |
130
|
|
|
return $resourcesPath; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.