|
1
|
|
|
<?php namespace Tarsana\Command\Template; |
|
2
|
|
|
|
|
3
|
|
|
use Tarsana\Command\Interfaces\Template\TemplateInterface; |
|
4
|
|
|
use Tarsana\Command\Interfaces\Template\TemplateLoaderInterface; |
|
5
|
|
|
use Tarsana\IO\Filesystem; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This is the all-in-one template loader, it can decide |
|
10
|
|
|
* which template loader implementation to call based on |
|
11
|
|
|
* the file extension. |
|
12
|
|
|
*/ |
|
13
|
|
|
class TemplateLoader implements TemplateLoaderInterface { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Template loader providers classes. |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $providers = [ |
|
21
|
|
|
'twig' => 'Tarsana\Command\Template\Twig\TwigLoader' |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Filesystem of the templates. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Tarsana\IO\Interfaces\Filesystem |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $fs; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Array of template loaders. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $loaders; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(string $templatesPath, string $cachePath = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->init($templatesPath, $cachePath); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Initialize the loader. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $templatesPath |
|
47
|
|
|
* @param string $cachePath |
|
48
|
|
|
* @return self |
|
49
|
|
|
*/ |
|
50
|
|
|
public function init(string $templatesPath, string $cachePath = null) : TemplateLoaderInterface |
|
51
|
|
|
{ |
|
52
|
|
|
|
|
53
|
|
|
$this->fs = new Filesystem($templatesPath); |
|
|
|
|
|
|
54
|
|
|
$this->loaders = []; |
|
55
|
|
|
|
|
56
|
|
|
foreach (self::$providers as $ext => $provider) { |
|
57
|
|
|
$this->loaders[$ext] = new $provider(); |
|
58
|
|
|
$this->loaders[$ext]->init($templatesPath, $cachePath); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Load a template by name. The name is the relative |
|
66
|
|
|
* path of the template file from the templates folder |
|
67
|
|
|
* The name is given without extension; Exceptions are |
|
68
|
|
|
* thrown if no file with supported extension is found |
|
69
|
|
|
* or if many exists. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* @return Tarsana\Command\Interfaces\TemplateInterface |
|
73
|
|
|
* @throws Tarsana\Command\Exceptions\TemplateNotFound |
|
74
|
|
|
* @throws Tarsana\Command\Exceptions\TemplateNameConflict |
|
75
|
|
|
*/ |
|
76
|
|
|
public function load (string $name) : TemplateInterface |
|
77
|
|
|
{ |
|
78
|
|
|
$supportedExtensions = array_keys(self::$providers); |
|
79
|
|
|
|
|
80
|
|
|
$fsPathLength = strlen($this->fs->path()); |
|
81
|
|
|
|
|
82
|
|
|
$files = $this->fs |
|
83
|
|
|
->find("{$name}.*") |
|
84
|
|
|
->files() |
|
85
|
|
|
->asArray(); |
|
86
|
|
|
|
|
87
|
|
|
$found = []; |
|
88
|
|
|
foreach ($files as $file) { |
|
89
|
|
|
$ext = $file->extension(); |
|
90
|
|
|
if (!in_array($ext, $supportedExtensions)) |
|
91
|
|
|
continue; |
|
92
|
|
|
$found[] = [ |
|
93
|
|
|
'name' => substr($file->path(), $fsPathLength), |
|
94
|
|
|
'extension' => $ext |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (count($found) == 0) { |
|
99
|
|
|
throw new \InvalidArgumentException("Unable to find template with name '{$name}' on '{$this->fs->path()}'"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (count($found) > 1) { |
|
103
|
|
|
throw new \InvalidArgumentException("Mutiple templates found for the name '{$name}' on '{$this->fs->path()}'"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->loaders[$found[0]['extension']]->load($found[0]['name']); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..