1 | <?php |
||
17 | class StemplerLoader implements LoaderInterface |
||
18 | { |
||
19 | const DEFAULT_NAMESPACE = 'default'; |
||
20 | const FILE_EXTENSION = 'php'; |
||
21 | |||
22 | /** |
||
23 | * Path chunks. |
||
24 | */ |
||
25 | const VIEW_FILENAME = 0; |
||
26 | const VIEW_NAMESPACE = 1; |
||
27 | const VIEW_NAME = 2; |
||
28 | |||
29 | /** |
||
30 | * Available view namespaces associated with their directories. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $namespaces = []; |
||
35 | |||
36 | /** |
||
37 | * @var FilesInterface |
||
38 | */ |
||
39 | protected $files = null; |
||
40 | |||
41 | /** |
||
42 | * @param array $namespaces |
||
43 | * @param FilesInterface $files |
||
44 | */ |
||
45 | public function __construct(array $namespaces, FilesInterface $files = null) |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function getSource(string $path): StemplerSource |
||
55 | { |
||
56 | return new StemplerSource( |
||
57 | $this->locateView($path)[self::VIEW_FILENAME] |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Locate view filename based on current loader settings. |
||
63 | * |
||
64 | * @param string $path |
||
65 | * |
||
66 | * @return array [namespace, name] |
||
67 | * |
||
68 | * @throws LoaderException |
||
69 | */ |
||
70 | protected function locateView(string $path): array |
||
90 | |||
91 | /** |
||
92 | * Fetch namespace and filename from view name or force default values. |
||
93 | * |
||
94 | * @param string $path |
||
95 | * |
||
96 | * @return array |
||
97 | * |
||
98 | * @throws LoaderException |
||
99 | */ |
||
100 | protected function parsePath(string $path): array |
||
117 | |||
118 | /** |
||
119 | * Make sure view filename is OK. Same as in twig. |
||
120 | * |
||
121 | * @param string $path |
||
122 | * |
||
123 | * @throws LoaderException |
||
124 | */ |
||
125 | protected function validatePath(string $path) |
||
149 | |||
150 | /** |
||
151 | * Resolve view name based on filename (depends on current extension settings). |
||
152 | * |
||
153 | * @param string $filename |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function fetchName(string $filename): string |
||
161 | } |