Passed
Push — master ( 9c6499...c22bc5 )
by Jens
04:52 queued 02:21
created

BaseComponent::renderTemplate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 6
nop 3
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
namespace CloudControl\Cms\components
3
{
4
5
    use CloudControl\Cms\cc\Request;
6
    use CloudControl\Cms\storage\Storage;
7
8
    class BaseComponent implements Component
9
	{
10
		/**
11
		 * @var string
12
		 */
13
		protected $template;
14
		/**
15
		 * @var \CloudControl\Cms\cc\Request
16
		 */
17
		protected $request;
18
		/**
19
		 * @var Storage
20
		 */
21
		protected $storage;
22
		/**
23
		 * @var mixed
24
		 */
25
		protected $renderedContent;
26
		/**
27
		 * @var array
28
		 */
29
		protected $parameters = array();
30
		/***
31
		 * @var \stdClass
32
		 */
33
		protected $matchedSitemapItem;
34
35
		/**
36
		 * BaseComponent constructor.
37
		 *
38
		 * @param string  $template
39
		 * @param Request $request
40
		 * @param array   $parameters
41
		 * @param         $matchedSitemapItem
42
		 */
43
		public function __construct($template='', Request $request, $parameters=array(), $matchedSitemapItem)
44
		{
45
			$this->template = $template;
46
			$this->request = $request;
47
			$this->parameters = (array) $parameters;
48
			$this->matchedSitemapItem = $matchedSitemapItem;
49
		}
50
51
		/**
52
		 * Hook for implementation in derived classes
53
		 *
54
		 * @param Storage $storage
55
		 */
56
		public function run(Storage $storage)
57
		{
58
			$this->storage = $storage;
59
		}
60
61
		/**
62
		 * Renders the template
63
		 *
64
		 * @param null|Application $application
65
		 *
66
		 * @throws \Exception
67
		 */
68
		public function render($application=null)
69
		{
70
			$this->renderedContent = $this->renderTemplate($this->template, true, $application);
71
		}
72
73
		/**
74
		 * Returns the rendered content
75
		 *
76
		 * @return mixed
77
		 */
78
		public function get()
79
		{
80
			return $this->renderedContent;
81
		}
82
83
		/**
84
		 * Decoupled render method, for usage in derived classes
85
		 *
86
		 * @param string $template
87
		 *
88
		 * @param bool   $obClean
89
		 * @param null|Application   $application
90
		 *
91
		 * @return string
92
		 * @throws \Exception
93
		 */
94
		public function renderTemplate($template='', $obClean = true, $application=null)
95
		{
96
		    $templatePath = $this->getTemplatePath($template, $application);
97
			if (realpath($templatePath) !== false) {
98
				if ($obClean) {
99
					ob_clean();
100
				}
101
				$this->parameters['request'] = $this->request;
102
				if ($application !== null) {
103
					$acParameters = $application->getAllApplicationComponentParameters();
104
					foreach ($acParameters as $parameters) {
105
						extract($parameters);
106
					}
107
				}
108
				extract($this->parameters);
109
				include($templatePath);
110
				return ob_get_contents();
111
			} else {
112
				if ($template !== null) { // If template is null, its a application component, which doesnt have a template
113
					throw new \Exception('Couldnt find template ' . $templatePath);
114
				}
115
			}
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='', $parameters = array())
129
		{
130
			if (is_array($parameters)) {
131
				foreach ($parameters as $name => $value) {
132
					$this->parameters[$name] = $value;
133
				}
134
			}
135
			return $this->renderTemplate($template, false);
136
		}
137
138
		public function getParameters()
139
		{
140
			return $this->parameters;
141
		}
142
143
        /**
144
         * @param $template
145
         * @param null $application
146
         * @return string
147
         */
148
        protected function getTemplatePath($template, $application=null)
149
        {
150
            $templatePath = $application->getTemplatePath();
0 ignored issues
show
Bug introduced by
The method getTemplatePath cannot be called on $application (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
151
            $templatePath = $templatePath . DIRECTORY_SEPARATOR . $template . '.php';
152
            return $templatePath;
153
        }
154
    }
155
}