1 | <?php |
||
2 | |||
3 | namespace Maestriam\Samurai\Foundation; |
||
4 | |||
5 | use Maestriam\Samurai\Entities\Theme; |
||
6 | use Maestriam\FileSystem\Support\FileSystem; |
||
0 ignored issues
–
show
|
|||
7 | use Maestriam\Samurai\Entities\Component; |
||
8 | use Maestriam\Samurai\Entities\Includer; |
||
9 | |||
10 | class DirectiveFinder |
||
11 | { |
||
12 | private string $pattern = '.blade'; |
||
13 | |||
14 | private DirectiveParser $parserInstance; |
||
15 | |||
16 | public function __construct(Theme $theme) |
||
17 | { |
||
18 | $this->init($theme); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Recupera TODAS as diretivas de um tema. |
||
23 | * |
||
24 | * @return array |
||
25 | */ |
||
26 | public function all() : array |
||
27 | { |
||
28 | $directives = []; |
||
29 | |||
30 | $files = $this->readFiles(); |
||
31 | |||
32 | foreach($files as $file) { |
||
33 | |||
34 | $file = FileSystem::folder($file)->sanitize(); |
||
35 | |||
36 | $directives[] = $this->parser()->parse($file)->toDirective(); |
||
37 | } |
||
38 | |||
39 | return $directives; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Retorna a instância de uma diretiva includer, de acordo com o tema e a sentença. |
||
44 | * |
||
45 | * @param string $sentence |
||
46 | * @return Includer |
||
47 | */ |
||
48 | public function include(string $sentence) : Includer |
||
49 | { |
||
50 | return new Includer($this->theme(), $sentence); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Retorna a instância de uma diretiva component, de acordo com o tema e a sentença. |
||
55 | * |
||
56 | * @param string $sentence |
||
57 | * @return Component |
||
58 | */ |
||
59 | public function component(string $sentence) : Component |
||
60 | { |
||
61 | return new Component($this->theme(), $sentence); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Retorna a instância do tema que será manipulado. |
||
66 | * |
||
67 | * @return Theme |
||
68 | */ |
||
69 | private function theme() : Theme |
||
70 | { |
||
71 | return $this->themeInstance; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Inicia os atributos necessários para instanciar a classe. |
||
76 | * |
||
77 | * @param Theme $theme |
||
78 | * @return DirectiveFinder |
||
79 | */ |
||
80 | private function init(Theme $theme) : DirectiveFinder |
||
81 | { |
||
82 | $this->themeInstance = $theme; |
||
0 ignored issues
–
show
|
|||
83 | $this->parserInstance = new DirectiveParser($theme); |
||
84 | |||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Retorna a instância para identificação de uma diretiva, |
||
90 | * através do caminho absoluto do arquivo da diretiva. |
||
91 | * |
||
92 | * @return DirectiveParser |
||
93 | */ |
||
94 | private function parser() : DirectiveParser |
||
95 | { |
||
96 | return $this->parserInstance; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Retorna a lista, com o caminho completo, de todas as diretivas |
||
101 | * inseridas no tema determinado. |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | private function readFiles() : array |
||
106 | { |
||
107 | $path = $this->theme()->paths()->source(); |
||
108 | |||
109 | return FileSystem::folder($path)->files($this->pattern); |
||
110 | } |
||
111 | } |
||
112 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: