1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Templates System. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Component\TemplatesSystem\Gimme\Loader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ChainLoader is a loader that calls other loaders to load Meta objects. |
19
|
|
|
*/ |
20
|
|
|
class ChainLoader implements LoaderInterface |
21
|
|
|
{ |
22
|
|
|
protected $loaders = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adds a loader instance. |
26
|
|
|
* |
27
|
|
|
* @param LoaderInterface $loader A Loader instance |
28
|
|
|
*/ |
29
|
|
|
public function addLoader(LoaderInterface $loader) |
30
|
|
|
{ |
31
|
|
|
$this->loaders[] = $loader; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Loads a Meta class from given datasource. |
36
|
|
|
* |
37
|
|
|
* @param string $type object type |
38
|
|
|
* @param array|null $parameters parameters needed to load required object type |
39
|
|
|
* @param int $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION) |
40
|
|
|
* |
41
|
|
|
* @return Meta|bool false if meta cannot be loaded, a Meta instance otherwise |
42
|
|
|
*/ |
43
|
|
|
public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE) |
44
|
|
|
{ |
45
|
|
|
foreach ($this->loaders as $loader) { |
46
|
|
|
if ($loader->isSupported($type)) { |
47
|
|
|
if (false !== $meta = $loader->load($type, $parameters, $responseType)) { |
48
|
|
|
return $meta; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Checks if Loader supports provided type. |
58
|
|
|
* |
59
|
|
|
* @param string $type |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function isSupported($type) |
64
|
|
|
{ |
65
|
|
|
foreach ($this->loaders as $loader) { |
66
|
|
|
if ($loader->isSupported($type)) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|