1 | <?php |
||
17 | class ModuleReader |
||
18 | { |
||
19 | /** @var DependenciesReader */ |
||
20 | private $fileReader; |
||
21 | |||
22 | public function __construct(DependenciesReader $fileReader) |
||
26 | |||
27 | /** |
||
28 | * @param Module[] $modules |
||
29 | * |
||
30 | * @throws MalformedFile |
||
31 | * @throws ModuleNotFound |
||
32 | * |
||
33 | * @return DirectedGraph Graph describing dependencies between modules |
||
34 | */ |
||
35 | public function generateDependenciesGraph(array $modules) |
||
36 | { |
||
37 | /** @var Vertex[] $vertices */ |
||
38 | $vertices = []; |
||
39 | foreach ($modules as $module) { |
||
40 | $vertices[] = new Vertex($module); |
||
41 | } |
||
42 | |||
43 | foreach ($vertices as $vertex) { |
||
44 | $dependencies = $this->findModuleDependencies($vertex->getValue()->getPath()); |
||
45 | foreach ($dependencies as $dependency) { |
||
46 | foreach ($vertices as $neighbour) { |
||
47 | if ($vertex !== $neighbour && \preg_match($neighbour->getValue()->getPattern(), $dependency)) { |
||
48 | $vertex->addNeighbour($neighbour); |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 | |||
54 | return new DirectedGraph($vertices); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $modulePath Path to module, either file or directory |
||
59 | * |
||
60 | * @throws MalformedFile |
||
61 | * @throws ModuleNotFound |
||
62 | * |
||
63 | * @return string[] List of module's dependencies |
||
64 | */ |
||
65 | public function findModuleDependencies($modulePath) |
||
66 | { |
||
67 | $dependencies = []; |
||
68 | $files = $this->findPHPFiles($modulePath); |
||
69 | foreach ($files as $file) { |
||
70 | $dependencies = \array_merge($dependencies, $this->fileReader->findFileDependencies($file)); |
||
71 | } |
||
72 | |||
73 | // remove duplicates |
||
74 | $dependencies = \array_unique($dependencies); |
||
75 | |||
76 | // reindex array |
||
77 | return \array_values($dependencies); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $modulePath |
||
82 | * |
||
83 | * @throws ModuleNotFound |
||
84 | * |
||
85 | * @return string[] Paths to PHP files in the module |
||
86 | */ |
||
87 | private function findPHPFiles($modulePath) |
||
107 | } |
||
108 |