Passed
Push — master ( fa28f5...070d6c )
by Jens
02:40
created

BaseComponent::renderTemplate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 2 Features 4
Metric Value
cc 6
eloc 16
c 8
b 2
f 4
nc 6
nop 3
dl 0
loc 23
rs 8.5906
1
<?php
2
namespace library\components
3
{
4
5
	use library\cc\Application;
6
	use library\cc\Request;
7
	use library\storage\Storage;
8
9
	class BaseComponent implements Component
10
	{
11
		/**
12
		 * @var string
13
		 */
14
		protected $template;
15
		/**
16
		 * @var 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
		 * @return mixed|void
57
		 */
58
		public function run(Storage $storage)
59
		{
60
			$this->storage = $storage;
61
		}
62
63
		/**
64
		 * Renders the template
65
		 *
66
		 * @param null|Application $application
67
		 *
68
		 * @throws \Exception
69
		 */
70
		public function render($application=null)
71
		{
72
			$this->renderedContent = $this->renderTemplate($this->template, true, $application);
73
		}
74
75
		/**
76
		 * Returns the rendered content
77
		 *
78
		 * @return mixed
79
		 */
80
		public function get()
81
		{
82
			return $this->renderedContent;
83
		}
84
85
		/**
86
		 * Decoupled render method, for usage in derived classes
87
		 *
88
		 * @param string $template
89
		 *
90
		 * @param bool   $obClean
91
		 * @param null|Application   $application
92
		 *
93
		 * @return string
94
		 * @throws \Exception
95
		 */
96
		public function renderTemplate($template='', $obClean = true, $application=null)
97
		{
98
			$templatePath = __DIR__ . '/../../templates/' . $template . '.php';
99
			if (realpath($templatePath) !== false) {
100
				if ($obClean) {
101
					ob_clean();
102
				}
103
				$this->parameters['request'] = $this->request;
104
				if ($application !== null) {
105
					$acParameters = $application->getAllApplicationComponentParameters();
106
					foreach ($acParameters as $parameters) {
107
						extract($parameters);
108
					}
109
				}
110
				extract($this->parameters);
111
				include($templatePath);
112
				return ob_get_contents();
113
			} else {
114
				if ($template !== null) { // If template is null, its a application component, which doesnt have a template
115
					throw new \Exception('Couldnt find template ' . $templatePath);
116
				}
117
			}
118
		}
119
120
		/**
121
		 * Alias for renderTemplate for usage to include templates in other templates
122
		 *
123
		 * @param string $template
124
		 *
125
		 * @param array  $parameters
126
		 *
127
		 * @return string
128
		 * @throws \Exception
129
		 */
130
		public function includeTemplate($template='', $parameters = array())
131
		{
132
			if (is_array($parameters)) {
133
				foreach ($parameters as $name => $value) {
134
					$this->parameters[$name] = $value;
135
				}
136
			}
137
			return $this->renderTemplate($template, false);
138
		}
139
140
		public function getParameters()
141
		{
142
			return $this->parameters;
143
		}
144
	}
145
}