1 | <?php |
||
21 | class Factory |
||
22 | { |
||
23 | const TEMPLATE_DEFINITION_FILENAME = 'template.xml'; |
||
24 | |||
25 | /** @var PathResolver */ |
||
26 | private $pathResolver; |
||
27 | |||
28 | /** |
||
29 | * Constructs a new template factory with its dependencies. |
||
30 | */ |
||
31 | 1 | public function __construct(PathResolver $pathResolver) |
|
32 | { |
||
33 | 1 | $this->pathResolver = $pathResolver; |
|
34 | 1 | } |
|
35 | |||
36 | /** |
||
37 | * Attempts to find, construct and return a template object with the given template name or (relative/absolute) |
||
38 | * path. |
||
39 | */ |
||
40 | 1 | public function get(string $nameOrPath): Template |
|
41 | { |
||
42 | 1 | return $this->createTemplateFromXml( |
|
43 | 1 | $this->fetchTemplateXmlFromPath( |
|
44 | 1 | $this->pathResolver->resolve($nameOrPath) |
|
45 | ) |
||
46 | ); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Returns a list of all template names. |
||
51 | * |
||
52 | * @return string[] |
||
53 | */ |
||
54 | 1 | public function getAllNames(): array |
|
55 | { |
||
56 | /** @var \RecursiveDirectoryIterator $files */ |
||
57 | 1 | $files = new \DirectoryIterator($this->getTemplatePath()); |
|
58 | |||
59 | 1 | $template_names = []; |
|
60 | 1 | while ($files->valid()) { |
|
61 | 1 | $name = $files->getBasename(); |
|
62 | |||
63 | // skip abstract files |
||
64 | 1 | if (!$files->isDir() || in_array($name, ['.', '..'], true)) { |
|
65 | 1 | $files->next(); |
|
66 | 1 | continue; |
|
67 | } |
||
68 | |||
69 | 1 | $template_names[] = $name; |
|
70 | 1 | $files->next(); |
|
71 | } |
||
72 | |||
73 | 1 | return $template_names; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Returns the path where all templates are stored. |
||
78 | */ |
||
79 | 1 | public function getTemplatePath(): string |
|
83 | |||
84 | /** |
||
85 | * Loads the template definition file from the given path and returns it's contents. |
||
86 | */ |
||
87 | 1 | protected function fetchTemplateXmlFromPath(string $path): string |
|
91 | |||
92 | /** |
||
93 | * Creates and returns a template object based on the provided template definition. |
||
94 | */ |
||
95 | 1 | protected function createTemplateFromXml(string $xml): Template |
|
132 | } |
||
133 |