1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CloudControl\Cms\components { |
4
|
|
|
|
5
|
|
|
use CloudControl\Cms\cc\Application; |
6
|
|
|
use CloudControl\Cms\cc\Request; |
7
|
|
|
use CloudControl\Cms\storage\Storage; |
8
|
|
|
|
9
|
|
|
class BaseComponent implements Component |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $template; |
15
|
|
|
/** |
16
|
|
|
* @var \CloudControl\Cms\cc\Request |
17
|
|
|
*/ |
18
|
|
|
protected $request; |
19
|
|
|
/** |
20
|
|
|
* @var Storage |
21
|
|
|
*/ |
22
|
|
|
protected $storage; |
23
|
|
|
/** |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
protected $renderedContent; |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $parameters = array(); |
31
|
|
|
/*** |
32
|
|
|
* @var \stdClass |
33
|
|
|
*/ |
34
|
|
|
protected $matchedSitemapItem; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* BaseComponent constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $template |
40
|
|
|
* @param Request $request |
41
|
|
|
* @param array $parameters |
42
|
|
|
* @param $matchedSitemapItem |
43
|
|
|
*/ |
44
|
|
|
public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem) |
45
|
|
|
{ |
46
|
|
|
$this->template = $template; |
47
|
|
|
$this->request = $request; |
48
|
|
|
$this->parameters = (array)$parameters; |
49
|
|
|
$this->matchedSitemapItem = $matchedSitemapItem; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Hook for implementation in derived classes |
54
|
|
|
* |
55
|
|
|
* @param Storage $storage |
56
|
|
|
*/ |
57
|
|
|
public function run(Storage $storage) |
58
|
|
|
{ |
59
|
|
|
$this->storage = $storage; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Renders the template |
64
|
|
|
* |
65
|
|
|
* @param null|Application $application |
66
|
|
|
* |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function render($application = null) |
70
|
|
|
{ |
71
|
|
|
$this->renderedContent = $this->renderTemplate($this->template, true, $application); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the rendered content |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function get() |
80
|
|
|
{ |
81
|
|
|
return $this->renderedContent; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Decoupled render method, for usage in derived classes |
86
|
|
|
* |
87
|
|
|
* @param string $template |
88
|
|
|
* |
89
|
|
|
* @param bool $obClean |
90
|
|
|
* @param null | Application $application |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
* @throws \Exception |
94
|
|
|
*/ |
95
|
|
|
public function renderTemplate($template = '', $obClean = true, $application = null) |
96
|
|
|
{ |
97
|
|
|
$templatePath = $this->getTemplatePath($template, $application); |
98
|
|
|
if (realpath($templatePath) !== false) { |
99
|
|
|
if ($obClean) { |
100
|
|
|
ob_clean(); |
101
|
|
|
} |
102
|
|
|
$this->parameters['request'] = $this->request; |
103
|
|
|
if ($application !== null) { |
104
|
|
|
$acParameters = $application->getAllApplicationComponentParameters(); |
105
|
|
|
foreach ($acParameters as $parameters) { |
106
|
|
|
extract($parameters); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
extract($this->parameters); |
110
|
|
|
include($templatePath); |
111
|
|
|
return ob_get_contents(); |
112
|
|
|
} else { |
113
|
|
|
if ($template !== null) { // If template is null, its a application component, which doesnt have a template |
114
|
|
|
throw new \Exception('Couldnt find template ' . $templatePath); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Alias for renderTemplate for usage to include templates in other templates |
121
|
|
|
* |
122
|
|
|
* @param string $template |
123
|
|
|
* |
124
|
|
|
* @param array $parameters |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* @throws \Exception |
128
|
|
|
*/ |
129
|
|
|
public function includeTemplate($template = '', $parameters = array()) |
130
|
|
|
{ |
131
|
|
|
if (is_array($parameters)) { |
132
|
|
|
foreach ($parameters as $name => $value) { |
133
|
|
|
$this->parameters[$name] = $value; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return $this->renderTemplate($template, false); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getParameters() |
140
|
|
|
{ |
141
|
|
|
return $this->parameters; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $template |
146
|
|
|
* @param null | Application $application |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
protected function getTemplateDir($template, $application = null) |
150
|
|
|
{ |
151
|
|
|
$templatePath = ''; |
152
|
|
|
if ($application !== null) { |
153
|
|
|
$templatePath = $application->getTemplateDir(); |
154
|
|
|
} |
155
|
|
|
$templatePath = $templatePath . $template . '.php'; |
156
|
|
|
return $templatePath; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $template |
161
|
|
|
* @param $application |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
private function getTemplatePath($template, $application) |
165
|
|
|
{ |
166
|
|
|
$templateDir = $this->getTemplateDir($template, $application); |
167
|
|
|
if ($application !== null) { |
168
|
|
|
$rootDir = $application->getRootDir(); |
169
|
|
|
if (strpos($templateDir, $rootDir) === false) { |
170
|
|
|
$templatePath = $rootDir . DIRECTORY_SEPARATOR . $templateDir; |
171
|
|
|
} else { |
172
|
|
|
$templatePath = $templateDir; |
173
|
|
|
} |
174
|
|
|
} else { |
175
|
|
|
$templatePath = $templateDir; |
176
|
|
|
} |
177
|
|
|
return $templatePath; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |