1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Phauthentic\Presentation\Renderer; |
5
|
|
|
|
6
|
|
|
use Phauthentic\Presentation\Renderer\Exception\MissingTemplateFolderException; |
7
|
|
|
use ArrayIterator; |
8
|
|
|
use Phauthentic\Presentation\Renderer\Exception\MissingTemplateException; |
9
|
|
|
use Phauthentic\Presentation\View\ViewInterface; |
10
|
|
|
use IteratorAggregate; |
11
|
|
|
use Traversable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* AbstractBaseRenderer |
15
|
|
|
*/ |
16
|
|
|
class TemplateFolderCollection implements IteratorAggregate |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Folders |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $folders = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param array $folders Folders |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $folders) |
31
|
|
|
{ |
32
|
|
|
foreach ($folders as $folder) { |
33
|
|
|
$this->add($folder); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Checks that the folder really exists |
39
|
|
|
* |
40
|
|
|
* @param string $folder Folder |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
protected function checkFolder(string $folder): void |
44
|
|
|
{ |
45
|
|
|
if (!is_dir($folder)) { |
46
|
|
|
throw new MissingTemplateFolderException(sprintf( |
47
|
|
|
'The folder %s does not exist or is not a folder', |
48
|
|
|
$folder |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Adds a folder |
55
|
|
|
* |
56
|
|
|
* @param string $folder Folder |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function add(string $folder) |
60
|
|
|
{ |
61
|
|
|
$folder = $this->sanitizePath($folder); |
62
|
|
|
$this->checkFolder($folder); |
63
|
|
|
|
64
|
|
|
if (!in_array($folder, $this->folders)) { |
65
|
|
|
$this->folders[] = $folder; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sanitizes the template path |
71
|
|
|
* |
72
|
|
|
* @param string $path |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function sanitizePath(string $path): string |
76
|
|
|
{ |
77
|
|
|
return Utility::sanitizePath($path); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retrieve an external iterator |
82
|
|
|
* |
83
|
|
|
* @link https://php.net/manual/en/iteratoraggregate.getiterator.php |
84
|
|
|
* @return Traversable An instance of an object implementing <b>Iterator</b> or |
85
|
|
|
* <b>Traversable</b> |
86
|
|
|
* @since 5.0.0 |
87
|
|
|
*/ |
88
|
|
|
public function getIterator() |
89
|
|
|
{ |
90
|
|
|
return new ArrayIterator($this->folders); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|