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