@@ -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 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Registry class |
|
| 4 | - */ |
|
| 3 | + * Registry class |
|
| 4 | + */ |
|
| 5 | 5 | namespace Phile\Core; |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -13,137 +13,137 @@ discard block |
||
| 13 | 13 | * @package Phile\Core |
| 14 | 14 | */ |
| 15 | 15 | class Registry extends \ArrayObject { |
| 16 | - /** |
|
| 17 | - * Registry object provides storage for shared objects. |
|
| 18 | - * |
|
| 19 | - * @var Registry |
|
| 20 | - */ |
|
| 21 | - private static $_registry = null; |
|
| 16 | + /** |
|
| 17 | + * Registry object provides storage for shared objects. |
|
| 18 | + * |
|
| 19 | + * @var Registry |
|
| 20 | + */ |
|
| 21 | + private static $_registry = null; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Retrieves the default registry instance. |
|
| 25 | - * |
|
| 26 | - * @return Registry |
|
| 27 | - */ |
|
| 28 | - public static function getInstance() { |
|
| 29 | - if (self::$_registry === null) { |
|
| 30 | - self::init(); |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Retrieves the default registry instance. |
|
| 25 | + * |
|
| 26 | + * @return Registry |
|
| 27 | + */ |
|
| 28 | + public static function getInstance() { |
|
| 29 | + if (self::$_registry === null) { |
|
| 30 | + self::init(); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - return self::$_registry; |
|
| 34 | - } |
|
| 33 | + return self::$_registry; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Set the default registry instance to a specified instance. |
|
| 38 | - * |
|
| 39 | - * @param Registry $registry An object instance of type Registry, |
|
| 40 | - * or a subclass. |
|
| 41 | - * |
|
| 42 | - * @param Registry $registry |
|
| 43 | - * |
|
| 44 | - * @throws \Exception |
|
| 45 | - */ |
|
| 46 | - public static function setInstance(Registry $registry) { |
|
| 47 | - if (self::$_registry !== null) { |
|
| 48 | - throw new \Exception('Registry is already initialized', 1398536572); |
|
| 49 | - } |
|
| 50 | - self::$_registry = $registry; |
|
| 51 | - } |
|
| 36 | + /** |
|
| 37 | + * Set the default registry instance to a specified instance. |
|
| 38 | + * |
|
| 39 | + * @param Registry $registry An object instance of type Registry, |
|
| 40 | + * or a subclass. |
|
| 41 | + * |
|
| 42 | + * @param Registry $registry |
|
| 43 | + * |
|
| 44 | + * @throws \Exception |
|
| 45 | + */ |
|
| 46 | + public static function setInstance(Registry $registry) { |
|
| 47 | + if (self::$_registry !== null) { |
|
| 48 | + throw new \Exception('Registry is already initialized', 1398536572); |
|
| 49 | + } |
|
| 50 | + self::$_registry = $registry; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Initialize the default registry instance. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - */ |
|
| 58 | - protected static function init() { |
|
| 59 | - self::setInstance(new Registry()); |
|
| 60 | - } |
|
| 53 | + /** |
|
| 54 | + * Initialize the default registry instance. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + */ |
|
| 58 | + protected static function init() { |
|
| 59 | + self::setInstance(new Registry()); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Unset the default registry instance. |
|
| 64 | - * Primarily used in tearDown() in unit tests. |
|
| 65 | - * |
|
| 66 | - * @returns void |
|
| 67 | - */ |
|
| 68 | - public static function _unsetInstance() { |
|
| 69 | - self::$_registry = null; |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * Unset the default registry instance. |
|
| 64 | + * Primarily used in tearDown() in unit tests. |
|
| 65 | + * |
|
| 66 | + * @returns void |
|
| 67 | + */ |
|
| 68 | + public static function _unsetInstance() { |
|
| 69 | + self::$_registry = null; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * getter method, basically same as offsetGet(). |
|
| 74 | - * |
|
| 75 | - * This method can be called from an object of type Registry, or it |
|
| 76 | - * can be called statically. In the latter case, it uses the default |
|
| 77 | - * static instance stored in the class. |
|
| 78 | - * |
|
| 79 | - * @param string $index - get the value associated with $index |
|
| 80 | - * |
|
| 81 | - * @return mixed |
|
| 82 | - * @throws \Exception if no entry is registerd for $index. |
|
| 83 | - */ |
|
| 84 | - public static function get($index) { |
|
| 85 | - $instance = self::getInstance(); |
|
| 86 | - if (!$instance->offsetExists($index)) { |
|
| 87 | - throw new \Exception("No entry is registered for key '$index'", 1398536594); |
|
| 88 | - } |
|
| 72 | + /** |
|
| 73 | + * getter method, basically same as offsetGet(). |
|
| 74 | + * |
|
| 75 | + * This method can be called from an object of type Registry, or it |
|
| 76 | + * can be called statically. In the latter case, it uses the default |
|
| 77 | + * static instance stored in the class. |
|
| 78 | + * |
|
| 79 | + * @param string $index - get the value associated with $index |
|
| 80 | + * |
|
| 81 | + * @return mixed |
|
| 82 | + * @throws \Exception if no entry is registerd for $index. |
|
| 83 | + */ |
|
| 84 | + public static function get($index) { |
|
| 85 | + $instance = self::getInstance(); |
|
| 86 | + if (!$instance->offsetExists($index)) { |
|
| 87 | + throw new \Exception("No entry is registered for key '$index'", 1398536594); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - return $instance->offsetGet($index); |
|
| 91 | - } |
|
| 90 | + return $instance->offsetGet($index); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * setter method, basically same as offsetSet(). |
|
| 95 | - * |
|
| 96 | - * This method can be called from an object of type Registry, or it |
|
| 97 | - * can be called statically. In the latter case, it uses the default |
|
| 98 | - * static instance stored in the class. |
|
| 99 | - * |
|
| 100 | - * @param string $index The location in the ArrayObject in which to store |
|
| 101 | - * the value. |
|
| 102 | - * @param mixed $value The object to store in the ArrayObject. |
|
| 103 | - * |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - public static function set($index, $value) { |
|
| 107 | - $instance = self::getInstance(); |
|
| 108 | - $instance->offsetSet($index, $value); |
|
| 109 | - } |
|
| 93 | + /** |
|
| 94 | + * setter method, basically same as offsetSet(). |
|
| 95 | + * |
|
| 96 | + * This method can be called from an object of type Registry, or it |
|
| 97 | + * can be called statically. In the latter case, it uses the default |
|
| 98 | + * static instance stored in the class. |
|
| 99 | + * |
|
| 100 | + * @param string $index The location in the ArrayObject in which to store |
|
| 101 | + * the value. |
|
| 102 | + * @param mixed $value The object to store in the ArrayObject. |
|
| 103 | + * |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + public static function set($index, $value) { |
|
| 107 | + $instance = self::getInstance(); |
|
| 108 | + $instance->offsetSet($index, $value); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Returns TRUE if the $index is a named value in the registry, |
|
| 113 | - * or FALSE if $index was not found in the registry. |
|
| 114 | - * |
|
| 115 | - * @param string $index |
|
| 116 | - * |
|
| 117 | - * @return boolean |
|
| 118 | - */ |
|
| 119 | - public static function isRegistered($index) { |
|
| 120 | - if (self::$_registry === null) { |
|
| 121 | - return false; |
|
| 122 | - } |
|
| 111 | + /** |
|
| 112 | + * Returns TRUE if the $index is a named value in the registry, |
|
| 113 | + * or FALSE if $index was not found in the registry. |
|
| 114 | + * |
|
| 115 | + * @param string $index |
|
| 116 | + * |
|
| 117 | + * @return boolean |
|
| 118 | + */ |
|
| 119 | + public static function isRegistered($index) { |
|
| 120 | + if (self::$_registry === null) { |
|
| 121 | + return false; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - return self::$_registry->offsetExists($index); |
|
| 125 | - } |
|
| 124 | + return self::$_registry->offsetExists($index); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * the constructor |
|
| 129 | - * |
|
| 130 | - * @param array $array data array |
|
| 131 | - * @param integer $flags ArrayObject flags |
|
| 132 | - */ |
|
| 133 | - public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS) { |
|
| 134 | - parent::__construct($array, $flags); |
|
| 135 | - } |
|
| 127 | + /** |
|
| 128 | + * the constructor |
|
| 129 | + * |
|
| 130 | + * @param array $array data array |
|
| 131 | + * @param integer $flags ArrayObject flags |
|
| 132 | + */ |
|
| 133 | + public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS) { |
|
| 134 | + parent::__construct($array, $flags); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * method to check if offset exists |
|
| 139 | - * |
|
| 140 | - * @param string $index |
|
| 141 | - * |
|
| 142 | - * @returns mixed |
|
| 143 | - * |
|
| 144 | - * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960). |
|
| 145 | - */ |
|
| 146 | - public function offsetExists($index) { |
|
| 147 | - return array_key_exists($index, $this); |
|
| 148 | - } |
|
| 137 | + /** |
|
| 138 | + * method to check if offset exists |
|
| 139 | + * |
|
| 140 | + * @param string $index |
|
| 141 | + * |
|
| 142 | + * @returns mixed |
|
| 143 | + * |
|
| 144 | + * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960). |
|
| 145 | + */ |
|
| 146 | + public function offsetExists($index) { |
|
| 147 | + return array_key_exists($index, $this); |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * the Router class |
|
| 4 | - */ |
|
| 3 | + * the Router class |
|
| 4 | + */ |
|
| 5 | 5 | |
| 6 | 6 | namespace Phile\Core; |
| 7 | 7 | |
@@ -15,138 +15,138 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class Router { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var array with $_SERVER environment |
|
| 20 | - */ |
|
| 21 | - protected $server; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @param array $server $_SERVER environment |
|
| 25 | - */ |
|
| 26 | - public function __construct(array $server = []) { |
|
| 27 | - if (empty($server)) { |
|
| 28 | - $server = $_SERVER; |
|
| 29 | - } |
|
| 30 | - $this->server = $server; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * get request-URL relative to Phile base-URL |
|
| 35 | - * |
|
| 36 | - * @return string relative URL e.g. `index`, `sub/`, `sub/page` |
|
| 37 | - */ |
|
| 38 | - public function getCurrentUrl() { |
|
| 39 | - $url = $this->server['REQUEST_URI']; |
|
| 40 | - |
|
| 41 | - // remove query string |
|
| 42 | - $queryPosition = strpos($url, '?'); |
|
| 43 | - if ($queryPosition) { |
|
| 44 | - $url = substr($url, 0, $queryPosition); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - // resolve root-relative URL-path |
|
| 48 | - $baseUrl = $this->getBaseUrl(); |
|
| 49 | - $basePath = $this->getUrlPath($baseUrl); |
|
| 50 | - if (!empty($basePath) && strpos($url, $basePath) === 0) { |
|
| 51 | - $url = substr($url, strlen($basePath)); |
|
| 52 | - } |
|
| 53 | - $url = ltrim($url, '/'); |
|
| 54 | - |
|
| 55 | - $url = urldecode($url); |
|
| 56 | - |
|
| 57 | - return $url; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Get base-URL of the Phile installation |
|
| 62 | - * |
|
| 63 | - * @return string `scheme://host/path/phile-root` |
|
| 64 | - */ |
|
| 65 | - public function getBaseUrl() { |
|
| 66 | - if (Registry::isRegistered('Phile_Settings')) { |
|
| 67 | - $config = Registry::get('Phile_Settings'); |
|
| 68 | - if (!empty($config['base_url'])) { |
|
| 69 | - return $config['base_url']; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $url = ''; |
|
| 74 | - |
|
| 75 | - if (isset($this->server['PHP_SELF'])) { |
|
| 76 | - $url = preg_replace('/index\.php(.*)?$/', '', $this->server['PHP_SELF']); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - if (isset($this->server['HTTP_HOST'])) { |
|
| 80 | - $host = $this->server['HTTP_HOST']; |
|
| 81 | - $protocol = $this->getProtocol(); |
|
| 82 | - $url = $protocol . '://' . $host . $url; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $url = rtrim($url, '/'); |
|
| 86 | - return $url; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * get the URL for a page-Id |
|
| 91 | - * |
|
| 92 | - * e.g. `sub/index` --> `http://host/phile-root/sub` |
|
| 93 | - * |
|
| 94 | - * @param string $pageId |
|
| 95 | - * @param bool $base return a full or root-relative URL |
|
| 96 | - * @return string URL |
|
| 97 | - */ |
|
| 98 | - public function urlForPage($pageId, $base = true) { |
|
| 99 | - $url = $pageId; |
|
| 100 | - if ($base) { |
|
| 101 | - $url = $this->url($url); |
|
| 102 | - } |
|
| 103 | - return $url; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * converts Phile-root relative URL to full URL |
|
| 108 | - * |
|
| 109 | - * e.g. `foo/bar.ext` --> `http://host/phile-root/foo/bar.ext` |
|
| 110 | - * |
|
| 111 | - * @param string $url |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function url($url) { |
|
| 115 | - return $this->getBaseUrl() . '/' . ltrim($url, '/'); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * get the HTTP-protocol |
|
| 120 | - * |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function getProtocol() { |
|
| 124 | - if (empty($this->server['HTTP_HOST'])) { |
|
| 125 | - return null; |
|
| 126 | - } |
|
| 127 | - $protocol = 'http'; |
|
| 128 | - if (isset($this->server['HTTPS']) && strtolower($this->server['HTTPS']) !== 'off') { |
|
| 129 | - $protocol = 'https'; |
|
| 130 | - } |
|
| 131 | - return $protocol; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * get path of an URL |
|
| 136 | - * |
|
| 137 | - * `scheme://host/path/sub` --> `/path/sub` |
|
| 138 | - * |
|
| 139 | - * @param string $url |
|
| 140 | - * @return string |
|
| 141 | - */ |
|
| 142 | - protected function getUrlPath($url) { |
|
| 143 | - $path = ''; |
|
| 144 | - if (strpos($url, '://') !== false) { |
|
| 145 | - $parsed = parse_url($url); |
|
| 146 | - if (isset($parsed['path'])) { |
|
| 147 | - $path = $parsed['path']; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - return $path; |
|
| 151 | - } |
|
| 18 | + /** |
|
| 19 | + * @var array with $_SERVER environment |
|
| 20 | + */ |
|
| 21 | + protected $server; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @param array $server $_SERVER environment |
|
| 25 | + */ |
|
| 26 | + public function __construct(array $server = []) { |
|
| 27 | + if (empty($server)) { |
|
| 28 | + $server = $_SERVER; |
|
| 29 | + } |
|
| 30 | + $this->server = $server; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * get request-URL relative to Phile base-URL |
|
| 35 | + * |
|
| 36 | + * @return string relative URL e.g. `index`, `sub/`, `sub/page` |
|
| 37 | + */ |
|
| 38 | + public function getCurrentUrl() { |
|
| 39 | + $url = $this->server['REQUEST_URI']; |
|
| 40 | + |
|
| 41 | + // remove query string |
|
| 42 | + $queryPosition = strpos($url, '?'); |
|
| 43 | + if ($queryPosition) { |
|
| 44 | + $url = substr($url, 0, $queryPosition); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + // resolve root-relative URL-path |
|
| 48 | + $baseUrl = $this->getBaseUrl(); |
|
| 49 | + $basePath = $this->getUrlPath($baseUrl); |
|
| 50 | + if (!empty($basePath) && strpos($url, $basePath) === 0) { |
|
| 51 | + $url = substr($url, strlen($basePath)); |
|
| 52 | + } |
|
| 53 | + $url = ltrim($url, '/'); |
|
| 54 | + |
|
| 55 | + $url = urldecode($url); |
|
| 56 | + |
|
| 57 | + return $url; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Get base-URL of the Phile installation |
|
| 62 | + * |
|
| 63 | + * @return string `scheme://host/path/phile-root` |
|
| 64 | + */ |
|
| 65 | + public function getBaseUrl() { |
|
| 66 | + if (Registry::isRegistered('Phile_Settings')) { |
|
| 67 | + $config = Registry::get('Phile_Settings'); |
|
| 68 | + if (!empty($config['base_url'])) { |
|
| 69 | + return $config['base_url']; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $url = ''; |
|
| 74 | + |
|
| 75 | + if (isset($this->server['PHP_SELF'])) { |
|
| 76 | + $url = preg_replace('/index\.php(.*)?$/', '', $this->server['PHP_SELF']); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + if (isset($this->server['HTTP_HOST'])) { |
|
| 80 | + $host = $this->server['HTTP_HOST']; |
|
| 81 | + $protocol = $this->getProtocol(); |
|
| 82 | + $url = $protocol . '://' . $host . $url; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $url = rtrim($url, '/'); |
|
| 86 | + return $url; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * get the URL for a page-Id |
|
| 91 | + * |
|
| 92 | + * e.g. `sub/index` --> `http://host/phile-root/sub` |
|
| 93 | + * |
|
| 94 | + * @param string $pageId |
|
| 95 | + * @param bool $base return a full or root-relative URL |
|
| 96 | + * @return string URL |
|
| 97 | + */ |
|
| 98 | + public function urlForPage($pageId, $base = true) { |
|
| 99 | + $url = $pageId; |
|
| 100 | + if ($base) { |
|
| 101 | + $url = $this->url($url); |
|
| 102 | + } |
|
| 103 | + return $url; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * converts Phile-root relative URL to full URL |
|
| 108 | + * |
|
| 109 | + * e.g. `foo/bar.ext` --> `http://host/phile-root/foo/bar.ext` |
|
| 110 | + * |
|
| 111 | + * @param string $url |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function url($url) { |
|
| 115 | + return $this->getBaseUrl() . '/' . ltrim($url, '/'); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * get the HTTP-protocol |
|
| 120 | + * |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function getProtocol() { |
|
| 124 | + if (empty($this->server['HTTP_HOST'])) { |
|
| 125 | + return null; |
|
| 126 | + } |
|
| 127 | + $protocol = 'http'; |
|
| 128 | + if (isset($this->server['HTTPS']) && strtolower($this->server['HTTPS']) !== 'off') { |
|
| 129 | + $protocol = 'https'; |
|
| 130 | + } |
|
| 131 | + return $protocol; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * get path of an URL |
|
| 136 | + * |
|
| 137 | + * `scheme://host/path/sub` --> `/path/sub` |
|
| 138 | + * |
|
| 139 | + * @param string $url |
|
| 140 | + * @return string |
|
| 141 | + */ |
|
| 142 | + protected function getUrlPath($url) { |
|
| 143 | + $path = ''; |
|
| 144 | + if (strpos($url, '://') !== false) { |
|
| 145 | + $parsed = parse_url($url); |
|
| 146 | + if (isset($parsed['path'])) { |
|
| 147 | + $path = $parsed['path']; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + return $path; |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -22,119 +22,119 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Response { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var string HTTP body |
|
| 27 | - */ |
|
| 28 | - protected $body = ''; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var string charset |
|
| 32 | - */ |
|
| 33 | - protected $charset = 'utf-8'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var array HTTP-headers |
|
| 37 | - */ |
|
| 38 | - protected $headers = []; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var int HTTP status code |
|
| 42 | - */ |
|
| 43 | - protected $statusCode = 200; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * redirect to another URL |
|
| 47 | - * |
|
| 48 | - * @param string $url URL |
|
| 49 | - * @param int $statusCode |
|
| 50 | - */ |
|
| 51 | - public function redirect($url, $statusCode = 302) { |
|
| 52 | - $this->setStatusCode($statusCode) |
|
| 53 | - ->setHeader('Location', $url, true) |
|
| 54 | - ->setBody('') |
|
| 55 | - ->send() |
|
| 56 | - ->stop(); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * set the response body |
|
| 61 | - * |
|
| 62 | - * @param $body |
|
| 63 | - * @return $this |
|
| 64 | - */ |
|
| 65 | - public function setBody($body) { |
|
| 66 | - $this->body = $body; |
|
| 67 | - return $this; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * set the response character-set |
|
| 72 | - * |
|
| 73 | - * @param $charset |
|
| 74 | - * @return $this |
|
| 75 | - */ |
|
| 76 | - public function setCharset($charset) { |
|
| 77 | - $this->charset = $charset; |
|
| 78 | - return $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * set a response HTTP-header |
|
| 83 | - * |
|
| 84 | - * @param string $key |
|
| 85 | - * @param string $value |
|
| 86 | - * @param bool $clear clear out any existing headers |
|
| 87 | - * @return $this |
|
| 88 | - */ |
|
| 89 | - public function setHeader($key, $value, $clear = false) { |
|
| 90 | - if ($clear) { |
|
| 91 | - $this->headers = []; |
|
| 92 | - } |
|
| 93 | - $this->headers[$key] = "$key: $value"; |
|
| 94 | - return $this; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * set the response HTTP status code |
|
| 99 | - * |
|
| 100 | - * @param $code |
|
| 101 | - * @return $this |
|
| 102 | - */ |
|
| 103 | - public function setStatusCode($code) { |
|
| 104 | - $this->statusCode = $code; |
|
| 105 | - return $this; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * sends the HTTP response |
|
| 110 | - * |
|
| 111 | - * @return $this |
|
| 112 | - */ |
|
| 113 | - public function send() { |
|
| 114 | - if (!isset($this->headers['Content-Type'])) { |
|
| 115 | - $this->setHeader('Content-Type', 'text/html; charset=' . $this->charset); |
|
| 116 | - } |
|
| 117 | - $this->outputHeader(); |
|
| 118 | - http_response_code($this->statusCode); |
|
| 119 | - echo $this->body; |
|
| 120 | - return $this; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * helper for easy testing |
|
| 125 | - */ |
|
| 126 | - public function stop() { |
|
| 127 | - die(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * output all set response headers |
|
| 132 | - */ |
|
| 133 | - protected function outputHeader() { |
|
| 134 | - foreach ($this->headers as $header) { |
|
| 135 | - header($header); |
|
| 136 | - } |
|
| 137 | - } |
|
| 25 | + /** |
|
| 26 | + * @var string HTTP body |
|
| 27 | + */ |
|
| 28 | + protected $body = ''; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var string charset |
|
| 32 | + */ |
|
| 33 | + protected $charset = 'utf-8'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var array HTTP-headers |
|
| 37 | + */ |
|
| 38 | + protected $headers = []; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var int HTTP status code |
|
| 42 | + */ |
|
| 43 | + protected $statusCode = 200; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * redirect to another URL |
|
| 47 | + * |
|
| 48 | + * @param string $url URL |
|
| 49 | + * @param int $statusCode |
|
| 50 | + */ |
|
| 51 | + public function redirect($url, $statusCode = 302) { |
|
| 52 | + $this->setStatusCode($statusCode) |
|
| 53 | + ->setHeader('Location', $url, true) |
|
| 54 | + ->setBody('') |
|
| 55 | + ->send() |
|
| 56 | + ->stop(); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * set the response body |
|
| 61 | + * |
|
| 62 | + * @param $body |
|
| 63 | + * @return $this |
|
| 64 | + */ |
|
| 65 | + public function setBody($body) { |
|
| 66 | + $this->body = $body; |
|
| 67 | + return $this; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * set the response character-set |
|
| 72 | + * |
|
| 73 | + * @param $charset |
|
| 74 | + * @return $this |
|
| 75 | + */ |
|
| 76 | + public function setCharset($charset) { |
|
| 77 | + $this->charset = $charset; |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * set a response HTTP-header |
|
| 83 | + * |
|
| 84 | + * @param string $key |
|
| 85 | + * @param string $value |
|
| 86 | + * @param bool $clear clear out any existing headers |
|
| 87 | + * @return $this |
|
| 88 | + */ |
|
| 89 | + public function setHeader($key, $value, $clear = false) { |
|
| 90 | + if ($clear) { |
|
| 91 | + $this->headers = []; |
|
| 92 | + } |
|
| 93 | + $this->headers[$key] = "$key: $value"; |
|
| 94 | + return $this; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * set the response HTTP status code |
|
| 99 | + * |
|
| 100 | + * @param $code |
|
| 101 | + * @return $this |
|
| 102 | + */ |
|
| 103 | + public function setStatusCode($code) { |
|
| 104 | + $this->statusCode = $code; |
|
| 105 | + return $this; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * sends the HTTP response |
|
| 110 | + * |
|
| 111 | + * @return $this |
|
| 112 | + */ |
|
| 113 | + public function send() { |
|
| 114 | + if (!isset($this->headers['Content-Type'])) { |
|
| 115 | + $this->setHeader('Content-Type', 'text/html; charset=' . $this->charset); |
|
| 116 | + } |
|
| 117 | + $this->outputHeader(); |
|
| 118 | + http_response_code($this->statusCode); |
|
| 119 | + echo $this->body; |
|
| 120 | + return $this; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * helper for easy testing |
|
| 125 | + */ |
|
| 126 | + public function stop() { |
|
| 127 | + die(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * output all set response headers |
|
| 132 | + */ |
|
| 133 | + protected function outputHeader() { |
|
| 134 | + foreach ($this->headers as $header) { |
|
| 135 | + header($header); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |
@@ -14,64 +14,64 @@ |
||
| 14 | 14 | * @package Phile\Core |
| 15 | 15 | */ |
| 16 | 16 | class ServiceLocator { |
| 17 | - /** |
|
| 18 | - * @var array of services |
|
| 19 | - */ |
|
| 20 | - protected static $services; |
|
| 17 | + /** |
|
| 18 | + * @var array of services |
|
| 19 | + */ |
|
| 20 | + protected static $services; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var array $serviceMap for mapping speaking names/keys to the interfaces |
|
| 24 | - */ |
|
| 25 | - protected static $serviceMap = array( |
|
| 26 | - 'Phile_Cache' => 'Phile\ServiceLocator\CacheInterface', |
|
| 27 | - 'Phile_Template' => 'Phile\ServiceLocator\TemplateInterface', |
|
| 28 | - 'Phile_Parser' => 'Phile\ServiceLocator\ParserInterface', |
|
| 29 | - 'Phile_Data_Persistence' => 'Phile\ServiceLocator\PersistenceInterface', |
|
| 30 | - 'Phile_Parser_Meta' => 'Phile\ServiceLocator\MetaInterface', |
|
| 31 | - 'Phile_ErrorHandler' => 'Phile\ServiceLocator\ErrorHandlerInterface', |
|
| 32 | - ); |
|
| 22 | + /** |
|
| 23 | + * @var array $serviceMap for mapping speaking names/keys to the interfaces |
|
| 24 | + */ |
|
| 25 | + protected static $serviceMap = array( |
|
| 26 | + 'Phile_Cache' => 'Phile\ServiceLocator\CacheInterface', |
|
| 27 | + 'Phile_Template' => 'Phile\ServiceLocator\TemplateInterface', |
|
| 28 | + 'Phile_Parser' => 'Phile\ServiceLocator\ParserInterface', |
|
| 29 | + 'Phile_Data_Persistence' => 'Phile\ServiceLocator\PersistenceInterface', |
|
| 30 | + 'Phile_Parser_Meta' => 'Phile\ServiceLocator\MetaInterface', |
|
| 31 | + 'Phile_ErrorHandler' => 'Phile\ServiceLocator\ErrorHandlerInterface', |
|
| 32 | + ); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * method to register a service |
|
| 36 | - * |
|
| 37 | - * @param string $serviceKey the key for the service |
|
| 38 | - * @param mixed $object |
|
| 39 | - * |
|
| 40 | - * @throws ServiceLocatorException |
|
| 41 | - */ |
|
| 42 | - public static function registerService($serviceKey, $object) { |
|
| 43 | - $interfaces = class_implements($object); |
|
| 44 | - $interface = self::$serviceMap[$serviceKey]; |
|
| 45 | - if ($interfaces === false || !in_array($interface, $interfaces)) { |
|
| 46 | - throw new ServiceLocatorException("the object must implement the interface: '{$interface}'", 1398536617); |
|
| 47 | - } |
|
| 48 | - self::$services[$serviceKey] = $object; |
|
| 49 | - } |
|
| 34 | + /** |
|
| 35 | + * method to register a service |
|
| 36 | + * |
|
| 37 | + * @param string $serviceKey the key for the service |
|
| 38 | + * @param mixed $object |
|
| 39 | + * |
|
| 40 | + * @throws ServiceLocatorException |
|
| 41 | + */ |
|
| 42 | + public static function registerService($serviceKey, $object) { |
|
| 43 | + $interfaces = class_implements($object); |
|
| 44 | + $interface = self::$serviceMap[$serviceKey]; |
|
| 45 | + if ($interfaces === false || !in_array($interface, $interfaces)) { |
|
| 46 | + throw new ServiceLocatorException("the object must implement the interface: '{$interface}'", 1398536617); |
|
| 47 | + } |
|
| 48 | + self::$services[$serviceKey] = $object; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * checks if a service is registered |
|
| 53 | - * |
|
| 54 | - * @param string $serviceKey |
|
| 55 | - * |
|
| 56 | - * @return bool |
|
| 57 | - */ |
|
| 58 | - public static function hasService($serviceKey) { |
|
| 59 | - return (isset(self::$services[$serviceKey])); |
|
| 60 | - } |
|
| 51 | + /** |
|
| 52 | + * checks if a service is registered |
|
| 53 | + * |
|
| 54 | + * @param string $serviceKey |
|
| 55 | + * |
|
| 56 | + * @return bool |
|
| 57 | + */ |
|
| 58 | + public static function hasService($serviceKey) { |
|
| 59 | + return (isset(self::$services[$serviceKey])); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * returns a service |
|
| 64 | - * |
|
| 65 | - * @param string $serviceKey the service key |
|
| 66 | - * |
|
| 67 | - * @return mixed |
|
| 68 | - * @throws ServiceLocatorException |
|
| 69 | - */ |
|
| 70 | - public static function getService($serviceKey) { |
|
| 71 | - if (!isset(self::$services[$serviceKey])) { |
|
| 72 | - throw new ServiceLocatorException("the service '{$serviceKey}' is not registered", 1398536637); |
|
| 73 | - } |
|
| 62 | + /** |
|
| 63 | + * returns a service |
|
| 64 | + * |
|
| 65 | + * @param string $serviceKey the service key |
|
| 66 | + * |
|
| 67 | + * @return mixed |
|
| 68 | + * @throws ServiceLocatorException |
|
| 69 | + */ |
|
| 70 | + public static function getService($serviceKey) { |
|
| 71 | + if (!isset(self::$services[$serviceKey])) { |
|
| 72 | + throw new ServiceLocatorException("the service '{$serviceKey}' is not registered", 1398536637); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - return self::$services[$serviceKey]; |
|
| 76 | - } |
|
| 75 | + return self::$services[$serviceKey]; |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * The Event class |
|
| 4 | - */ |
|
| 3 | + * The Event class |
|
| 4 | + */ |
|
| 5 | 5 | namespace Phile\Core; |
| 6 | 6 | |
| 7 | 7 | use Phile\Gateway\EventObserverInterface; |
@@ -15,44 +15,44 @@ discard block |
||
| 15 | 15 | * @package Phile\Core |
| 16 | 16 | */ |
| 17 | 17 | class Event { |
| 18 | - /** |
|
| 19 | - * Registry object provides storage for objects. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected static $_registry = []; |
|
| 18 | + /** |
|
| 19 | + * Registry object provides storage for objects. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected static $_registry = []; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * method to register an event |
|
| 27 | - * |
|
| 28 | - * @param string $eventName the event to observe |
|
| 29 | - * @param EventObserverInterface|callable $object observer |
|
| 30 | - */ |
|
| 31 | - public static function registerEvent($eventName, $object) { |
|
| 32 | - if ($object instanceof EventObserverInterface) { |
|
| 33 | - $object = [$object, 'on']; |
|
| 34 | - } |
|
| 35 | - if (!is_callable($object)) { |
|
| 36 | - throw new \InvalidArgumentException( |
|
| 37 | - "Can't register event. Observer is not callable.", |
|
| 38 | - 1427814905 |
|
| 39 | - ); |
|
| 40 | - } |
|
| 41 | - self::$_registry[$eventName][] = $object; |
|
| 42 | - } |
|
| 25 | + /** |
|
| 26 | + * method to register an event |
|
| 27 | + * |
|
| 28 | + * @param string $eventName the event to observe |
|
| 29 | + * @param EventObserverInterface|callable $object observer |
|
| 30 | + */ |
|
| 31 | + public static function registerEvent($eventName, $object) { |
|
| 32 | + if ($object instanceof EventObserverInterface) { |
|
| 33 | + $object = [$object, 'on']; |
|
| 34 | + } |
|
| 35 | + if (!is_callable($object)) { |
|
| 36 | + throw new \InvalidArgumentException( |
|
| 37 | + "Can't register event. Observer is not callable.", |
|
| 38 | + 1427814905 |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | + self::$_registry[$eventName][] = $object; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * method to trigger an event |
|
| 46 | - * |
|
| 47 | - * @param string $eventName the event name (register for this name) |
|
| 48 | - * @param array $data array with some additional data |
|
| 49 | - */ |
|
| 50 | - public static function triggerEvent($eventName, $data = null) { |
|
| 51 | - if (empty(self::$_registry[$eventName])) { |
|
| 52 | - return; |
|
| 53 | - } |
|
| 54 | - foreach (self::$_registry[$eventName] as $observer) { |
|
| 55 | - call_user_func_array($observer, [$eventName, $data]); |
|
| 56 | - } |
|
| 57 | - } |
|
| 44 | + /** |
|
| 45 | + * method to trigger an event |
|
| 46 | + * |
|
| 47 | + * @param string $eventName the event name (register for this name) |
|
| 48 | + * @param array $data array with some additional data |
|
| 49 | + */ |
|
| 50 | + public static function triggerEvent($eventName, $data = null) { |
|
| 51 | + if (empty(self::$_registry[$eventName])) { |
|
| 52 | + return; |
|
| 53 | + } |
|
| 54 | + foreach (self::$_registry[$eventName] as $observer) { |
|
| 55 | + call_user_func_array($observer, [$eventName, $data]); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * The Session class |
|
| 4 | - */ |
|
| 3 | + * The Session class |
|
| 4 | + */ |
|
| 5 | 5 | namespace Phile\Core; |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -13,109 +13,109 @@ discard block |
||
| 13 | 13 | * @package Phile\Core |
| 14 | 14 | */ |
| 15 | 15 | class Session { |
| 16 | - /** @var bool mark if session is started */ |
|
| 17 | - static public $isStarted = false; |
|
| 16 | + /** @var bool mark if session is started */ |
|
| 17 | + static public $isStarted = false; |
|
| 18 | 18 | |
| 19 | - /** @var string the session id */ |
|
| 20 | - static public $sessionId = ''; |
|
| 19 | + /** @var string the session id */ |
|
| 20 | + static public $sessionId = ''; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * method to start the session |
|
| 24 | - */ |
|
| 25 | - static public function start() { |
|
| 26 | - if (self::$isStarted === false) { |
|
| 27 | - session_cache_limiter('private'); |
|
| 28 | - session_cache_expire(120); |
|
| 29 | - if (session_start()) { |
|
| 30 | - self::$isStarted = true; |
|
| 31 | - } |
|
| 32 | - if (self::$isStarted) { |
|
| 33 | - if (PHILE_CLI_MODE) { |
|
| 34 | - $_SERVER['REMOTE_ADDR'] = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; |
|
| 35 | - } |
|
| 36 | - if (self::get('REMOTE_ADDR') != $_SERVER['REMOTE_ADDR']) { |
|
| 37 | - session_destroy(); |
|
| 38 | - session_start(); |
|
| 39 | - session_regenerate_id(); |
|
| 40 | - self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']); |
|
| 41 | - } |
|
| 42 | - if (self::get('REMOTE_ADDR') === null) { |
|
| 43 | - self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']); |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - self::$sessionId = session_id(); |
|
| 48 | - } |
|
| 22 | + /** |
|
| 23 | + * method to start the session |
|
| 24 | + */ |
|
| 25 | + static public function start() { |
|
| 26 | + if (self::$isStarted === false) { |
|
| 27 | + session_cache_limiter('private'); |
|
| 28 | + session_cache_expire(120); |
|
| 29 | + if (session_start()) { |
|
| 30 | + self::$isStarted = true; |
|
| 31 | + } |
|
| 32 | + if (self::$isStarted) { |
|
| 33 | + if (PHILE_CLI_MODE) { |
|
| 34 | + $_SERVER['REMOTE_ADDR'] = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; |
|
| 35 | + } |
|
| 36 | + if (self::get('REMOTE_ADDR') != $_SERVER['REMOTE_ADDR']) { |
|
| 37 | + session_destroy(); |
|
| 38 | + session_start(); |
|
| 39 | + session_regenerate_id(); |
|
| 40 | + self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']); |
|
| 41 | + } |
|
| 42 | + if (self::get('REMOTE_ADDR') === null) { |
|
| 43 | + self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + self::$sessionId = session_id(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * method to destroy the session |
|
| 52 | - */ |
|
| 53 | - static public function destroy() { |
|
| 54 | - unset($_SESSION); |
|
| 55 | - session_destroy(); |
|
| 56 | - } |
|
| 50 | + /** |
|
| 51 | + * method to destroy the session |
|
| 52 | + */ |
|
| 53 | + static public function destroy() { |
|
| 54 | + unset($_SESSION); |
|
| 55 | + session_destroy(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * method to save and close the session |
|
| 60 | - */ |
|
| 61 | - static public function save() { |
|
| 62 | - session_write_close(); |
|
| 63 | - } |
|
| 58 | + /** |
|
| 59 | + * method to save and close the session |
|
| 60 | + */ |
|
| 61 | + static public function save() { |
|
| 62 | + session_write_close(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * method to set value into session |
|
| 67 | - * |
|
| 68 | - * @param string $key |
|
| 69 | - * @param mixed $value |
|
| 70 | - */ |
|
| 71 | - static public function set($key, $value) { |
|
| 72 | - if (!self::$isStarted) { |
|
| 73 | - self::start(); |
|
| 74 | - } |
|
| 75 | - $_SESSION[$key] = $value; |
|
| 76 | - } |
|
| 65 | + /** |
|
| 66 | + * method to set value into session |
|
| 67 | + * |
|
| 68 | + * @param string $key |
|
| 69 | + * @param mixed $value |
|
| 70 | + */ |
|
| 71 | + static public function set($key, $value) { |
|
| 72 | + if (!self::$isStarted) { |
|
| 73 | + self::start(); |
|
| 74 | + } |
|
| 75 | + $_SESSION[$key] = $value; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * method to get value from session |
|
| 80 | - * |
|
| 81 | - * @param string $key |
|
| 82 | - * @param mixed $default |
|
| 83 | - * |
|
| 84 | - * @return null|mixed |
|
| 85 | - */ |
|
| 86 | - static public function get($key, $default = null) { |
|
| 87 | - if (!self::$isStarted) { |
|
| 88 | - self::start(); |
|
| 89 | - } |
|
| 78 | + /** |
|
| 79 | + * method to get value from session |
|
| 80 | + * |
|
| 81 | + * @param string $key |
|
| 82 | + * @param mixed $default |
|
| 83 | + * |
|
| 84 | + * @return null|mixed |
|
| 85 | + */ |
|
| 86 | + static public function get($key, $default = null) { |
|
| 87 | + if (!self::$isStarted) { |
|
| 88 | + self::start(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - return (self::isEmpty($key)) ? $default : $_SESSION[$key]; |
|
| 92 | - } |
|
| 91 | + return (self::isEmpty($key)) ? $default : $_SESSION[$key]; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * get the session id |
|
| 96 | - * |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - static public function getSessionId() { |
|
| 100 | - if (!self::$isStarted) { |
|
| 101 | - self::start(); |
|
| 102 | - } |
|
| 94 | + /** |
|
| 95 | + * get the session id |
|
| 96 | + * |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + static public function getSessionId() { |
|
| 100 | + if (!self::$isStarted) { |
|
| 101 | + self::start(); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - return self::$sessionId; |
|
| 105 | - } |
|
| 104 | + return self::$sessionId; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * check id key is empty/set or not |
|
| 109 | - * |
|
| 110 | - * @param $key |
|
| 111 | - * |
|
| 112 | - * @return bool |
|
| 113 | - */ |
|
| 114 | - static public function isEmpty($key) { |
|
| 115 | - if (!self::$isStarted) { |
|
| 116 | - self::start(); |
|
| 117 | - } |
|
| 107 | + /** |
|
| 108 | + * check id key is empty/set or not |
|
| 109 | + * |
|
| 110 | + * @param $key |
|
| 111 | + * |
|
| 112 | + * @return bool |
|
| 113 | + */ |
|
| 114 | + static public function isEmpty($key) { |
|
| 115 | + if (!self::$isStarted) { |
|
| 116 | + self::start(); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - return (!isset($_SESSION[$key])); |
|
| 120 | - } |
|
| 119 | + return (!isset($_SESSION[$key])); |
|
| 120 | + } |
|
| 121 | 121 | } |
@@ -16,172 +16,172 @@ |
||
| 16 | 16 | * @version 0.1 |
| 17 | 17 | */ |
| 18 | 18 | class Bootstrap { |
| 19 | - /** |
|
| 20 | - * @var \Phile\Bootstrap instance of Bootstrap class |
|
| 21 | - */ |
|
| 22 | - static protected $instance = NULL; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var array the settings array |
|
| 26 | - */ |
|
| 27 | - protected $settings; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var array the loaded plugins |
|
| 31 | - */ |
|
| 32 | - protected $plugins; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * the constructor |
|
| 36 | - * Disable direct creation of this object. |
|
| 37 | - */ |
|
| 38 | - protected function __construct() { |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Disable direct cloning of this object. |
|
| 43 | - */ |
|
| 44 | - protected function __clone() { |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Return instance of Bootstrap class as singleton |
|
| 49 | - * |
|
| 50 | - * @return Bootstrap |
|
| 51 | - */ |
|
| 52 | - static public function getInstance() { |
|
| 53 | - if (is_null(static::$instance)) { |
|
| 54 | - self::$instance = new Bootstrap(); |
|
| 55 | - } |
|
| 56 | - return static::$instance; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * initialize basics |
|
| 61 | - */ |
|
| 62 | - public function initializeBasics() { |
|
| 63 | - $this->initializeDefinitions(); |
|
| 64 | - $this->initializeAutoloader(); |
|
| 65 | - $this->initializeConfiguration(); |
|
| 66 | - $this->initializeFilesAndFolders(); |
|
| 67 | - $this->initializePlugins(); |
|
| 68 | - return $this; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * initialize the global definitions |
|
| 73 | - */ |
|
| 74 | - protected function initializeDefinitions() { |
|
| 75 | - // for php unit testings, we need to check if constant is defined |
|
| 76 | - // before setting them, because there is a bug in PHPUnit which |
|
| 77 | - // init our bootstrap multiple times. |
|
| 78 | - defined('PHILE_VERSION') or define('PHILE_VERSION', '1.6.0'); |
|
| 79 | - defined('PHILE_CLI_MODE') or define('PHILE_CLI_MODE', (php_sapi_name() == "cli") ? true : false); |
|
| 80 | - defined('ROOT_DIR') or define('ROOT_DIR', realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); |
|
| 81 | - defined('CONTENT_DIR') or define('CONTENT_DIR', ROOT_DIR . 'content' . DIRECTORY_SEPARATOR); |
|
| 82 | - defined('CONTENT_EXT') or define('CONTENT_EXT', '.md'); |
|
| 83 | - defined('LIB_DIR') or define('LIB_DIR', ROOT_DIR . 'lib' . DIRECTORY_SEPARATOR); |
|
| 84 | - defined('PLUGINS_DIR') or define('PLUGINS_DIR', ROOT_DIR . 'plugins' . DIRECTORY_SEPARATOR); |
|
| 85 | - defined('THEMES_DIR') or define('THEMES_DIR', ROOT_DIR . 'themes' . DIRECTORY_SEPARATOR); |
|
| 86 | - defined('CACHE_DIR') or define('CACHE_DIR', LIB_DIR . 'cache' . DIRECTORY_SEPARATOR); |
|
| 87 | - defined('STORAGE_DIR') or define('STORAGE_DIR', LIB_DIR . 'datastorage' . DIRECTORY_SEPARATOR); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * initialize the autoloader |
|
| 92 | - */ |
|
| 93 | - protected function initializeAutoloader() { |
|
| 94 | - spl_autoload_extensions(".php"); |
|
| 95 | - // load phile core |
|
| 96 | - spl_autoload_register(function ($className) { |
|
| 97 | - $fileName = LIB_DIR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php'; |
|
| 98 | - if (file_exists($fileName)) { |
|
| 99 | - require_once $fileName; |
|
| 100 | - } |
|
| 101 | - }); |
|
| 102 | - // load phile plugins |
|
| 103 | - spl_autoload_register('\Phile\Plugin\PluginRepository::autoload'); |
|
| 104 | - |
|
| 105 | - require(LIB_DIR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * initialize configuration |
|
| 110 | - */ |
|
| 111 | - protected function initializeConfiguration() { |
|
| 112 | - $defaults = Utility::load(ROOT_DIR . 'default_config.php'); |
|
| 113 | - $localSettings = Utility::load(ROOT_DIR . 'config.php'); |
|
| 114 | - if (is_array($localSettings)) { |
|
| 115 | - $this->settings = array_replace_recursive($defaults, $localSettings); |
|
| 116 | - } else { |
|
| 117 | - $this->settings = $defaults; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - Registry::set('Phile_Settings', $this->settings); |
|
| 121 | - date_default_timezone_set($this->settings['timezone']); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * auto-setup of files and folders |
|
| 126 | - */ |
|
| 127 | - protected function initializeFilesAndFolders() { |
|
| 128 | - $dirs = [ |
|
| 129 | - ['path' => CACHE_DIR], |
|
| 130 | - ['path' => STORAGE_DIR] |
|
| 131 | - ]; |
|
| 132 | - $defaults = ['protected' => true]; |
|
| 133 | - |
|
| 134 | - foreach ($dirs as $dir) { |
|
| 135 | - $dir += $defaults; |
|
| 136 | - $path = $dir['path']; |
|
| 137 | - if (empty($path) || strpos($path, ROOT_DIR) !== 0) { |
|
| 138 | - continue; |
|
| 139 | - } |
|
| 140 | - if (!file_exists($path)) { |
|
| 141 | - mkdir($path, 0775, true); |
|
| 142 | - } |
|
| 143 | - if ($dir['protected']) { |
|
| 144 | - $file = "$path.htaccess"; |
|
| 145 | - if (!file_exists($file)) { |
|
| 146 | - $content = "order deny,allow\ndeny from all\nallow from 127.0.0.1"; |
|
| 147 | - file_put_contents($file, $content); |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * initialize plugins |
|
| 155 | - * |
|
| 156 | - * @throws Exception\PluginException |
|
| 157 | - */ |
|
| 158 | - protected function initializePlugins() { |
|
| 159 | - $loader = new PluginRepository(); |
|
| 160 | - if (isset($this->settings['plugins']) && is_array($this->settings['plugins'])) { |
|
| 161 | - $this->plugins = $loader->loadAll($this->settings['plugins']); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - Event::triggerEvent('plugins_loaded', ['plugins' => $this->plugins]); |
|
| 165 | - |
|
| 166 | - // throw not earlier to have the error-handler plugin loaded |
|
| 167 | - // and initialized (by 'plugins_loaded' event) |
|
| 168 | - $errors = $loader->getLoadErrors(); |
|
| 169 | - if (count($errors) > 0) { |
|
| 170 | - throw new PluginException($errors[0]['message'], $errors[0]['code']); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // settings now include initialized plugin-configs |
|
| 174 | - $this->settings = Registry::get('Phile_Settings'); |
|
| 175 | - Event::triggerEvent('config_loaded', ['config' => $this->settings]); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * method to get plugins |
|
| 180 | - * @return array |
|
| 181 | - * @deprecated since 1.5 will be removed |
|
| 182 | - * @use 'plugins_loaded' event |
|
| 183 | - */ |
|
| 184 | - public function getPlugins() { |
|
| 185 | - return $this->plugins; |
|
| 186 | - } |
|
| 19 | + /** |
|
| 20 | + * @var \Phile\Bootstrap instance of Bootstrap class |
|
| 21 | + */ |
|
| 22 | + static protected $instance = NULL; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var array the settings array |
|
| 26 | + */ |
|
| 27 | + protected $settings; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var array the loaded plugins |
|
| 31 | + */ |
|
| 32 | + protected $plugins; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * the constructor |
|
| 36 | + * Disable direct creation of this object. |
|
| 37 | + */ |
|
| 38 | + protected function __construct() { |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Disable direct cloning of this object. |
|
| 43 | + */ |
|
| 44 | + protected function __clone() { |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Return instance of Bootstrap class as singleton |
|
| 49 | + * |
|
| 50 | + * @return Bootstrap |
|
| 51 | + */ |
|
| 52 | + static public function getInstance() { |
|
| 53 | + if (is_null(static::$instance)) { |
|
| 54 | + self::$instance = new Bootstrap(); |
|
| 55 | + } |
|
| 56 | + return static::$instance; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * initialize basics |
|
| 61 | + */ |
|
| 62 | + public function initializeBasics() { |
|
| 63 | + $this->initializeDefinitions(); |
|
| 64 | + $this->initializeAutoloader(); |
|
| 65 | + $this->initializeConfiguration(); |
|
| 66 | + $this->initializeFilesAndFolders(); |
|
| 67 | + $this->initializePlugins(); |
|
| 68 | + return $this; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * initialize the global definitions |
|
| 73 | + */ |
|
| 74 | + protected function initializeDefinitions() { |
|
| 75 | + // for php unit testings, we need to check if constant is defined |
|
| 76 | + // before setting them, because there is a bug in PHPUnit which |
|
| 77 | + // init our bootstrap multiple times. |
|
| 78 | + defined('PHILE_VERSION') or define('PHILE_VERSION', '1.6.0'); |
|
| 79 | + defined('PHILE_CLI_MODE') or define('PHILE_CLI_MODE', (php_sapi_name() == "cli") ? true : false); |
|
| 80 | + defined('ROOT_DIR') or define('ROOT_DIR', realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); |
|
| 81 | + defined('CONTENT_DIR') or define('CONTENT_DIR', ROOT_DIR . 'content' . DIRECTORY_SEPARATOR); |
|
| 82 | + defined('CONTENT_EXT') or define('CONTENT_EXT', '.md'); |
|
| 83 | + defined('LIB_DIR') or define('LIB_DIR', ROOT_DIR . 'lib' . DIRECTORY_SEPARATOR); |
|
| 84 | + defined('PLUGINS_DIR') or define('PLUGINS_DIR', ROOT_DIR . 'plugins' . DIRECTORY_SEPARATOR); |
|
| 85 | + defined('THEMES_DIR') or define('THEMES_DIR', ROOT_DIR . 'themes' . DIRECTORY_SEPARATOR); |
|
| 86 | + defined('CACHE_DIR') or define('CACHE_DIR', LIB_DIR . 'cache' . DIRECTORY_SEPARATOR); |
|
| 87 | + defined('STORAGE_DIR') or define('STORAGE_DIR', LIB_DIR . 'datastorage' . DIRECTORY_SEPARATOR); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * initialize the autoloader |
|
| 92 | + */ |
|
| 93 | + protected function initializeAutoloader() { |
|
| 94 | + spl_autoload_extensions(".php"); |
|
| 95 | + // load phile core |
|
| 96 | + spl_autoload_register(function ($className) { |
|
| 97 | + $fileName = LIB_DIR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php'; |
|
| 98 | + if (file_exists($fileName)) { |
|
| 99 | + require_once $fileName; |
|
| 100 | + } |
|
| 101 | + }); |
|
| 102 | + // load phile plugins |
|
| 103 | + spl_autoload_register('\Phile\Plugin\PluginRepository::autoload'); |
|
| 104 | + |
|
| 105 | + require(LIB_DIR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * initialize configuration |
|
| 110 | + */ |
|
| 111 | + protected function initializeConfiguration() { |
|
| 112 | + $defaults = Utility::load(ROOT_DIR . 'default_config.php'); |
|
| 113 | + $localSettings = Utility::load(ROOT_DIR . 'config.php'); |
|
| 114 | + if (is_array($localSettings)) { |
|
| 115 | + $this->settings = array_replace_recursive($defaults, $localSettings); |
|
| 116 | + } else { |
|
| 117 | + $this->settings = $defaults; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + Registry::set('Phile_Settings', $this->settings); |
|
| 121 | + date_default_timezone_set($this->settings['timezone']); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * auto-setup of files and folders |
|
| 126 | + */ |
|
| 127 | + protected function initializeFilesAndFolders() { |
|
| 128 | + $dirs = [ |
|
| 129 | + ['path' => CACHE_DIR], |
|
| 130 | + ['path' => STORAGE_DIR] |
|
| 131 | + ]; |
|
| 132 | + $defaults = ['protected' => true]; |
|
| 133 | + |
|
| 134 | + foreach ($dirs as $dir) { |
|
| 135 | + $dir += $defaults; |
|
| 136 | + $path = $dir['path']; |
|
| 137 | + if (empty($path) || strpos($path, ROOT_DIR) !== 0) { |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 140 | + if (!file_exists($path)) { |
|
| 141 | + mkdir($path, 0775, true); |
|
| 142 | + } |
|
| 143 | + if ($dir['protected']) { |
|
| 144 | + $file = "$path.htaccess"; |
|
| 145 | + if (!file_exists($file)) { |
|
| 146 | + $content = "order deny,allow\ndeny from all\nallow from 127.0.0.1"; |
|
| 147 | + file_put_contents($file, $content); |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * initialize plugins |
|
| 155 | + * |
|
| 156 | + * @throws Exception\PluginException |
|
| 157 | + */ |
|
| 158 | + protected function initializePlugins() { |
|
| 159 | + $loader = new PluginRepository(); |
|
| 160 | + if (isset($this->settings['plugins']) && is_array($this->settings['plugins'])) { |
|
| 161 | + $this->plugins = $loader->loadAll($this->settings['plugins']); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + Event::triggerEvent('plugins_loaded', ['plugins' => $this->plugins]); |
|
| 165 | + |
|
| 166 | + // throw not earlier to have the error-handler plugin loaded |
|
| 167 | + // and initialized (by 'plugins_loaded' event) |
|
| 168 | + $errors = $loader->getLoadErrors(); |
|
| 169 | + if (count($errors) > 0) { |
|
| 170 | + throw new PluginException($errors[0]['message'], $errors[0]['code']); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // settings now include initialized plugin-configs |
|
| 174 | + $this->settings = Registry::get('Phile_Settings'); |
|
| 175 | + Event::triggerEvent('config_loaded', ['config' => $this->settings]); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * method to get plugins |
|
| 180 | + * @return array |
|
| 181 | + * @deprecated since 1.5 will be removed |
|
| 182 | + * @use 'plugins_loaded' event |
|
| 183 | + */ |
|
| 184 | + public function getPlugins() { |
|
| 185 | + return $this->plugins; |
|
| 186 | + } |
|
| 187 | 187 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * the page repository class |
|
| 4 | - */ |
|
| 3 | + * the page repository class |
|
| 4 | + */ |
|
| 5 | 5 | namespace Phile\Repository; |
| 6 | 6 | |
| 7 | 7 | use Phile\Core\Registry; |
@@ -17,181 +17,181 @@ discard block |
||
| 17 | 17 | * @package Phile\Repository |
| 18 | 18 | */ |
| 19 | 19 | class Page { |
| 20 | - /** |
|
| 21 | - * @var array the settings array |
|
| 22 | - */ |
|
| 23 | - protected $settings; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var array object storage for initialized objects, to prevent multiple loading of objects. |
|
| 27 | - */ |
|
| 28 | - protected $storage = array(); |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var \Phile\ServiceLocator\CacheInterface the cache implementation |
|
| 32 | - */ |
|
| 33 | - protected $cache = null; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * the constructor |
|
| 37 | - */ |
|
| 38 | - public function __construct($settings = null) { |
|
| 39 | - if ($settings === null) { |
|
| 40 | - $settings = Registry::get('Phile_Settings'); |
|
| 41 | - } |
|
| 42 | - $this->settings = $settings; |
|
| 43 | - if (ServiceLocator::hasService('Phile_Cache')) { |
|
| 44 | - $this->cache = ServiceLocator::getService('Phile_Cache'); |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * find a page by path |
|
| 50 | - * |
|
| 51 | - * @param string $pageId |
|
| 52 | - * @param string $folder |
|
| 53 | - * |
|
| 54 | - * @return null|\Phile\Model\Page |
|
| 55 | - */ |
|
| 56 | - public function findByPath($pageId, $folder = CONTENT_DIR) { |
|
| 57 | - // be merciful to lazy third-party-usage and accept a leading slash |
|
| 58 | - $pageId = ltrim($pageId, '/'); |
|
| 59 | - // 'sub/' should serve page 'sub/index' |
|
| 60 | - if ($pageId === '' || substr($pageId, -1) === '/') { |
|
| 61 | - $pageId .= 'index'; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $file = $folder . $pageId . CONTENT_EXT; |
|
| 65 | - if (!file_exists($file)) { |
|
| 66 | - if (substr($pageId, -6) === '/index') { |
|
| 67 | - // try to resolve sub-directory 'sub/' to page 'sub' |
|
| 68 | - $pageId = substr($pageId, 0, strlen($pageId) - 6); |
|
| 69 | - } else { |
|
| 70 | - // try to resolve page 'sub' to sub-directory 'sub/' |
|
| 71 | - $pageId .= '/index'; |
|
| 72 | - } |
|
| 73 | - $file = $folder . $pageId . CONTENT_EXT; |
|
| 74 | - } |
|
| 75 | - if (!file_exists($file)) { |
|
| 76 | - return null; |
|
| 77 | - } |
|
| 78 | - return $this->getPage($file, $folder); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * find all pages (*.md) files and returns an array of Page models |
|
| 83 | - * |
|
| 84 | - * @param array $options |
|
| 85 | - * @param string $folder |
|
| 86 | - * |
|
| 87 | - * @return PageCollection of \Phile\Model\Page objects |
|
| 88 | - */ |
|
| 89 | - public function findAll(array $options = array(), $folder = CONTENT_DIR) { |
|
| 90 | - return new PageCollection(function() use ($options, $folder){ |
|
| 91 | - $options += $this->settings; |
|
| 92 | - // ignore files with a leading '.' in its filename |
|
| 93 | - $files = Utility::getFiles($folder, '\Phile\FilterIterator\ContentFileFilterIterator'); |
|
| 94 | - $pages = array(); |
|
| 95 | - foreach ($files as $file) { |
|
| 96 | - if (str_replace($folder, '', $file) == '404' . CONTENT_EXT) { |
|
| 97 | - // jump to next page if file is the 404 page |
|
| 98 | - continue; |
|
| 99 | - } |
|
| 100 | - $pages[] = $this->getPage($file, $folder); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if (empty($options['pages_order'])) { |
|
| 104 | - return $pages; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // parse search criteria |
|
| 108 | - $terms = preg_split('/\s+/', $options['pages_order'], -1, PREG_SPLIT_NO_EMPTY); |
|
| 109 | - foreach ($terms as $term) { |
|
| 110 | - $term = explode('.', $term); |
|
| 111 | - if (count($term) > 1) { |
|
| 112 | - $type = array_shift($term); |
|
| 113 | - } else { |
|
| 114 | - $type = null; |
|
| 115 | - } |
|
| 116 | - $term = explode(':', $term[0]); |
|
| 117 | - $sorting[] = array('type' => $type, 'key' => $term[0], 'order' => $term[1]); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // prepare search criteria for array_multisort |
|
| 121 | - foreach ($sorting as $sort) { |
|
| 122 | - $key = $sort['key']; |
|
| 123 | - $column = array(); |
|
| 124 | - foreach ($pages as $page) { |
|
| 125 | - /** @var \Phile\Model\Page $page */ |
|
| 126 | - $meta = $page->getMeta(); |
|
| 127 | - if ($sort['type'] === 'page') { |
|
| 128 | - $method = 'get' . ucfirst($key); |
|
| 129 | - $value = $page->$method(); |
|
| 130 | - } elseif ($sort['type'] === 'meta') { |
|
| 131 | - $value = $meta->get($key); |
|
| 132 | - } else { |
|
| 133 | - continue 2; // ignore unhandled search term |
|
| 134 | - } |
|
| 135 | - $column[] = $value; |
|
| 136 | - } |
|
| 137 | - $sortHelper[] = $column; |
|
| 138 | - $sortHelper[] = constant('SORT_' . strtoupper($sort['order'])); |
|
| 139 | - } |
|
| 140 | - $sortHelper[] = &$pages; |
|
| 141 | - |
|
| 142 | - call_user_func_array('array_multisort', $sortHelper); |
|
| 143 | - |
|
| 144 | - return $pages; |
|
| 145 | - }); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * return page at offset from $page in applied search order |
|
| 150 | - * |
|
| 151 | - * @param \Phile\Model\Page $page |
|
| 152 | - * @param int $offset |
|
| 153 | - * @return null|\Phile\Model\Page |
|
| 154 | - */ |
|
| 155 | - public function getPageOffset(\Phile\Model\Page $page, $offset = 0) { |
|
| 156 | - $pages = $this->findAll(); |
|
| 157 | - $order = array(); |
|
| 158 | - foreach ($pages as $p) { |
|
| 159 | - $order[] = $p->getFilePath(); |
|
| 160 | - } |
|
| 161 | - $key = array_search($page->getFilePath(), $order) + $offset; |
|
| 162 | - if (!isset($order[$key])) { |
|
| 163 | - return null; |
|
| 164 | - } |
|
| 165 | - return $this->getPage($order[$key]); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * get page from cache or filepath |
|
| 170 | - * |
|
| 171 | - * @param $filePath |
|
| 172 | - * @param string $folder |
|
| 173 | - * |
|
| 174 | - * @return mixed|\Phile\Model\Page |
|
| 175 | - */ |
|
| 176 | - protected function getPage($filePath, $folder = CONTENT_DIR) { |
|
| 177 | - $key = 'Phile_Model_Page_' . md5($filePath); |
|
| 178 | - if (isset($this->storage[$key])) { |
|
| 179 | - return $this->storage[$key]; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($this->cache !== null) { |
|
| 183 | - if ($this->cache->has($key)) { |
|
| 184 | - $page = $this->cache->get($key); |
|
| 185 | - } else { |
|
| 186 | - $page = new \Phile\Model\Page($filePath, $folder); |
|
| 187 | - $this->cache->set($key, $page); |
|
| 188 | - } |
|
| 189 | - } else { |
|
| 190 | - $page = new \Phile\Model\Page($filePath, $folder); |
|
| 191 | - } |
|
| 192 | - $this->storage[$key] = $page; |
|
| 193 | - |
|
| 194 | - return $page; |
|
| 195 | - } |
|
| 20 | + /** |
|
| 21 | + * @var array the settings array |
|
| 22 | + */ |
|
| 23 | + protected $settings; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var array object storage for initialized objects, to prevent multiple loading of objects. |
|
| 27 | + */ |
|
| 28 | + protected $storage = array(); |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var \Phile\ServiceLocator\CacheInterface the cache implementation |
|
| 32 | + */ |
|
| 33 | + protected $cache = null; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * the constructor |
|
| 37 | + */ |
|
| 38 | + public function __construct($settings = null) { |
|
| 39 | + if ($settings === null) { |
|
| 40 | + $settings = Registry::get('Phile_Settings'); |
|
| 41 | + } |
|
| 42 | + $this->settings = $settings; |
|
| 43 | + if (ServiceLocator::hasService('Phile_Cache')) { |
|
| 44 | + $this->cache = ServiceLocator::getService('Phile_Cache'); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * find a page by path |
|
| 50 | + * |
|
| 51 | + * @param string $pageId |
|
| 52 | + * @param string $folder |
|
| 53 | + * |
|
| 54 | + * @return null|\Phile\Model\Page |
|
| 55 | + */ |
|
| 56 | + public function findByPath($pageId, $folder = CONTENT_DIR) { |
|
| 57 | + // be merciful to lazy third-party-usage and accept a leading slash |
|
| 58 | + $pageId = ltrim($pageId, '/'); |
|
| 59 | + // 'sub/' should serve page 'sub/index' |
|
| 60 | + if ($pageId === '' || substr($pageId, -1) === '/') { |
|
| 61 | + $pageId .= 'index'; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $file = $folder . $pageId . CONTENT_EXT; |
|
| 65 | + if (!file_exists($file)) { |
|
| 66 | + if (substr($pageId, -6) === '/index') { |
|
| 67 | + // try to resolve sub-directory 'sub/' to page 'sub' |
|
| 68 | + $pageId = substr($pageId, 0, strlen($pageId) - 6); |
|
| 69 | + } else { |
|
| 70 | + // try to resolve page 'sub' to sub-directory 'sub/' |
|
| 71 | + $pageId .= '/index'; |
|
| 72 | + } |
|
| 73 | + $file = $folder . $pageId . CONTENT_EXT; |
|
| 74 | + } |
|
| 75 | + if (!file_exists($file)) { |
|
| 76 | + return null; |
|
| 77 | + } |
|
| 78 | + return $this->getPage($file, $folder); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * find all pages (*.md) files and returns an array of Page models |
|
| 83 | + * |
|
| 84 | + * @param array $options |
|
| 85 | + * @param string $folder |
|
| 86 | + * |
|
| 87 | + * @return PageCollection of \Phile\Model\Page objects |
|
| 88 | + */ |
|
| 89 | + public function findAll(array $options = array(), $folder = CONTENT_DIR) { |
|
| 90 | + return new PageCollection(function() use ($options, $folder){ |
|
| 91 | + $options += $this->settings; |
|
| 92 | + // ignore files with a leading '.' in its filename |
|
| 93 | + $files = Utility::getFiles($folder, '\Phile\FilterIterator\ContentFileFilterIterator'); |
|
| 94 | + $pages = array(); |
|
| 95 | + foreach ($files as $file) { |
|
| 96 | + if (str_replace($folder, '', $file) == '404' . CONTENT_EXT) { |
|
| 97 | + // jump to next page if file is the 404 page |
|
| 98 | + continue; |
|
| 99 | + } |
|
| 100 | + $pages[] = $this->getPage($file, $folder); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if (empty($options['pages_order'])) { |
|
| 104 | + return $pages; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // parse search criteria |
|
| 108 | + $terms = preg_split('/\s+/', $options['pages_order'], -1, PREG_SPLIT_NO_EMPTY); |
|
| 109 | + foreach ($terms as $term) { |
|
| 110 | + $term = explode('.', $term); |
|
| 111 | + if (count($term) > 1) { |
|
| 112 | + $type = array_shift($term); |
|
| 113 | + } else { |
|
| 114 | + $type = null; |
|
| 115 | + } |
|
| 116 | + $term = explode(':', $term[0]); |
|
| 117 | + $sorting[] = array('type' => $type, 'key' => $term[0], 'order' => $term[1]); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // prepare search criteria for array_multisort |
|
| 121 | + foreach ($sorting as $sort) { |
|
| 122 | + $key = $sort['key']; |
|
| 123 | + $column = array(); |
|
| 124 | + foreach ($pages as $page) { |
|
| 125 | + /** @var \Phile\Model\Page $page */ |
|
| 126 | + $meta = $page->getMeta(); |
|
| 127 | + if ($sort['type'] === 'page') { |
|
| 128 | + $method = 'get' . ucfirst($key); |
|
| 129 | + $value = $page->$method(); |
|
| 130 | + } elseif ($sort['type'] === 'meta') { |
|
| 131 | + $value = $meta->get($key); |
|
| 132 | + } else { |
|
| 133 | + continue 2; // ignore unhandled search term |
|
| 134 | + } |
|
| 135 | + $column[] = $value; |
|
| 136 | + } |
|
| 137 | + $sortHelper[] = $column; |
|
| 138 | + $sortHelper[] = constant('SORT_' . strtoupper($sort['order'])); |
|
| 139 | + } |
|
| 140 | + $sortHelper[] = &$pages; |
|
| 141 | + |
|
| 142 | + call_user_func_array('array_multisort', $sortHelper); |
|
| 143 | + |
|
| 144 | + return $pages; |
|
| 145 | + }); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * return page at offset from $page in applied search order |
|
| 150 | + * |
|
| 151 | + * @param \Phile\Model\Page $page |
|
| 152 | + * @param int $offset |
|
| 153 | + * @return null|\Phile\Model\Page |
|
| 154 | + */ |
|
| 155 | + public function getPageOffset(\Phile\Model\Page $page, $offset = 0) { |
|
| 156 | + $pages = $this->findAll(); |
|
| 157 | + $order = array(); |
|
| 158 | + foreach ($pages as $p) { |
|
| 159 | + $order[] = $p->getFilePath(); |
|
| 160 | + } |
|
| 161 | + $key = array_search($page->getFilePath(), $order) + $offset; |
|
| 162 | + if (!isset($order[$key])) { |
|
| 163 | + return null; |
|
| 164 | + } |
|
| 165 | + return $this->getPage($order[$key]); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * get page from cache or filepath |
|
| 170 | + * |
|
| 171 | + * @param $filePath |
|
| 172 | + * @param string $folder |
|
| 173 | + * |
|
| 174 | + * @return mixed|\Phile\Model\Page |
|
| 175 | + */ |
|
| 176 | + protected function getPage($filePath, $folder = CONTENT_DIR) { |
|
| 177 | + $key = 'Phile_Model_Page_' . md5($filePath); |
|
| 178 | + if (isset($this->storage[$key])) { |
|
| 179 | + return $this->storage[$key]; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($this->cache !== null) { |
|
| 183 | + if ($this->cache->has($key)) { |
|
| 184 | + $page = $this->cache->get($key); |
|
| 185 | + } else { |
|
| 186 | + $page = new \Phile\Model\Page($filePath, $folder); |
|
| 187 | + $this->cache->set($key, $page); |
|
| 188 | + } |
|
| 189 | + } else { |
|
| 190 | + $page = new \Phile\Model\Page($filePath, $folder); |
|
| 191 | + } |
|
| 192 | + $this->storage[$key] = $page; |
|
| 193 | + |
|
| 194 | + return $page; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | 197 | } |