|
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
|
|
|
* ChainStorage is a loader that calls other storage loaders to load templates. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class ChainStorage implements StorageInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var array<int,StorageInterface> */ |
|
31
|
|
|
protected $loaders = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array<int,StorageInterface> $storages An array of storage instances |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function __construct(array $storages = []) |
|
37
|
|
|
{ |
|
38
|
3 |
|
$this->loaders = $storages; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function addLocation(string $location, string $loaderClassName = null): void |
|
45
|
|
|
{ |
|
46
|
|
|
if (null !== $loaderClassName) { |
|
47
|
|
|
foreach ($this->loaders as $loader) { |
|
48
|
|
|
if ($loaderClassName === \get_class($loader)) { |
|
49
|
|
|
$loader->addLocation($location); |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
throw new LoaderException(\sprintf('Failed to use [%s] for views loading', $location)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Adds a storage loader instance. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function addStorage(StorageInterface $storage): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->loaders[] = $storage; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function load(string $template, array $namespaces): ?string |
|
70
|
|
|
{ |
|
71
|
3 |
|
foreach ($this->loaders as $loader) { |
|
72
|
3 |
|
if (null !== $storage = $loader->load($template, $namespaces)) { |
|
73
|
3 |
|
return $storage; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|