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\Exceptions\LoaderException; |
21
|
|
|
use Biurad\UI\Interfaces\StorageInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Load templates from an array. |
25
|
|
|
* |
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ArrayStorage implements StorageInterface |
29
|
|
|
{ |
30
|
|
|
/** @var string[] */ |
31
|
|
|
protected $templates = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string[] $templates An array of templates |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct(array $templates = []) |
37
|
|
|
{ |
38
|
5 |
|
$this->templates = $templates; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
1 |
|
public function addLocation(string $location): void |
45
|
|
|
{ |
46
|
1 |
|
if (!$templates = \json_decode($location)) { |
47
|
1 |
|
throw new LoaderException(\sprintf('Cannot use [%s] for templates loading.', $location)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->templates = \array_merge($this->templates, $templates); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
4 |
|
public function load(string $template, array $namespaces): ?string |
57
|
|
|
{ |
58
|
4 |
|
$loadedTemplate = $namespaces[$template] ?? $this->templates[$template] ?? null; |
59
|
|
|
|
60
|
4 |
|
if ($loadedTemplate instanceof \Stringable || \is_string($loadedTemplate)) { |
61
|
4 |
|
return (string) $loadedTemplate; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
if (null !== $loadedTemplate) { |
65
|
1 |
|
throw new LoaderException(\sprintf('Failed to load "%s" as it\'s source isn\'t stringable.', $template)); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return $loadedTemplate; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|