1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maestriam\Samurai\Foundation; |
4
|
|
|
|
5
|
|
|
use Maestriam\Samurai\Entities\Theme; |
6
|
|
|
use Maestriam\FileSystem\Support\FileSystem; |
|
|
|
|
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
|
|
|
$directives[] = $this->parser()->parse($file)->toDirective(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $directives; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retorna a instância de uma diretiva includer, de acordo com o tema e a sentença. |
41
|
|
|
* |
42
|
|
|
* @param string $sentence |
43
|
|
|
* @return Includer |
44
|
|
|
*/ |
45
|
|
|
public function include(string $sentence) : Includer |
46
|
|
|
{ |
47
|
|
|
return new Includer($this->theme(), $sentence); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Retorna a instância de uma diretiva component, de acordo com o tema e a sentença. |
52
|
|
|
* |
53
|
|
|
* @param string $sentence |
54
|
|
|
* @return Component |
55
|
|
|
*/ |
56
|
|
|
public function component(string $sentence) : Component |
57
|
|
|
{ |
58
|
|
|
return new Component($this->theme(), $sentence); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retorna a instância do tema que será manipulado. |
63
|
|
|
* |
64
|
|
|
* @return Theme |
65
|
|
|
*/ |
66
|
|
|
private function theme() : Theme |
67
|
|
|
{ |
68
|
|
|
return $this->themeInstance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Inicia os atributos necessários para instanciar a classe. |
73
|
|
|
* |
74
|
|
|
* @param Theme $theme |
75
|
|
|
* @return DirectiveFinder |
76
|
|
|
*/ |
77
|
|
|
private function init(Theme $theme) : DirectiveFinder |
78
|
|
|
{ |
79
|
|
|
$this->themeInstance = $theme; |
|
|
|
|
80
|
|
|
$this->parserInstance = new DirectiveParser($theme); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retorna a instância para identificação de uma diretiva, |
87
|
|
|
* através do caminho absoluto do arquivo da diretiva. |
88
|
|
|
* |
89
|
|
|
* @return DirectiveParser |
90
|
|
|
*/ |
91
|
|
|
private function parser() : DirectiveParser |
92
|
|
|
{ |
93
|
|
|
return $this->parserInstance; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Retorna a lista, com o caminho completo, de todas as diretivas |
98
|
|
|
* inseridas no tema determinado. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function readFiles() : array |
103
|
|
|
{ |
104
|
|
|
$path = $this->theme()->paths()->source(); |
105
|
|
|
|
106
|
|
|
return FileSystem::folder($path)->files($this->pattern); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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: