1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\CMS\Theme; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
use TheCodingMachine\CMS\Block\BlockInterface; |
9
|
|
|
use TheCodingMachine\CMS\Block\BlockRendererInterface; |
10
|
|
|
use TheCodingMachine\CMS\RenderableInterface; |
11
|
|
|
|
12
|
|
|
class AggregateThemeFactory implements ThemeFactoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ThemeFactoryInterface[] |
16
|
|
|
*/ |
17
|
|
|
private $themeFactories; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param ThemeFactoryInterface[] $themeFactories |
21
|
|
|
*/ |
22
|
|
|
public function __construct(array $themeFactories = []) |
23
|
|
|
{ |
24
|
|
|
$this->themeFactories = $themeFactories; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ThemeFactoryInterface[] $themeFactories |
29
|
|
|
*/ |
30
|
|
|
public function setThemeFactories(array $themeFactories): void |
31
|
|
|
{ |
32
|
|
|
$this->themeFactories = $themeFactories; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ThemeFactoryInterface $themeFactory |
37
|
|
|
*/ |
38
|
|
|
public function addThemeFactory(ThemeFactoryInterface $themeFactory): void |
39
|
|
|
{ |
40
|
|
|
$this->themeFactories[] = $themeFactory; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates a theme object based on the descriptor object passed in parameter. |
46
|
|
|
* Throws an exception if the factory cannot handle this type of descriptor. |
47
|
|
|
* |
48
|
|
|
* @throws \TheCodingMachine\CMS\Theme\CannotHandleThemeDescriptorExceptionInterface |
49
|
|
|
*/ |
50
|
|
|
public function createTheme(ThemeDescriptorInterface $descriptor): RenderableInterface |
51
|
|
|
{ |
52
|
|
|
foreach ($this->themeFactories as $themeFactory) { |
53
|
|
|
if ($themeFactory->canCreateTheme($descriptor)) { |
54
|
|
|
return $themeFactory->createTheme($descriptor); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
throw CannotHandleThemeDescriptorException::cannotHandleDescriptorClass(get_class($descriptor)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns true if this factory can handle the descriptor passed in parameter. False otherwise. |
62
|
|
|
*/ |
63
|
|
|
public function canCreateTheme(ThemeDescriptorInterface $descriptor): bool |
64
|
|
|
{ |
65
|
|
|
foreach ($this->themeFactories as $themeFactory) { |
66
|
|
|
if ($themeFactory->canCreateTheme($descriptor)) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|