@@ -1,10 +1,10 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Created by PhpStorm. |
|
4 | - * User: franae |
|
5 | - * Date: 30/10/13 |
|
6 | - * Time: 14:47 |
|
7 | - */ |
|
3 | + * Created by PhpStorm. |
|
4 | + * User: franae |
|
5 | + * Date: 30/10/13 |
|
6 | + * Time: 14:47 |
|
7 | + */ |
|
8 | 8 | |
9 | 9 | namespace Phile\Exception; |
10 | 10 |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * The Event class |
|
4 | - */ |
|
3 | + * The Event class |
|
4 | + */ |
|
5 | 5 | namespace Phile; |
6 | 6 | |
7 | 7 | /** |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * config file |
|
4 | - */ |
|
3 | + * config file |
|
4 | + */ |
|
5 | 5 | return [ |
6 | 6 | 'setting-example' => 'I love Phile!' |
7 | 7 | ]; |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * The SerciveLocator class |
|
4 | - */ |
|
3 | + * The SerciveLocator class |
|
4 | + */ |
|
5 | 5 | namespace Phile\Core; |
6 | 6 | |
7 | 7 | use Phile\Exception\ServiceLocatorException; |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | /** |
52 | 52 | * resolve a file path by replace the mod: prefix |
53 | 53 | * |
54 | - * @param $path |
|
54 | + * @param string $path |
|
55 | 55 | * |
56 | 56 | * @return string|null the full filepath or null if file does not exists |
57 | 57 | */ |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | /** |
72 | 72 | * load files e.g. config files |
73 | 73 | * |
74 | - * @param $file |
|
74 | + * @param string $file |
|
75 | 75 | * |
76 | 76 | * @return mixed|null |
77 | 77 | */ |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | /** |
100 | 100 | * static method to get files by directory and file filter |
101 | 101 | * |
102 | - * @param $directory |
|
102 | + * @param string $directory |
|
103 | 103 | * @param string $filter |
104 | 104 | * |
105 | 105 | * @return array |
@@ -179,8 +179,8 @@ discard block |
||
179 | 179 | * method to get a more secure random value |
180 | 180 | * code from http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string/13733588#13733588 |
181 | 181 | * |
182 | - * @param $min |
|
183 | - * @param $max |
|
182 | + * @param integer $min |
|
183 | + * @param integer $max |
|
184 | 184 | * |
185 | 185 | * @return mixed |
186 | 186 | */ |
@@ -14,190 +14,190 @@ |
||
14 | 14 | */ |
15 | 15 | class Utility { |
16 | 16 | |
17 | - /** |
|
18 | - * method to get the current http protocol |
|
19 | - * |
|
20 | - * @return string the current protocol |
|
21 | - * @deprecated since 1.5 will be removed |
|
22 | - */ |
|
23 | - public static function getProtocol() { |
|
24 | - return (new Router)->getProtocol(); |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * detect base url |
|
29 | - * |
|
30 | - * @return string |
|
31 | - * @deprecated since 1.5 will be removed |
|
32 | - */ |
|
33 | - public static function getBaseUrl() { |
|
34 | - return (new Router)->getBaseUrl(); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * detect install path |
|
39 | - * |
|
40 | - * @return string |
|
41 | - * @deprecated since 1.5 will be removed |
|
42 | - */ |
|
43 | - public static function getInstallPath() { |
|
44 | - $path = self::getBaseUrl(); |
|
45 | - $path = substr($path, strpos($path, '://') + 3); |
|
46 | - $path = substr($path, strpos($path, '/') + 1); |
|
47 | - |
|
48 | - return $path; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * resolve a file path by replace the mod: prefix |
|
53 | - * |
|
54 | - * @param $path |
|
55 | - * |
|
56 | - * @return string|null the full filepath or null if file does not exists |
|
57 | - */ |
|
58 | - public static function resolveFilePath($path) { |
|
59 | - // resolve MOD: prefix |
|
60 | - if (strtoupper(substr($path, 0, 3)) === 'MOD') { |
|
61 | - $path = str_ireplace('mod:', PLUGINS_DIR, $path); |
|
62 | - } |
|
63 | - // check if file exists |
|
64 | - if (file_exists($path)) { |
|
65 | - return $path; |
|
66 | - } |
|
67 | - |
|
68 | - return null; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * load files e.g. config files |
|
73 | - * |
|
74 | - * @param $file |
|
75 | - * |
|
76 | - * @return mixed|null |
|
77 | - */ |
|
78 | - public static function load($file) { |
|
79 | - if (file_exists($file)) { |
|
80 | - return include $file; |
|
81 | - } |
|
82 | - |
|
83 | - return null; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * check if a plugin is loaded |
|
88 | - * |
|
89 | - * @param $plugin |
|
90 | - * @return bool |
|
91 | - * @deprecated since 1.5 will be removed |
|
92 | - * @use 'plugins_loaded' event |
|
93 | - */ |
|
94 | - public static function isPluginLoaded($plugin) { |
|
95 | - $config = Registry::get('Phile_Settings'); |
|
96 | - return (isset($config['plugins'][$plugin]) && isset($config['plugins'][$plugin]['active']) && $config['plugins'][$plugin]['active'] === true); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * static method to get files by directory and file filter |
|
101 | - * |
|
102 | - * @param $directory |
|
103 | - * @param string $filter |
|
104 | - * |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - public static function getFiles($directory, $filter = '\Phile\FilterIterator\GeneralFileFilterIterator') { |
|
108 | - $files = new $filter( |
|
109 | - new \RecursiveIteratorIterator( |
|
110 | - new \RecursiveDirectoryIterator( |
|
111 | - $directory, |
|
112 | - \RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
|
113 | - ) |
|
114 | - ) |
|
115 | - ); |
|
116 | - $result = array(); |
|
117 | - foreach ($files as $file) { |
|
118 | - /** @var \SplFileInfo $file */ |
|
119 | - $result[] = $file->getPathname(); |
|
120 | - } |
|
121 | - |
|
122 | - return $result; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * redirect to an url |
|
127 | - * |
|
128 | - * @param $url the url to redirect to |
|
129 | - * @param int $statusCode the http status code |
|
130 | - * @deprecated since 1.5 will be removed |
|
131 | - */ |
|
132 | - public static function redirect($url, $statusCode = 302) { |
|
133 | - (new Response)->redirect($url, $statusCode); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * generate secure md5 hash |
|
138 | - * |
|
139 | - * @param $value |
|
140 | - * |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - public static function getSecureMD5Hash($value) { |
|
144 | - $config = Registry::get('Phile_Settings'); |
|
145 | - |
|
146 | - return md5($config['encryptionKey'] . $value); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * method to generate a secure token |
|
151 | - * code from http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string/13733588#13733588 |
|
152 | - * modified by Frank Nägler |
|
153 | - * |
|
154 | - * @param int $length |
|
155 | - * @param bool $widthSpecialChars |
|
156 | - * @param null $additionalChars |
|
157 | - * |
|
158 | - * @return string |
|
159 | - */ |
|
160 | - public static function generateSecureToken($length = 32, $widthSpecialChars = true, $additionalChars = null) { |
|
161 | - $token = ""; |
|
162 | - $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
163 | - $codeAlphabet .= "abcdefghijklmnopqrstuvwxyz"; |
|
164 | - $codeAlphabet .= "0123456789"; |
|
165 | - if ($widthSpecialChars) { |
|
166 | - $codeAlphabet .= "!/()=?[]|{}"; |
|
167 | - } |
|
168 | - if ($additionalChars !== null) { |
|
169 | - $codeAlphabet .= $additionalChars; |
|
170 | - } |
|
171 | - for ($i = 0; $i < $length; $i++) { |
|
172 | - $token .= $codeAlphabet[Utility::crypto_rand_secure(0, strlen($codeAlphabet))]; |
|
173 | - } |
|
174 | - |
|
175 | - return $token; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * method to get a more secure random value |
|
180 | - * code from http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string/13733588#13733588 |
|
181 | - * |
|
182 | - * @param $min |
|
183 | - * @param $max |
|
184 | - * |
|
185 | - * @return mixed |
|
186 | - */ |
|
187 | - public static function crypto_rand_secure($min, $max) { |
|
188 | - $range = $max - $min; |
|
189 | - if ($range < 0) { |
|
190 | - return $min; |
|
191 | - } // not so random... |
|
192 | - $log = log($range, 2); |
|
193 | - $bytes = (int)($log / 8) + 1; // length in bytes |
|
194 | - $bits = (int)$log + 1; // length in bits |
|
195 | - $filter = (int)(1 << $bits) - 1; // set all lower bits to 1 |
|
196 | - do { |
|
197 | - $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); |
|
198 | - $rnd = $rnd & $filter; // discard irrelevant bits |
|
199 | - } while ($rnd >= $range); |
|
200 | - |
|
201 | - return $min + $rnd; |
|
202 | - } |
|
17 | + /** |
|
18 | + * method to get the current http protocol |
|
19 | + * |
|
20 | + * @return string the current protocol |
|
21 | + * @deprecated since 1.5 will be removed |
|
22 | + */ |
|
23 | + public static function getProtocol() { |
|
24 | + return (new Router)->getProtocol(); |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * detect base url |
|
29 | + * |
|
30 | + * @return string |
|
31 | + * @deprecated since 1.5 will be removed |
|
32 | + */ |
|
33 | + public static function getBaseUrl() { |
|
34 | + return (new Router)->getBaseUrl(); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * detect install path |
|
39 | + * |
|
40 | + * @return string |
|
41 | + * @deprecated since 1.5 will be removed |
|
42 | + */ |
|
43 | + public static function getInstallPath() { |
|
44 | + $path = self::getBaseUrl(); |
|
45 | + $path = substr($path, strpos($path, '://') + 3); |
|
46 | + $path = substr($path, strpos($path, '/') + 1); |
|
47 | + |
|
48 | + return $path; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * resolve a file path by replace the mod: prefix |
|
53 | + * |
|
54 | + * @param $path |
|
55 | + * |
|
56 | + * @return string|null the full filepath or null if file does not exists |
|
57 | + */ |
|
58 | + public static function resolveFilePath($path) { |
|
59 | + // resolve MOD: prefix |
|
60 | + if (strtoupper(substr($path, 0, 3)) === 'MOD') { |
|
61 | + $path = str_ireplace('mod:', PLUGINS_DIR, $path); |
|
62 | + } |
|
63 | + // check if file exists |
|
64 | + if (file_exists($path)) { |
|
65 | + return $path; |
|
66 | + } |
|
67 | + |
|
68 | + return null; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * load files e.g. config files |
|
73 | + * |
|
74 | + * @param $file |
|
75 | + * |
|
76 | + * @return mixed|null |
|
77 | + */ |
|
78 | + public static function load($file) { |
|
79 | + if (file_exists($file)) { |
|
80 | + return include $file; |
|
81 | + } |
|
82 | + |
|
83 | + return null; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * check if a plugin is loaded |
|
88 | + * |
|
89 | + * @param $plugin |
|
90 | + * @return bool |
|
91 | + * @deprecated since 1.5 will be removed |
|
92 | + * @use 'plugins_loaded' event |
|
93 | + */ |
|
94 | + public static function isPluginLoaded($plugin) { |
|
95 | + $config = Registry::get('Phile_Settings'); |
|
96 | + return (isset($config['plugins'][$plugin]) && isset($config['plugins'][$plugin]['active']) && $config['plugins'][$plugin]['active'] === true); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * static method to get files by directory and file filter |
|
101 | + * |
|
102 | + * @param $directory |
|
103 | + * @param string $filter |
|
104 | + * |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + public static function getFiles($directory, $filter = '\Phile\FilterIterator\GeneralFileFilterIterator') { |
|
108 | + $files = new $filter( |
|
109 | + new \RecursiveIteratorIterator( |
|
110 | + new \RecursiveDirectoryIterator( |
|
111 | + $directory, |
|
112 | + \RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
|
113 | + ) |
|
114 | + ) |
|
115 | + ); |
|
116 | + $result = array(); |
|
117 | + foreach ($files as $file) { |
|
118 | + /** @var \SplFileInfo $file */ |
|
119 | + $result[] = $file->getPathname(); |
|
120 | + } |
|
121 | + |
|
122 | + return $result; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * redirect to an url |
|
127 | + * |
|
128 | + * @param $url the url to redirect to |
|
129 | + * @param int $statusCode the http status code |
|
130 | + * @deprecated since 1.5 will be removed |
|
131 | + */ |
|
132 | + public static function redirect($url, $statusCode = 302) { |
|
133 | + (new Response)->redirect($url, $statusCode); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * generate secure md5 hash |
|
138 | + * |
|
139 | + * @param $value |
|
140 | + * |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + public static function getSecureMD5Hash($value) { |
|
144 | + $config = Registry::get('Phile_Settings'); |
|
145 | + |
|
146 | + return md5($config['encryptionKey'] . $value); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * method to generate a secure token |
|
151 | + * code from http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string/13733588#13733588 |
|
152 | + * modified by Frank Nägler |
|
153 | + * |
|
154 | + * @param int $length |
|
155 | + * @param bool $widthSpecialChars |
|
156 | + * @param null $additionalChars |
|
157 | + * |
|
158 | + * @return string |
|
159 | + */ |
|
160 | + public static function generateSecureToken($length = 32, $widthSpecialChars = true, $additionalChars = null) { |
|
161 | + $token = ""; |
|
162 | + $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
163 | + $codeAlphabet .= "abcdefghijklmnopqrstuvwxyz"; |
|
164 | + $codeAlphabet .= "0123456789"; |
|
165 | + if ($widthSpecialChars) { |
|
166 | + $codeAlphabet .= "!/()=?[]|{}"; |
|
167 | + } |
|
168 | + if ($additionalChars !== null) { |
|
169 | + $codeAlphabet .= $additionalChars; |
|
170 | + } |
|
171 | + for ($i = 0; $i < $length; $i++) { |
|
172 | + $token .= $codeAlphabet[Utility::crypto_rand_secure(0, strlen($codeAlphabet))]; |
|
173 | + } |
|
174 | + |
|
175 | + return $token; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * method to get a more secure random value |
|
180 | + * code from http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string/13733588#13733588 |
|
181 | + * |
|
182 | + * @param $min |
|
183 | + * @param $max |
|
184 | + * |
|
185 | + * @return mixed |
|
186 | + */ |
|
187 | + public static function crypto_rand_secure($min, $max) { |
|
188 | + $range = $max - $min; |
|
189 | + if ($range < 0) { |
|
190 | + return $min; |
|
191 | + } // not so random... |
|
192 | + $log = log($range, 2); |
|
193 | + $bytes = (int)($log / 8) + 1; // length in bytes |
|
194 | + $bits = (int)$log + 1; // length in bits |
|
195 | + $filter = (int)(1 << $bits) - 1; // set all lower bits to 1 |
|
196 | + do { |
|
197 | + $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); |
|
198 | + $rnd = $rnd & $filter; // discard irrelevant bits |
|
199 | + } while ($rnd >= $range); |
|
200 | + |
|
201 | + return $min + $rnd; |
|
202 | + } |
|
203 | 203 | } |
@@ -123,7 +123,7 @@ |
||
123 | 123 | /** |
124 | 124 | * set content of page |
125 | 125 | * |
126 | - * @param $content |
|
126 | + * @param string $content |
|
127 | 127 | */ |
128 | 128 | public function setContent($content) { |
129 | 129 | $this->content = $content; |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * The page model |
|
4 | - */ |
|
3 | + * The page model |
|
4 | + */ |
|
5 | 5 | namespace Phile\Model; |
6 | 6 | |
7 | 7 | use Phile\Core\Router; |
@@ -17,237 +17,237 @@ discard block |
||
17 | 17 | * @package Phile\Model |
18 | 18 | */ |
19 | 19 | class Page { |
20 | - /** |
|
21 | - * @var \Phile\Model\Meta the meta model |
|
22 | - */ |
|
23 | - protected $meta; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string the content |
|
27 | - */ |
|
28 | - protected $content; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var string the path to the original file |
|
32 | - */ |
|
33 | - protected $filePath; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string the raw file |
|
37 | - */ |
|
38 | - protected $rawData; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var \Phile\ServiceLocator\ParserInterface the parser |
|
42 | - */ |
|
43 | - protected $parser; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string the pageId of the page |
|
47 | - */ |
|
48 | - protected $pageId; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var \Phile\Model\Page the previous page if one exist |
|
52 | - */ |
|
53 | - protected $previousPage; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var \Phile\Model\Page the next page if one exist |
|
57 | - */ |
|
58 | - protected $nextPage; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var string The content folder, as passed to the class constructor when initiating the object. |
|
62 | - */ |
|
63 | - protected $contentFolder = CONTENT_DIR; |
|
64 | - |
|
65 | - /** |
|
66 | - * the constructor |
|
67 | - * |
|
68 | - * @param $filePath |
|
69 | - * @param string $folder |
|
70 | - */ |
|
71 | - public function __construct($filePath, $folder = CONTENT_DIR) { |
|
72 | - $this->contentFolder = $folder; |
|
73 | - $this->setFilePath($filePath); |
|
74 | - |
|
75 | - /** |
|
76 | - * @triggerEvent before_load_content this event is triggered before the content is loaded |
|
77 | - * |
|
78 | - * @param string filePath the path to the file |
|
79 | - * @param \Phile\Model\Page page the page model |
|
80 | - */ |
|
81 | - Event::triggerEvent('before_load_content', array('filePath' => &$this->filePath, 'page' => &$this)); |
|
82 | - if (file_exists($this->filePath)) { |
|
83 | - $this->rawData = file_get_contents($this->filePath); |
|
84 | - $this->parseRawData(); |
|
85 | - } |
|
86 | - /** |
|
87 | - * @triggerEvent after_load_content this event is triggered after the content is loaded |
|
88 | - * |
|
89 | - * @param string filePath the path to the file |
|
90 | - * @param string rawData the raw data |
|
91 | - * @param \Phile\Model\Page page the page model |
|
92 | - */ |
|
93 | - Event::triggerEvent('after_load_content', array('filePath' => &$this->filePath, 'rawData' => $this->rawData, 'page' => &$this)); |
|
94 | - |
|
95 | - $this->parser = ServiceLocator::getService('Phile_Parser'); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * method to get content of page, this method returned the parsed content |
|
100 | - * |
|
101 | - * @return mixed |
|
102 | - */ |
|
103 | - public function getContent() { |
|
104 | - /** |
|
105 | - * @triggerEvent before_parse_content this event is triggered before the content is parsed |
|
106 | - * |
|
107 | - * @param string content the raw data |
|
108 | - * @param \Phile\Model\Page page the page model |
|
109 | - */ |
|
110 | - Event::triggerEvent('before_parse_content', array('content' => $this->content, 'page' => &$this)); |
|
111 | - $content = $this->parser->parse($this->content); |
|
112 | - /** |
|
113 | - * @triggerEvent after_parse_content this event is triggered after the content is parsed |
|
114 | - * |
|
115 | - * @param string content the parsed content |
|
116 | - * @param \Phile\Model\Page page the page model |
|
117 | - */ |
|
118 | - Event::triggerEvent('after_parse_content', array('content' => &$content, 'page' => &$this)); |
|
119 | - |
|
120 | - return $content; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * set content of page |
|
125 | - * |
|
126 | - * @param $content |
|
127 | - */ |
|
128 | - public function setContent($content) { |
|
129 | - $this->content = $content; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * get raw (un-parsed) page content |
|
134 | - * |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - public function getRawContent() { |
|
138 | - return $this->content; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * get the meta model |
|
143 | - * |
|
144 | - * @return Meta |
|
145 | - */ |
|
146 | - public function getMeta() { |
|
147 | - return $this->meta; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * parse the raw content |
|
152 | - */ |
|
153 | - protected function parseRawData() { |
|
154 | - $this->meta = new Meta($this->rawData); |
|
155 | - // Remove only the optional, leading meta-block comment |
|
156 | - $rawData = trim($this->rawData); |
|
157 | - if (strncmp('<!--', $rawData, 4) === 0) { |
|
158 | - // leading meta-block comment uses the <!-- --> style |
|
159 | - $this->content = substr($rawData, max(4, strpos($rawData, '-->') + 3)); |
|
160 | - } elseif (strncmp('/*', $rawData, 2) === 0) { |
|
161 | - // leading meta-block comment uses the /* */ style |
|
162 | - $this->content = substr($rawData, strpos($rawData, '*/') + 2); |
|
163 | - } else { |
|
164 | - // no leading meta-block comment |
|
165 | - $this->content = $rawData; |
|
166 | - } |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * get the title of page from meta information |
|
171 | - * |
|
172 | - * @return string|null |
|
173 | - */ |
|
174 | - public function getTitle() { |
|
175 | - return $this->getMeta()->get('title'); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * get Phile $pageId |
|
180 | - * |
|
181 | - * @param string $filePath |
|
182 | - * @return string |
|
183 | - */ |
|
184 | - protected function buildPageId($filePath) { |
|
185 | - $pageId = str_replace($this->contentFolder, '', $filePath); |
|
186 | - $pageId = str_replace(CONTENT_EXT, '', $pageId); |
|
187 | - $pageId = str_replace(DIRECTORY_SEPARATOR, '/', $pageId); |
|
188 | - $pageId = ltrim($pageId, '/'); |
|
189 | - $pageId = preg_replace('/(?<=^|\/)index$/', '', $pageId); |
|
190 | - return $pageId; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * get the url of page |
|
195 | - * |
|
196 | - * @return string |
|
197 | - */ |
|
198 | - public function getUrl() { |
|
199 | - return (new Router)->urlForPage($this->pageId, false); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * set the filepath of the page |
|
204 | - * |
|
205 | - * @param string $filePath |
|
206 | - */ |
|
207 | - public function setFilePath($filePath) { |
|
208 | - $this->filePath = $filePath; |
|
209 | - $this->pageId = $this->buildPageId($this->filePath); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * get the filepath of the page |
|
214 | - * |
|
215 | - * @return string |
|
216 | - */ |
|
217 | - public function getFilePath() { |
|
218 | - return $this->filePath; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * get the folder name |
|
223 | - * |
|
224 | - * @return string |
|
225 | - */ |
|
226 | - public function getFolder() { |
|
227 | - return basename(dirname($this->getFilePath())); |
|
228 | - } |
|
229 | - |
|
230 | - public function getPageId() { |
|
231 | - return $this->pageId; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * get the previous page if one exist |
|
236 | - * |
|
237 | - * @return null|\Phile\Model\Page |
|
238 | - */ |
|
239 | - public function getPreviousPage() { |
|
240 | - $pageRepository = new \Phile\Repository\Page(); |
|
241 | - return $pageRepository->getPageOffset($this, -1); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * get the next page if one exist |
|
246 | - * |
|
247 | - * @return null|\Phile\Model\Page |
|
248 | - */ |
|
249 | - public function getNextPage() { |
|
250 | - $pageRepository = new \Phile\Repository\Page(); |
|
251 | - return $pageRepository->getPageOffset($this, 1); |
|
252 | - } |
|
20 | + /** |
|
21 | + * @var \Phile\Model\Meta the meta model |
|
22 | + */ |
|
23 | + protected $meta; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string the content |
|
27 | + */ |
|
28 | + protected $content; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var string the path to the original file |
|
32 | + */ |
|
33 | + protected $filePath; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string the raw file |
|
37 | + */ |
|
38 | + protected $rawData; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var \Phile\ServiceLocator\ParserInterface the parser |
|
42 | + */ |
|
43 | + protected $parser; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string the pageId of the page |
|
47 | + */ |
|
48 | + protected $pageId; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var \Phile\Model\Page the previous page if one exist |
|
52 | + */ |
|
53 | + protected $previousPage; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var \Phile\Model\Page the next page if one exist |
|
57 | + */ |
|
58 | + protected $nextPage; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var string The content folder, as passed to the class constructor when initiating the object. |
|
62 | + */ |
|
63 | + protected $contentFolder = CONTENT_DIR; |
|
64 | + |
|
65 | + /** |
|
66 | + * the constructor |
|
67 | + * |
|
68 | + * @param $filePath |
|
69 | + * @param string $folder |
|
70 | + */ |
|
71 | + public function __construct($filePath, $folder = CONTENT_DIR) { |
|
72 | + $this->contentFolder = $folder; |
|
73 | + $this->setFilePath($filePath); |
|
74 | + |
|
75 | + /** |
|
76 | + * @triggerEvent before_load_content this event is triggered before the content is loaded |
|
77 | + * |
|
78 | + * @param string filePath the path to the file |
|
79 | + * @param \Phile\Model\Page page the page model |
|
80 | + */ |
|
81 | + Event::triggerEvent('before_load_content', array('filePath' => &$this->filePath, 'page' => &$this)); |
|
82 | + if (file_exists($this->filePath)) { |
|
83 | + $this->rawData = file_get_contents($this->filePath); |
|
84 | + $this->parseRawData(); |
|
85 | + } |
|
86 | + /** |
|
87 | + * @triggerEvent after_load_content this event is triggered after the content is loaded |
|
88 | + * |
|
89 | + * @param string filePath the path to the file |
|
90 | + * @param string rawData the raw data |
|
91 | + * @param \Phile\Model\Page page the page model |
|
92 | + */ |
|
93 | + Event::triggerEvent('after_load_content', array('filePath' => &$this->filePath, 'rawData' => $this->rawData, 'page' => &$this)); |
|
94 | + |
|
95 | + $this->parser = ServiceLocator::getService('Phile_Parser'); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * method to get content of page, this method returned the parsed content |
|
100 | + * |
|
101 | + * @return mixed |
|
102 | + */ |
|
103 | + public function getContent() { |
|
104 | + /** |
|
105 | + * @triggerEvent before_parse_content this event is triggered before the content is parsed |
|
106 | + * |
|
107 | + * @param string content the raw data |
|
108 | + * @param \Phile\Model\Page page the page model |
|
109 | + */ |
|
110 | + Event::triggerEvent('before_parse_content', array('content' => $this->content, 'page' => &$this)); |
|
111 | + $content = $this->parser->parse($this->content); |
|
112 | + /** |
|
113 | + * @triggerEvent after_parse_content this event is triggered after the content is parsed |
|
114 | + * |
|
115 | + * @param string content the parsed content |
|
116 | + * @param \Phile\Model\Page page the page model |
|
117 | + */ |
|
118 | + Event::triggerEvent('after_parse_content', array('content' => &$content, 'page' => &$this)); |
|
119 | + |
|
120 | + return $content; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * set content of page |
|
125 | + * |
|
126 | + * @param $content |
|
127 | + */ |
|
128 | + public function setContent($content) { |
|
129 | + $this->content = $content; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * get raw (un-parsed) page content |
|
134 | + * |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + public function getRawContent() { |
|
138 | + return $this->content; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * get the meta model |
|
143 | + * |
|
144 | + * @return Meta |
|
145 | + */ |
|
146 | + public function getMeta() { |
|
147 | + return $this->meta; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * parse the raw content |
|
152 | + */ |
|
153 | + protected function parseRawData() { |
|
154 | + $this->meta = new Meta($this->rawData); |
|
155 | + // Remove only the optional, leading meta-block comment |
|
156 | + $rawData = trim($this->rawData); |
|
157 | + if (strncmp('<!--', $rawData, 4) === 0) { |
|
158 | + // leading meta-block comment uses the <!-- --> style |
|
159 | + $this->content = substr($rawData, max(4, strpos($rawData, '-->') + 3)); |
|
160 | + } elseif (strncmp('/*', $rawData, 2) === 0) { |
|
161 | + // leading meta-block comment uses the /* */ style |
|
162 | + $this->content = substr($rawData, strpos($rawData, '*/') + 2); |
|
163 | + } else { |
|
164 | + // no leading meta-block comment |
|
165 | + $this->content = $rawData; |
|
166 | + } |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * get the title of page from meta information |
|
171 | + * |
|
172 | + * @return string|null |
|
173 | + */ |
|
174 | + public function getTitle() { |
|
175 | + return $this->getMeta()->get('title'); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * get Phile $pageId |
|
180 | + * |
|
181 | + * @param string $filePath |
|
182 | + * @return string |
|
183 | + */ |
|
184 | + protected function buildPageId($filePath) { |
|
185 | + $pageId = str_replace($this->contentFolder, '', $filePath); |
|
186 | + $pageId = str_replace(CONTENT_EXT, '', $pageId); |
|
187 | + $pageId = str_replace(DIRECTORY_SEPARATOR, '/', $pageId); |
|
188 | + $pageId = ltrim($pageId, '/'); |
|
189 | + $pageId = preg_replace('/(?<=^|\/)index$/', '', $pageId); |
|
190 | + return $pageId; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * get the url of page |
|
195 | + * |
|
196 | + * @return string |
|
197 | + */ |
|
198 | + public function getUrl() { |
|
199 | + return (new Router)->urlForPage($this->pageId, false); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * set the filepath of the page |
|
204 | + * |
|
205 | + * @param string $filePath |
|
206 | + */ |
|
207 | + public function setFilePath($filePath) { |
|
208 | + $this->filePath = $filePath; |
|
209 | + $this->pageId = $this->buildPageId($this->filePath); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * get the filepath of the page |
|
214 | + * |
|
215 | + * @return string |
|
216 | + */ |
|
217 | + public function getFilePath() { |
|
218 | + return $this->filePath; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * get the folder name |
|
223 | + * |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + public function getFolder() { |
|
227 | + return basename(dirname($this->getFilePath())); |
|
228 | + } |
|
229 | + |
|
230 | + public function getPageId() { |
|
231 | + return $this->pageId; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * get the previous page if one exist |
|
236 | + * |
|
237 | + * @return null|\Phile\Model\Page |
|
238 | + */ |
|
239 | + public function getPreviousPage() { |
|
240 | + $pageRepository = new \Phile\Repository\Page(); |
|
241 | + return $pageRepository->getPageOffset($this, -1); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * get the next page if one exist |
|
246 | + * |
|
247 | + * @return null|\Phile\Model\Page |
|
248 | + */ |
|
249 | + public function getNextPage() { |
|
250 | + $pageRepository = new \Phile\Repository\Page(); |
|
251 | + return $pageRepository->getPageOffset($this, 1); |
|
252 | + } |
|
253 | 253 | } |
@@ -118,7 +118,7 @@ |
||
118 | 118 | * get file path to plugin root (trailing slash) or to a sub-item |
119 | 119 | * |
120 | 120 | * @param string $subPath |
121 | - * @return null|string null if item does not exist |
|
121 | + * @return string null if item does not exist |
|
122 | 122 | */ |
123 | 123 | protected function getPluginPath($subPath = '') { |
124 | 124 | return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR); |
@@ -19,109 +19,109 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class AbstractPlugin implements EventObserverInterface { |
21 | 21 | |
22 | - /** @var string plugin attributes */ |
|
23 | - protected $plugin = []; |
|
24 | - |
|
25 | - /** @var array subscribed Phile events ['eventName' => 'classMethodToCall'] */ |
|
26 | - protected $events = []; |
|
27 | - |
|
28 | - /** @var array the plugin settings */ |
|
29 | - protected $settings = []; |
|
30 | - |
|
31 | - /** |
|
32 | - * initialize plugin |
|
33 | - * |
|
34 | - * try to keep all initialization in one method to have a clean class |
|
35 | - * for the plugin-user |
|
36 | - * |
|
37 | - * @param string $pluginKey |
|
38 | - * @deprecated since 1.5.1 will be declared 'final' |
|
39 | - */ |
|
40 | - public function initializePlugin($pluginKey) { |
|
41 | - /** |
|
42 | - * init $plugin property |
|
43 | - */ |
|
44 | - $this->plugin['key'] = $pluginKey; |
|
45 | - list($vendor, $name) = explode('\\', $this->plugin['key']); |
|
46 | - $DS = DIRECTORY_SEPARATOR; |
|
47 | - $this->plugin['dir'] = PLUGINS_DIR . $vendor . $DS . $name . $DS; |
|
48 | - |
|
49 | - /** |
|
50 | - * init events |
|
51 | - */ |
|
52 | - foreach ($this->events as $event => $method) { |
|
53 | - Event::registerEvent($event, $this); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * init plugin settings |
|
58 | - */ |
|
59 | - $defaults = Utility::load($this->getPluginPath('config.php')); |
|
60 | - if (empty($defaults) || !is_array($defaults)) { |
|
61 | - $defaults = []; |
|
62 | - } |
|
63 | - |
|
64 | - $globals = Registry::get('Phile_Settings'); |
|
65 | - if (!isset($globals['plugins'][$pluginKey])) { |
|
66 | - $globals['plugins'][$pluginKey] = []; |
|
67 | - } |
|
68 | - |
|
69 | - // settings precedence: global > default > class |
|
70 | - $this->settings = array_replace_recursive( |
|
71 | - $this->settings, |
|
72 | - $defaults, |
|
73 | - $globals['plugins'][$pluginKey] |
|
74 | - ); |
|
75 | - |
|
76 | - // backwards compatibility to Phile 1.4 |
|
77 | - $this->injectSettings($this->settings); |
|
78 | - |
|
79 | - $globals['plugins'][$pluginKey]['settings'] = $this->settings; |
|
80 | - Registry::set('Phile_Settings', $globals); |
|
81 | - |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * inject settings |
|
86 | - * |
|
87 | - * backwards compatibility to Phile 1.4 |
|
88 | - * |
|
89 | - * @param array $settings |
|
90 | - * @deprecated since 1.5.1 will be removed |
|
91 | - */ |
|
92 | - public function injectSettings(array $settings = null) { |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * implements EventObserverInterface |
|
97 | - * |
|
98 | - * @param string $eventKey |
|
99 | - * @param null $data |
|
100 | - * @return void |
|
101 | - */ |
|
102 | - public function on($eventKey, $data = null) { |
|
103 | - if (!isset($this->events[$eventKey])) { |
|
104 | - return; |
|
105 | - } |
|
106 | - $method = $this->events[$eventKey]; |
|
107 | - if (!is_callable([$this, $method])) { |
|
108 | - $class = get_class($this); |
|
109 | - throw new \RuntimeException( |
|
110 | - "Event $eventKey can't invoke $class::$method(). Not callable.", |
|
111 | - 1428564865 |
|
112 | - ); |
|
113 | - } |
|
114 | - $this->{$this->events[$eventKey]}($data); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * get file path to plugin root (trailing slash) or to a sub-item |
|
119 | - * |
|
120 | - * @param string $subPath |
|
121 | - * @return null|string null if item does not exist |
|
122 | - */ |
|
123 | - protected function getPluginPath($subPath = '') { |
|
124 | - return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR); |
|
125 | - } |
|
22 | + /** @var string plugin attributes */ |
|
23 | + protected $plugin = []; |
|
24 | + |
|
25 | + /** @var array subscribed Phile events ['eventName' => 'classMethodToCall'] */ |
|
26 | + protected $events = []; |
|
27 | + |
|
28 | + /** @var array the plugin settings */ |
|
29 | + protected $settings = []; |
|
30 | + |
|
31 | + /** |
|
32 | + * initialize plugin |
|
33 | + * |
|
34 | + * try to keep all initialization in one method to have a clean class |
|
35 | + * for the plugin-user |
|
36 | + * |
|
37 | + * @param string $pluginKey |
|
38 | + * @deprecated since 1.5.1 will be declared 'final' |
|
39 | + */ |
|
40 | + public function initializePlugin($pluginKey) { |
|
41 | + /** |
|
42 | + * init $plugin property |
|
43 | + */ |
|
44 | + $this->plugin['key'] = $pluginKey; |
|
45 | + list($vendor, $name) = explode('\\', $this->plugin['key']); |
|
46 | + $DS = DIRECTORY_SEPARATOR; |
|
47 | + $this->plugin['dir'] = PLUGINS_DIR . $vendor . $DS . $name . $DS; |
|
48 | + |
|
49 | + /** |
|
50 | + * init events |
|
51 | + */ |
|
52 | + foreach ($this->events as $event => $method) { |
|
53 | + Event::registerEvent($event, $this); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * init plugin settings |
|
58 | + */ |
|
59 | + $defaults = Utility::load($this->getPluginPath('config.php')); |
|
60 | + if (empty($defaults) || !is_array($defaults)) { |
|
61 | + $defaults = []; |
|
62 | + } |
|
63 | + |
|
64 | + $globals = Registry::get('Phile_Settings'); |
|
65 | + if (!isset($globals['plugins'][$pluginKey])) { |
|
66 | + $globals['plugins'][$pluginKey] = []; |
|
67 | + } |
|
68 | + |
|
69 | + // settings precedence: global > default > class |
|
70 | + $this->settings = array_replace_recursive( |
|
71 | + $this->settings, |
|
72 | + $defaults, |
|
73 | + $globals['plugins'][$pluginKey] |
|
74 | + ); |
|
75 | + |
|
76 | + // backwards compatibility to Phile 1.4 |
|
77 | + $this->injectSettings($this->settings); |
|
78 | + |
|
79 | + $globals['plugins'][$pluginKey]['settings'] = $this->settings; |
|
80 | + Registry::set('Phile_Settings', $globals); |
|
81 | + |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * inject settings |
|
86 | + * |
|
87 | + * backwards compatibility to Phile 1.4 |
|
88 | + * |
|
89 | + * @param array $settings |
|
90 | + * @deprecated since 1.5.1 will be removed |
|
91 | + */ |
|
92 | + public function injectSettings(array $settings = null) { |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * implements EventObserverInterface |
|
97 | + * |
|
98 | + * @param string $eventKey |
|
99 | + * @param null $data |
|
100 | + * @return void |
|
101 | + */ |
|
102 | + public function on($eventKey, $data = null) { |
|
103 | + if (!isset($this->events[$eventKey])) { |
|
104 | + return; |
|
105 | + } |
|
106 | + $method = $this->events[$eventKey]; |
|
107 | + if (!is_callable([$this, $method])) { |
|
108 | + $class = get_class($this); |
|
109 | + throw new \RuntimeException( |
|
110 | + "Event $eventKey can't invoke $class::$method(). Not callable.", |
|
111 | + 1428564865 |
|
112 | + ); |
|
113 | + } |
|
114 | + $this->{$this->events[$eventKey]}($data); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * get file path to plugin root (trailing slash) or to a sub-item |
|
119 | + * |
|
120 | + * @param string $subPath |
|
121 | + * @return null|string null if item does not exist |
|
122 | + */ |
|
123 | + protected function getPluginPath($subPath = '') { |
|
124 | + return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR); |
|
125 | + } |
|
126 | 126 | |
127 | 127 | } |
@@ -107,7 +107,7 @@ |
||
107 | 107 | if (!is_callable([$this, $method])) { |
108 | 108 | $class = get_class($this); |
109 | 109 | throw new \RuntimeException( |
110 | - "Event $eventKey can't invoke $class::$method(). Not callable.", |
|
110 | + "event $eventKey can't invoke $class::$method(). Not callable.", |
|
111 | 111 | 1428564865 |
112 | 112 | ); |
113 | 113 | } |
@@ -75,7 +75,7 @@ |
||
75 | 75 | /** |
76 | 76 | * wrapper to call the render engine |
77 | 77 | * |
78 | - * @param $engine |
|
78 | + * @param \Twig_Environment $engine |
|
79 | 79 | * @param $vars |
80 | 80 | * @return mixed |
81 | 81 | */ |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Template engine class |
|
4 | - */ |
|
3 | + * Template engine class |
|
4 | + */ |
|
5 | 5 | namespace Phile\Plugin\Phile\TemplateTwig\Template; |
6 | 6 | |
7 | 7 | use Phile\Core\Event; |
@@ -19,157 +19,157 @@ discard block |
||
19 | 19 | * @package Phile\Plugin\Phile\TemplateTwig\Template |
20 | 20 | */ |
21 | 21 | class Twig implements TemplateInterface { |
22 | - /** |
|
23 | - * @var array the complete phile config |
|
24 | - */ |
|
25 | - protected $settings; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var array the config for twig |
|
29 | - */ |
|
30 | - protected $config; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var Page the page model |
|
34 | - */ |
|
35 | - protected $page; |
|
36 | - |
|
37 | - /** |
|
38 | - * the constructor |
|
39 | - * |
|
40 | - * @param array $config the configuration |
|
41 | - */ |
|
42 | - public function __construct($config = []) { |
|
43 | - $this->config = $config; |
|
44 | - $this->settings = Registry::get('Phile_Settings'); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * method to set the current page |
|
49 | - * |
|
50 | - * @param Page $page the page model |
|
51 | - * |
|
52 | - * @return mixed|void |
|
53 | - */ |
|
54 | - public function setCurrentPage(Page $page) { |
|
55 | - $this->page = $page; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * method to render the page/template |
|
60 | - * |
|
61 | - * @return mixed|string |
|
62 | - */ |
|
63 | - public function render() { |
|
64 | - $engine = $this->getEngine(); |
|
65 | - $vars = $this->getTemplateVars(); |
|
66 | - |
|
67 | - Event::triggerEvent( |
|
68 | - 'template_engine_registered', |
|
69 | - ['engine' => &$engine, 'data' => &$vars] |
|
70 | - ); |
|
71 | - |
|
72 | - return $this->_render($engine, $vars); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * wrapper to call the render engine |
|
77 | - * |
|
78 | - * @param $engine |
|
79 | - * @param $vars |
|
80 | - * @return mixed |
|
81 | - */ |
|
82 | - protected function _render($engine, $vars) { |
|
83 | - try { |
|
84 | - $template = $this->getTemplateFileName(); |
|
85 | - } catch (\RuntimeException $e) { |
|
86 | - return $e->getMessage(); |
|
87 | - } |
|
88 | - return $engine->render($template, $vars); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * get template engine |
|
93 | - * |
|
94 | - * @return \Twig_Environment |
|
95 | - */ |
|
96 | - protected function getEngine() { |
|
97 | - $loader = new \Twig_Loader_Filesystem($this->getTemplatePath()); |
|
98 | - $twig = new \Twig_Environment($loader, $this->config); |
|
99 | - |
|
100 | - // load the twig debug extension if required |
|
101 | - if (!empty($this->config['debug'])) { |
|
102 | - $twig->addExtension(new \Twig_Extension_Debug()); |
|
103 | - } |
|
104 | - return $twig; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * get template file name |
|
109 | - * |
|
110 | - * @return string |
|
111 | - * @throws \RuntimeException |
|
112 | - */ |
|
113 | - protected function getTemplateFileName() { |
|
114 | - $template = $this->page->getMeta()->get('template'); |
|
115 | - if (empty($template)) { |
|
116 | - $template = 'index'; |
|
117 | - } |
|
118 | - if (!empty($this->config['template-extension'])) { |
|
119 | - $template .= '.' . $this->config['template-extension']; |
|
120 | - } |
|
121 | - $templatePath = $this->getTemplatePath($template); |
|
122 | - if (!file_exists($templatePath)) { |
|
123 | - throw new \RuntimeException( |
|
124 | - "Template file '{$templatePath}' not found.", |
|
125 | - 1427990135 |
|
126 | - ); |
|
127 | - } |
|
128 | - return $template; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * get file path to (sub-path) in theme-path |
|
133 | - * |
|
134 | - * @param string $sub |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - protected function getTemplatePath($sub = '') { |
|
138 | - $themePath = THEMES_DIR . $this->settings['theme']; |
|
139 | - if (!empty($sub)) { |
|
140 | - $themePath .= '/' . ltrim($sub, DIRECTORY_SEPARATOR); |
|
141 | - } |
|
142 | - return $themePath; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * get template vars |
|
147 | - * |
|
148 | - * @return array|mixed |
|
149 | - * @throws \Exception |
|
150 | - */ |
|
151 | - protected function getTemplateVars() { |
|
152 | - $repository = new Repository($this->settings); |
|
153 | - $defaults = [ |
|
154 | - 'content' => $this->page->getContent(), |
|
155 | - 'meta' => $this->page->getMeta(), |
|
156 | - 'current_page' => $this->page, |
|
157 | - 'base_dir' => rtrim(ROOT_DIR, '/'), |
|
158 | - 'base_url' => $this->settings['base_url'], |
|
159 | - 'config' => $this->settings, |
|
160 | - 'content_dir' => CONTENT_DIR, |
|
161 | - 'content_url' => $this->settings['base_url'] . '/' . basename(CONTENT_DIR), |
|
162 | - 'pages' => $repository->findAll(), |
|
163 | - 'site_title' => $this->settings['site_title'], |
|
164 | - 'theme_dir' => THEMES_DIR . $this->settings['theme'], |
|
165 | - 'theme_url' => $this->settings['base_url'] . '/' . basename(THEMES_DIR) . '/' . $this->settings['theme'], |
|
166 | - ]; |
|
167 | - |
|
168 | - /** @var array $templateVars */ |
|
169 | - $templateVars = Registry::get('templateVars'); |
|
170 | - $templateVars += $defaults; |
|
171 | - |
|
172 | - return $templateVars; |
|
173 | - } |
|
22 | + /** |
|
23 | + * @var array the complete phile config |
|
24 | + */ |
|
25 | + protected $settings; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var array the config for twig |
|
29 | + */ |
|
30 | + protected $config; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var Page the page model |
|
34 | + */ |
|
35 | + protected $page; |
|
36 | + |
|
37 | + /** |
|
38 | + * the constructor |
|
39 | + * |
|
40 | + * @param array $config the configuration |
|
41 | + */ |
|
42 | + public function __construct($config = []) { |
|
43 | + $this->config = $config; |
|
44 | + $this->settings = Registry::get('Phile_Settings'); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * method to set the current page |
|
49 | + * |
|
50 | + * @param Page $page the page model |
|
51 | + * |
|
52 | + * @return mixed|void |
|
53 | + */ |
|
54 | + public function setCurrentPage(Page $page) { |
|
55 | + $this->page = $page; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * method to render the page/template |
|
60 | + * |
|
61 | + * @return mixed|string |
|
62 | + */ |
|
63 | + public function render() { |
|
64 | + $engine = $this->getEngine(); |
|
65 | + $vars = $this->getTemplateVars(); |
|
66 | + |
|
67 | + Event::triggerEvent( |
|
68 | + 'template_engine_registered', |
|
69 | + ['engine' => &$engine, 'data' => &$vars] |
|
70 | + ); |
|
71 | + |
|
72 | + return $this->_render($engine, $vars); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * wrapper to call the render engine |
|
77 | + * |
|
78 | + * @param $engine |
|
79 | + * @param $vars |
|
80 | + * @return mixed |
|
81 | + */ |
|
82 | + protected function _render($engine, $vars) { |
|
83 | + try { |
|
84 | + $template = $this->getTemplateFileName(); |
|
85 | + } catch (\RuntimeException $e) { |
|
86 | + return $e->getMessage(); |
|
87 | + } |
|
88 | + return $engine->render($template, $vars); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * get template engine |
|
93 | + * |
|
94 | + * @return \Twig_Environment |
|
95 | + */ |
|
96 | + protected function getEngine() { |
|
97 | + $loader = new \Twig_Loader_Filesystem($this->getTemplatePath()); |
|
98 | + $twig = new \Twig_Environment($loader, $this->config); |
|
99 | + |
|
100 | + // load the twig debug extension if required |
|
101 | + if (!empty($this->config['debug'])) { |
|
102 | + $twig->addExtension(new \Twig_Extension_Debug()); |
|
103 | + } |
|
104 | + return $twig; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * get template file name |
|
109 | + * |
|
110 | + * @return string |
|
111 | + * @throws \RuntimeException |
|
112 | + */ |
|
113 | + protected function getTemplateFileName() { |
|
114 | + $template = $this->page->getMeta()->get('template'); |
|
115 | + if (empty($template)) { |
|
116 | + $template = 'index'; |
|
117 | + } |
|
118 | + if (!empty($this->config['template-extension'])) { |
|
119 | + $template .= '.' . $this->config['template-extension']; |
|
120 | + } |
|
121 | + $templatePath = $this->getTemplatePath($template); |
|
122 | + if (!file_exists($templatePath)) { |
|
123 | + throw new \RuntimeException( |
|
124 | + "Template file '{$templatePath}' not found.", |
|
125 | + 1427990135 |
|
126 | + ); |
|
127 | + } |
|
128 | + return $template; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * get file path to (sub-path) in theme-path |
|
133 | + * |
|
134 | + * @param string $sub |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + protected function getTemplatePath($sub = '') { |
|
138 | + $themePath = THEMES_DIR . $this->settings['theme']; |
|
139 | + if (!empty($sub)) { |
|
140 | + $themePath .= '/' . ltrim($sub, DIRECTORY_SEPARATOR); |
|
141 | + } |
|
142 | + return $themePath; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * get template vars |
|
147 | + * |
|
148 | + * @return array|mixed |
|
149 | + * @throws \Exception |
|
150 | + */ |
|
151 | + protected function getTemplateVars() { |
|
152 | + $repository = new Repository($this->settings); |
|
153 | + $defaults = [ |
|
154 | + 'content' => $this->page->getContent(), |
|
155 | + 'meta' => $this->page->getMeta(), |
|
156 | + 'current_page' => $this->page, |
|
157 | + 'base_dir' => rtrim(ROOT_DIR, '/'), |
|
158 | + 'base_url' => $this->settings['base_url'], |
|
159 | + 'config' => $this->settings, |
|
160 | + 'content_dir' => CONTENT_DIR, |
|
161 | + 'content_url' => $this->settings['base_url'] . '/' . basename(CONTENT_DIR), |
|
162 | + 'pages' => $repository->findAll(), |
|
163 | + 'site_title' => $this->settings['site_title'], |
|
164 | + 'theme_dir' => THEMES_DIR . $this->settings['theme'], |
|
165 | + 'theme_url' => $this->settings['base_url'] . '/' . basename(THEMES_DIR) . '/' . $this->settings['theme'], |
|
166 | + ]; |
|
167 | + |
|
168 | + /** @var array $templateVars */ |
|
169 | + $templateVars = Registry::get('templateVars'); |
|
170 | + $templateVars += $defaults; |
|
171 | + |
|
172 | + return $templateVars; |
|
173 | + } |
|
174 | 174 | |
175 | 175 | } |
@@ -11,25 +11,25 @@ |
||
11 | 11 | ob_start(); |
12 | 12 | |
13 | 13 | try { |
14 | - \Phile\Bootstrap::getInstance()->initializeBasics(); |
|
15 | - $router = new \Phile\Core\Router(); |
|
16 | - $response = new \Phile\Core\Response(); |
|
17 | - $phileCore = new \Phile\Core($router, $response); |
|
18 | - $phileCore->render(); |
|
14 | + \Phile\Bootstrap::getInstance()->initializeBasics(); |
|
15 | + $router = new \Phile\Core\Router(); |
|
16 | + $response = new \Phile\Core\Response(); |
|
17 | + $phileCore = new \Phile\Core($router, $response); |
|
18 | + $phileCore->render(); |
|
19 | 19 | } catch (\Phile\Exception\AbstractException $e) { |
20 | - if (\Phile\Core\ServiceLocator::hasService('Phile_ErrorHandler')) { |
|
21 | - ob_end_clean(); |
|
20 | + if (\Phile\Core\ServiceLocator::hasService('Phile_ErrorHandler')) { |
|
21 | + ob_end_clean(); |
|
22 | 22 | |
23 | - /** @var \Phile\ServiceLocator\ErrorHandlerInterface $errorHandler */ |
|
24 | - $errorHandler = \Phile\Core\ServiceLocator::getService('Phile_ErrorHandler'); |
|
25 | - $errorHandler->handleException($e); |
|
26 | - } |
|
23 | + /** @var \Phile\ServiceLocator\ErrorHandlerInterface $errorHandler */ |
|
24 | + $errorHandler = \Phile\Core\ServiceLocator::getService('Phile_ErrorHandler'); |
|
25 | + $errorHandler->handleException($e); |
|
26 | + } |
|
27 | 27 | } catch (\Exception $e) { |
28 | - if (\Phile\Core\ServiceLocator::hasService('Phile_ErrorHandler')) { |
|
29 | - ob_end_clean(); |
|
28 | + if (\Phile\Core\ServiceLocator::hasService('Phile_ErrorHandler')) { |
|
29 | + ob_end_clean(); |
|
30 | 30 | |
31 | - /** @var \Phile\ServiceLocator\ErrorHandlerInterface $errorHandler */ |
|
32 | - $errorHandler = \Phile\Core\ServiceLocator::getService('Phile_ErrorHandler'); |
|
33 | - $errorHandler->handleException($e); |
|
34 | - } |
|
31 | + /** @var \Phile\ServiceLocator\ErrorHandlerInterface $errorHandler */ |
|
32 | + $errorHandler = \Phile\Core\ServiceLocator::getService('Phile_ErrorHandler'); |
|
33 | + $errorHandler->handleException($e); |
|
34 | + } |
|
35 | 35 | } |