Passed
Push — develop ( 770114...0a3e89 )
by Jens
02:48
created

BaseComponent::includeTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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