1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by: Jens |
4
|
|
|
* Date: 12-10-2017 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CloudControl\Cms\cc\application; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
use CloudControl\Cms\cc\Request; |
11
|
|
|
use CloudControl\Cms\components\Component; |
12
|
|
|
use CloudControl\Cms\storage\Storage; |
13
|
|
|
|
14
|
|
|
class ApplicationRunner |
15
|
|
|
{ |
16
|
|
|
private $storage; |
17
|
|
|
private $request; |
18
|
|
|
|
19
|
|
|
public function __construct(Storage $storage, Request $request) |
20
|
|
|
{ |
21
|
|
|
$this->storage = $storage; |
22
|
|
|
$this->request = $request; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Loop through all application components and run them |
27
|
|
|
* |
28
|
|
|
* @param $applicationComponents |
29
|
|
|
*/ |
30
|
|
|
public function runApplicationComponents($applicationComponents) |
31
|
|
|
{ |
32
|
|
|
foreach ($applicationComponents as $key => $applicationComponent) { |
33
|
|
|
$class = $applicationComponent->component; |
34
|
|
|
$parameters = $applicationComponent->parameters; |
35
|
|
|
$applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null); |
36
|
|
|
$applicationComponents[$key]->{'object'}->run($this->storage); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $class |
42
|
|
|
* @param string $template |
43
|
|
|
* @param array $parameters |
44
|
|
|
* @param \stdClass|null $matchedSitemapItem |
45
|
|
|
* |
46
|
|
|
* @return Component |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
|
|
private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem) |
50
|
|
|
{ |
51
|
|
|
$libraryComponentName = '\\CloudControl\Cms\\components\\' . $class; |
52
|
|
|
$userComponentName = '\\components\\' . $class; |
53
|
|
|
|
54
|
|
|
if (class_exists($libraryComponentName)) { |
55
|
|
|
$component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
56
|
|
|
} elseif (class_exists($userComponentName)) { |
57
|
|
|
$component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
58
|
|
|
} else { |
59
|
|
|
throw new \Exception('Could not load component ' . $class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!$component instanceof Component) { |
63
|
|
|
throw new \Exception('Component not of type Component. Must inherit \CloudControl\Cms\components\Component'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $component; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Loop through all (matched) sitemap components and run them |
71
|
|
|
* |
72
|
|
|
* @param $matchedSitemapItems |
73
|
|
|
*/ |
74
|
|
|
public function runSitemapComponents($matchedSitemapItems) |
75
|
|
|
{ |
76
|
|
|
foreach ($matchedSitemapItems as $key => $sitemapItem) { |
77
|
|
|
$class = $sitemapItem->component; |
78
|
|
|
$template = $sitemapItem->template; |
79
|
|
|
$parameters = $sitemapItem->parameters; |
80
|
|
|
|
81
|
|
|
$matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem); |
82
|
|
|
|
83
|
|
|
$matchedSitemapItems[$key]->object->run($this->storage); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |