Passed
Push — master ( 6aacd7...ff96ae )
by Jens
04:01
created

extractParametersAndIncludeTemplateFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 12
rs 9.4285
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 = null;
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 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
            } else {
111
                if ($template !== null) { // If template is null, its a application component, which doesnt have a template
112
                    throw new \Exception('Couldnt find template ' . $templatePath);
113
                }
114
            }
115
        }
116
117
        /**
118
         * Alias for renderTemplate for usage to include templates in other templates
119
         *
120
         * @param string $template
121
         *
122
         * @param array $parameters
123
         *
124
         * @return string
125
         * @throws \Exception
126
         */
127
        public function includeTemplate($template = '', $parameters = array())
128
        {
129
            if (is_array($parameters)) {
130
                foreach ($parameters as $name => $value) {
131
                    $this->parameters[$name] = $value;
132
                }
133
            }
134
            return $this->renderTemplate($template, false);
135
        }
136
137
        public function getParameters()
138
        {
139
            return $this->parameters;
140
        }
141
142
        /**
143
         * @param $template
144
         * @param null | Application $application
145
         * @return string
146
         */
147
        protected function getTemplateDir($template, $application = null)
148
        {
149
            $templatePath = '';
150
            if ($application !== null) {
151
                $templatePath = $application->getTemplateDir();
152
            }
153
            $templatePath = $templatePath . $template . '.php';
154
            return $templatePath;
155
        }
156
157
        /**
158
         * @param string $template
159
         * @param Application $application
160
         * @return string
161
         */
162
        private function getTemplatePath($template, $application = null)
163
        {
164
            $templateDir = $this->getTemplateDir($template, $application);
165
            if ($application !== null) {
166
                $templatePath = $this->getTemplatePathFromApplication($application, $templateDir);
167
            } elseif ($this->application !== null) {
168
                $templatePath = $this->getTemplatePathFromApplication($this->application, $templateDir);
169
            } else {
170
                $templatePath = $templateDir;
171
            }
172
            return $templatePath;
173
        }
174
175
        /**
176
         * @param Application $application
177
         * @param string $templateDir
178
         * @return string
179
         */
180
        private function getTemplatePathFromApplication($application, $templateDir)
181
        {
182
            $rootDir = $application->getRootDir();
183
184
            if (strpos($templateDir, $rootDir) === false) {
185
                $templatePath = $rootDir . DIRECTORY_SEPARATOR . $templateDir;
186
            } else {
187
                $templatePath = $templateDir;
188
            }
189
            return $templatePath;
190
        }
191
192
        /**
193
         * @param $templatePath
194
         * @param $obClean
195
         * @return mixed|string
196
         */
197
        private function includeTemplateFile($templatePath, $obClean)
198
        {
199
            if ($obClean) {
200
                include($templatePath);
201
                return ob_get_contents();
202
            } else {
203
                return include($templatePath);
204
            }
205
        }
206
207
        /**
208
         * @param string $templatePath
209
         * @param Application $application
210
         * @param bool $obClean
211
         * @return mixed|string
212
         */
213
        private function extractParametersAndIncludeTemplateFile($templatePath, $application = null, $obClean = true)
214
        {
215
            $this->parameters['request'] = $this->request;
216
            if ($application !== null) {
217
                $acParameters = $application->getAllApplicationComponentParameters();
218
                foreach ($acParameters as $parameters) {
219
                    extract($parameters);
220
                }
221
            }
222
            extract($this->parameters);
223
            return $this->includeTemplateFile($templatePath, $obClean);
224
        }
225
    }
226
}