Issues (55)

src/components/BaseComponent.php (2 issues)

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
         * @var Application
38
         */
39
        protected $application;
40
41
        /**
42
         * BaseComponent constructor.
43
         *
44
         * @param string $template
45
         * @param Request $request
46
         * @param array $parameters
47
         * @param         $matchedSitemapItem
48
         */
49
        public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem)
50
        {
51
            $this->template = $template;
52
            $this->request = $request;
53
            $this->parameters = (array)$parameters;
54
            $this->matchedSitemapItem = $matchedSitemapItem;
55
        }
56
57
        /**
58
         * Hook for implementation in derived classes
59
         *
60
         * @param Storage $storage
61
         */
62
        public function run(Storage $storage)
63
        {
64
            $this->storage = $storage;
65
        }
66
67
        /**
68
         * Renders the template
69
         *
70
         * @param null|Application $application
71
         *
72
         * @throws \Exception
73
         */
74
        public function render($application = null)
75
        {
76
            $this->application = $application;
77
            $this->renderedContent = $this->renderTemplate($this->template, true, $application);
78
        }
79
80
        /**
81
         * Returns the rendered content
82
         *
83
         * @return mixed
84
         */
85
        public function get()
86
        {
87
            return $this->renderedContent;
88
        }
89
90
        /**
91
         * Decoupled render method, for usage in derived classes
92
         *
93
         * @param string $template
94
         *
95
         * @param bool $obClean
96
         * @param null | Application $application
97
         * @param Application $application
98
         *
99
         * @return mixed|string
100
         * @throws \Exception
101
         */
102
        public function renderTemplate($template = '', $obClean = true, $application = null)
103
        {
104
            $templatePath = $this->getTemplatePath($template, $application);
105
            if (realpath($templatePath) !== false) {
106
                if ($obClean) {
107
                    ob_clean();
108
                }
109
                return $this->extractParametersAndIncludeTemplateFile($templatePath, $application, $obClean);
110
            }
111
112
            if ($template !== null) { // If template is null, its a application component, which doesnt have a template
0 ignored issues
show
The condition $template !== null is always true.
Loading history...
113
                throw new \RuntimeException('Couldnt find template ' . $templatePath);
114
            }
115
            return '';
116
        }
117
118
        /**
119
         * Alias for renderTemplate for usage to include templates in other templates
120
         *
121
         * @param string $template
122
         *
123
         * @param array $parameters
124
         *
125
         * @return string
126
         * @throws \Exception
127
         */
128
        public function includeTemplate($template = '', array $parameters = array())
129
        {
130
            if (is_array($parameters)) {
0 ignored issues
show
The condition is_array($parameters) is always true.
Loading history...
131
                foreach ($parameters as $name => $value) {
132
                    $this->parameters[$name] = $value;
133
                }
134
            }
135
            return $this->renderTemplate($template, false);
136
        }
137
138
        /**
139
         * @return array
140
         */
141
        public function getParameters()
142
        {
143
            return $this->parameters;
144
        }
145
146
        /**
147
         * @param $template
148
         * @param null | Application $application
149
         * @return string
150
         */
151
        protected function getTemplateDir($template, $application = null)
152
        {
153
            $templatePath = '';
154
            if ($application !== null) {
155
                $templatePath = $application->getTemplateDir();
156
            }
157
            $templatePath = $templatePath . $template . '.php';
158
            return $templatePath;
159
        }
160
161
        /**
162
         * @param string $template
163
         * @param Application $application
164
         * @return string
165
         */
166
        protected function getTemplatePath($template, $application = null)
167
        {
168
            $templateDir = $this->getTemplateDir($template, $application);
169
            $templatePath = $templateDir;
170
            if ($application !== null) {
171
                $templatePath = $this->getTemplatePathFromApplication($application, $templateDir);
172
            } elseif ($this->application !== null) {
173
                $templatePath = $this->getTemplatePathFromApplication($this->application, $templateDir);
174
            }
175
            return $templatePath;
176
        }
177
178
        /**
179
         * @param Application $application
180
         * @param string $templateDir
181
         * @return string
182
         */
183
        protected function getTemplatePathFromApplication($application, $templateDir)
184
        {
185
            $rootDir = $application->getRootDir();
186
187
            $templatePath = $templateDir;
188
            if (strpos($templateDir, $rootDir) === false) {
189
                $templatePath = $rootDir . DIRECTORY_SEPARATOR . $templateDir;
190
            }
191
            return $templatePath;
192
        }
193
194
        /**
195
         * @param string $templatePath
196
         * @param Application $application
197
         * @param bool $obClean
198
         * @return mixed|string
199
         */
200
        protected function extractParametersAndIncludeTemplateFile($templatePath, $application = null, $obClean = true)
201
        {
202
            $this->parameters['request'] = $this->request;
203
            if ($application !== null) {
204
                $acParameters = $application->getAllApplicationComponentParameters();
205
                foreach ($acParameters as $parameters) {
206
                    extract($parameters, EXTR_OVERWRITE);
207
                }
208
            }
209
            extract($this->parameters, EXTR_OVERWRITE);
210
            if ($obClean) {
211
                include($templatePath);
212
                return ob_get_contents();
213
            }
214
            return include($templatePath);
215
        }
216
217
        /**
218
         * Wheter or not this component is beeing run from a MultiComponent
219
         *
220
         * @return bool
221
         */
222
        protected function ranFromMultiComponent()
223
        {
224
            return isset($this->parameters[MultiComponent::PARAMETER_MULTI_COMPONENT]) && $this->parameters[MultiComponent::PARAMETER_MULTI_COMPONENT] instanceof MultiComponent;
225
        }
226
227
        /**
228
         * If this component is beeing run from a MultiComponent
229
         * returns that MultiComponent. Otherwise will return null
230
         *
231
         * @return MultiComponent|null
232
         */
233
        protected function getMultiComponent()
234
        {
235
            if ($this->ranFromMultiComponent()) {
236
                return $this->parameters[MultiComponent::PARAMETER_MULTI_COMPONENT];
237
            }
238
            return null;
239
        }
240
    }
241
}