1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\UI\Storage; |
19
|
|
|
|
20
|
|
|
use Biurad\UI\Interfaces\StorageInterface; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* FilesystemLoader is a loader that read templates from the filesystem. |
25
|
|
|
* |
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class FilesystemStorage implements StorageInterface |
29
|
|
|
{ |
30
|
|
|
/** @var LoggerInterface|null */ |
31
|
|
|
protected $logger; |
32
|
|
|
|
33
|
|
|
/** @var array<int,string> */ |
34
|
|
|
protected $paths; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string|string[] $templatePaths An array of paths to look for templates |
38
|
|
|
*/ |
39
|
6 |
|
public function __construct($templatePaths = [], LoggerInterface $logger = null) |
40
|
|
|
{ |
41
|
6 |
|
$this->logger = $logger; |
42
|
6 |
|
$this->paths = \is_array($templatePaths) ? $templatePaths : [$templatePaths]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function addLocation(string $location): void |
49
|
|
|
{ |
50
|
|
|
$this->paths[] = $location; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
5 |
|
public function load(string $template, array $namespaces): ?string |
57
|
|
|
{ |
58
|
5 |
|
$fileFailures = []; |
59
|
|
|
|
60
|
5 |
|
if (\file_exists($template)) { |
61
|
2 |
|
if (null !== $this->logger) { |
62
|
|
|
$this->logger->debug('Loaded template file.', ['file' => $template]); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return $template; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
foreach (($namespaces ?: $this->paths) as $path) { |
69
|
5 |
|
if (\file_exists($found = $path . '/' . $template)) { |
70
|
5 |
|
if (null !== $this->logger) { |
71
|
1 |
|
$this->logger->debug('Loaded template file.', ['file' => $template]); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
return $found; |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
if (null !== $this->logger) { |
78
|
1 |
|
$fileFailures[] = $template; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// only log failures if no template could be loaded at all |
83
|
5 |
|
foreach ($fileFailures as $file) { |
84
|
1 |
|
if (null !== $this->logger) { |
85
|
1 |
|
$this->logger->debug('Failed loading template file.', ['file' => $file]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
return null; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|