@@ -1,201 +1,201 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * For application languages management |
|
| 29 | - */ |
|
| 30 | - class Lang extends BaseClass{ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * For application languages management |
|
| 29 | + */ |
|
| 30 | + class Lang extends BaseClass{ |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The supported available language for this application. |
|
| 34 | - * @example "en" => "english" |
|
| 35 | - * @see Lang::addLang() |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $availables = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The all messages language |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $languages = array(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The default language to use if can not |
|
| 48 | - * determine the client language |
|
| 49 | - * |
|
| 50 | - * @example $default = 'en' |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - protected $default = null; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The current client language |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $current = null; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Construct new Lang instance |
|
| 63 | - */ |
|
| 64 | - public function __construct(){ |
|
| 65 | - parent::__construct(); |
|
| 66 | - |
|
| 67 | - $this->default = get_config('default_language', 'en'); |
|
| 68 | - $this->logger->debug('Setting the supported languages'); |
|
| 32 | + /** |
|
| 33 | + * The supported available language for this application. |
|
| 34 | + * @example "en" => "english" |
|
| 35 | + * @see Lang::addLang() |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $availables = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The all messages language |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $languages = array(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The default language to use if can not |
|
| 48 | + * determine the client language |
|
| 49 | + * |
|
| 50 | + * @example $default = 'en' |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + protected $default = null; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The current client language |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $current = null; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Construct new Lang instance |
|
| 63 | + */ |
|
| 64 | + public function __construct(){ |
|
| 65 | + parent::__construct(); |
|
| 66 | + |
|
| 67 | + $this->default = get_config('default_language', 'en'); |
|
| 68 | + $this->logger->debug('Setting the supported languages'); |
|
| 69 | 69 | |
| 70 | - //add the supported languages ('key', 'display name') |
|
| 71 | - $languages = get_config('languages', null); |
|
| 72 | - if(! empty($languages)){ |
|
| 73 | - foreach($languages as $key => $displayName){ |
|
| 74 | - $this->addLang($key, $displayName); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - unset($languages); |
|
| 78 | - |
|
| 79 | - //if the language exists in cookie use it |
|
| 80 | - $cfgKey = get_config('language_cookie_name'); |
|
| 81 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 82 | - $objCookie = & class_loader('Cookie'); |
|
| 83 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 84 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
| 85 | - $this->current = $cookieLang; |
|
| 86 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 87 | - } |
|
| 88 | - else{ |
|
| 89 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 90 | - $this->current = $this->getDefault(); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Get the all languages messages |
|
| 96 | - * |
|
| 97 | - * @return array the language message list |
|
| 98 | - */ |
|
| 99 | - public function getAll(){ |
|
| 100 | - return $this->languages; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Set the language message |
|
| 105 | - * |
|
| 106 | - * @param string $key the language key to identify |
|
| 107 | - * @param string $value the language message value |
|
| 108 | - */ |
|
| 109 | - public function set($key, $value){ |
|
| 110 | - $this->languages[$key] = $value; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the language message for the given key. If can't find return the default value |
|
| 115 | - * |
|
| 116 | - * @param string $key the message language key |
|
| 117 | - * @param string $default the default value to return if can not found the language message key |
|
| 118 | - * |
|
| 119 | - * @return string the language message value |
|
| 120 | - */ |
|
| 121 | - public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 122 | - if(isset($this->languages[$key])){ |
|
| 123 | - return $this->languages[$key]; |
|
| 124 | - } |
|
| 125 | - $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 126 | - return $default; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Check whether the language file for given name exists |
|
| 131 | - * |
|
| 132 | - * @param string $language the language name like "fr", "en", etc. |
|
| 133 | - * |
|
| 134 | - * @return boolean true if the language directory exists, false or not |
|
| 135 | - */ |
|
| 136 | - public function isValid($language){ |
|
| 137 | - $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 138 | - foreach($searchDir as $dir){ |
|
| 139 | - if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 140 | - return true; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the default language value like "en" , "fr", etc. |
|
| 148 | - * |
|
| 149 | - * @return string the default language |
|
| 150 | - */ |
|
| 151 | - public function getDefault(){ |
|
| 152 | - return $this->default; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Get the current language defined by cookie or the default value |
|
| 157 | - * |
|
| 158 | - * @return string the current language |
|
| 159 | - */ |
|
| 160 | - public function getCurrent(){ |
|
| 161 | - return $this->current; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Add new supported or available language |
|
| 166 | - * |
|
| 167 | - * @param string $name the short language name like "en", "fr". |
|
| 168 | - * @param string $description the human readable description of this language |
|
| 169 | - */ |
|
| 170 | - public function addLang($name, $description){ |
|
| 171 | - if(isset($this->availables[$name])){ |
|
| 172 | - return; //already added cost in performance |
|
| 173 | - } |
|
| 174 | - if($this->isValid($name)){ |
|
| 175 | - $this->availables[$name] = $description; |
|
| 176 | - } |
|
| 177 | - else{ |
|
| 178 | - show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Get the list of the application supported language |
|
| 184 | - * |
|
| 185 | - * @return array the list of the application language |
|
| 186 | - */ |
|
| 187 | - public function getSupported(){ |
|
| 188 | - return $this->availables; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Add new language messages |
|
| 193 | - * |
|
| 194 | - * @param array $langs the languages array of the messages to be added |
|
| 195 | - */ |
|
| 196 | - public function addLangMessages(array $langs){ |
|
| 197 | - foreach ($langs as $key => $value) { |
|
| 198 | - $this->set($key, $value); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 70 | + //add the supported languages ('key', 'display name') |
|
| 71 | + $languages = get_config('languages', null); |
|
| 72 | + if(! empty($languages)){ |
|
| 73 | + foreach($languages as $key => $displayName){ |
|
| 74 | + $this->addLang($key, $displayName); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + unset($languages); |
|
| 78 | + |
|
| 79 | + //if the language exists in cookie use it |
|
| 80 | + $cfgKey = get_config('language_cookie_name'); |
|
| 81 | + $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 82 | + $objCookie = & class_loader('Cookie'); |
|
| 83 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 84 | + if($cookieLang && $this->isValid($cookieLang)){ |
|
| 85 | + $this->current = $cookieLang; |
|
| 86 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 87 | + } |
|
| 88 | + else{ |
|
| 89 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 90 | + $this->current = $this->getDefault(); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Get the all languages messages |
|
| 96 | + * |
|
| 97 | + * @return array the language message list |
|
| 98 | + */ |
|
| 99 | + public function getAll(){ |
|
| 100 | + return $this->languages; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Set the language message |
|
| 105 | + * |
|
| 106 | + * @param string $key the language key to identify |
|
| 107 | + * @param string $value the language message value |
|
| 108 | + */ |
|
| 109 | + public function set($key, $value){ |
|
| 110 | + $this->languages[$key] = $value; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the language message for the given key. If can't find return the default value |
|
| 115 | + * |
|
| 116 | + * @param string $key the message language key |
|
| 117 | + * @param string $default the default value to return if can not found the language message key |
|
| 118 | + * |
|
| 119 | + * @return string the language message value |
|
| 120 | + */ |
|
| 121 | + public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 122 | + if(isset($this->languages[$key])){ |
|
| 123 | + return $this->languages[$key]; |
|
| 124 | + } |
|
| 125 | + $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 126 | + return $default; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Check whether the language file for given name exists |
|
| 131 | + * |
|
| 132 | + * @param string $language the language name like "fr", "en", etc. |
|
| 133 | + * |
|
| 134 | + * @return boolean true if the language directory exists, false or not |
|
| 135 | + */ |
|
| 136 | + public function isValid($language){ |
|
| 137 | + $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 138 | + foreach($searchDir as $dir){ |
|
| 139 | + if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 140 | + return true; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the default language value like "en" , "fr", etc. |
|
| 148 | + * |
|
| 149 | + * @return string the default language |
|
| 150 | + */ |
|
| 151 | + public function getDefault(){ |
|
| 152 | + return $this->default; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Get the current language defined by cookie or the default value |
|
| 157 | + * |
|
| 158 | + * @return string the current language |
|
| 159 | + */ |
|
| 160 | + public function getCurrent(){ |
|
| 161 | + return $this->current; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Add new supported or available language |
|
| 166 | + * |
|
| 167 | + * @param string $name the short language name like "en", "fr". |
|
| 168 | + * @param string $description the human readable description of this language |
|
| 169 | + */ |
|
| 170 | + public function addLang($name, $description){ |
|
| 171 | + if(isset($this->availables[$name])){ |
|
| 172 | + return; //already added cost in performance |
|
| 173 | + } |
|
| 174 | + if($this->isValid($name)){ |
|
| 175 | + $this->availables[$name] = $description; |
|
| 176 | + } |
|
| 177 | + else{ |
|
| 178 | + show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Get the list of the application supported language |
|
| 184 | + * |
|
| 185 | + * @return array the list of the application language |
|
| 186 | + */ |
|
| 187 | + public function getSupported(){ |
|
| 188 | + return $this->availables; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Add new language messages |
|
| 193 | + * |
|
| 194 | + * @param array $langs the languages array of the messages to be added |
|
| 195 | + */ |
|
| 196 | + public function addLangMessages(array $langs){ |
|
| 197 | + foreach ($langs as $key => $value) { |
|
| 198 | + $this->set($key, $value); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
@@ -1,586 +1,586 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - class Router extends BaseClass{ |
|
| 28 | - /** |
|
| 29 | - * @var array $pattern: The list of URIs to validate against |
|
| 30 | - */ |
|
| 31 | - private $pattern = array(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $callback: The list of callback to call |
|
| 35 | - */ |
|
| 36 | - private $callback = array(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | - */ |
|
| 41 | - protected $uriTrim = '/\^$'; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var string $uri: The route URI to use |
|
| 45 | - */ |
|
| 46 | - protected $uri = ''; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The module name of the current request |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $module = null; |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + class Router extends BaseClass{ |
|
| 28 | + /** |
|
| 29 | + * @var array $pattern: The list of URIs to validate against |
|
| 30 | + */ |
|
| 31 | + private $pattern = array(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $callback: The list of callback to call |
|
| 35 | + */ |
|
| 36 | + private $callback = array(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | + */ |
|
| 41 | + protected $uriTrim = '/\^$'; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var string $uri: The route URI to use |
|
| 45 | + */ |
|
| 46 | + protected $uri = ''; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The module name of the current request |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $module = null; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * The controller name of the current request |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - protected $controller = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The controller path |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - protected $controllerPath = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The method name. The default value is "index" |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - protected $method = 'index'; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * List of argument to pass to the method |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $args = array(); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * List of routes configurations |
|
| 80 | - * @var array |
|
| 81 | - */ |
|
| 82 | - protected $routes = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The segments array for the current request |
|
| 86 | - * @var array |
|
| 87 | - */ |
|
| 88 | - protected $segments = array(); |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Construct the new Router instance |
|
| 92 | - */ |
|
| 93 | - public function __construct(){ |
|
| 94 | - parent::__construct(); |
|
| 54 | + /** |
|
| 55 | + * The controller name of the current request |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + protected $controller = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The controller path |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + protected $controllerPath = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The method name. The default value is "index" |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + protected $method = 'index'; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * List of argument to pass to the method |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $args = array(); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * List of routes configurations |
|
| 80 | + * @var array |
|
| 81 | + */ |
|
| 82 | + protected $routes = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The segments array for the current request |
|
| 86 | + * @var array |
|
| 87 | + */ |
|
| 88 | + protected $segments = array(); |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Construct the new Router instance |
|
| 92 | + */ |
|
| 93 | + public function __construct(){ |
|
| 94 | + parent::__construct(); |
|
| 95 | 95 | |
| 96 | - //loading routes for module |
|
| 97 | - $moduleRouteList = array(); |
|
| 98 | - $modulesRoutes = Module::getModulesRoutesConfig(); |
|
| 99 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 100 | - $moduleRouteList = $modulesRoutes; |
|
| 101 | - unset($modulesRoutes); |
|
| 102 | - } |
|
| 103 | - $this->setRouteConfiguration($moduleRouteList); |
|
| 104 | - $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 105 | - |
|
| 106 | - //Set route informations |
|
| 107 | - $this->setRouteConfigurationInfos(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Get the route patterns |
|
| 112 | - * @return array |
|
| 113 | - */ |
|
| 114 | - public function getPattern(){ |
|
| 115 | - return $this->pattern; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get the route callbacks |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function getCallback(){ |
|
| 123 | - return $this->callback; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Get the module name |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function getModule(){ |
|
| 131 | - return $this->module; |
|
| 132 | - } |
|
| 96 | + //loading routes for module |
|
| 97 | + $moduleRouteList = array(); |
|
| 98 | + $modulesRoutes = Module::getModulesRoutesConfig(); |
|
| 99 | + if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 100 | + $moduleRouteList = $modulesRoutes; |
|
| 101 | + unset($modulesRoutes); |
|
| 102 | + } |
|
| 103 | + $this->setRouteConfiguration($moduleRouteList); |
|
| 104 | + $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 105 | + |
|
| 106 | + //Set route informations |
|
| 107 | + $this->setRouteConfigurationInfos(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Get the route patterns |
|
| 112 | + * @return array |
|
| 113 | + */ |
|
| 114 | + public function getPattern(){ |
|
| 115 | + return $this->pattern; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get the route callbacks |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function getCallback(){ |
|
| 123 | + return $this->callback; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Get the module name |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function getModule(){ |
|
| 131 | + return $this->module; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Get the controller name |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function getController(){ |
|
| 139 | - return $this->controller; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get the controller file path |
|
| 144 | - * @return string |
|
| 145 | - */ |
|
| 146 | - public function getControllerPath(){ |
|
| 147 | - return $this->controllerPath; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Get the controller method |
|
| 152 | - * @return string |
|
| 153 | - */ |
|
| 154 | - public function getMethod(){ |
|
| 155 | - return $this->method; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get the request arguments |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - public function getArgs(){ |
|
| 163 | - return $this->args; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get the URL segments array |
|
| 168 | - * @return array |
|
| 169 | - */ |
|
| 170 | - public function getSegments(){ |
|
| 171 | - return $this->segments; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Get the route URI |
|
| 176 | - * @return string |
|
| 177 | - */ |
|
| 178 | - public function getRouteUri(){ |
|
| 179 | - return $this->uri; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Add the URI and callback to the list of URIs to validate |
|
| 184 | - * |
|
| 185 | - * @param string $uri the request URI |
|
| 186 | - * @param string $callback the callback function |
|
| 187 | - * |
|
| 188 | - * @return object the current instance |
|
| 189 | - */ |
|
| 190 | - public function add($uri, $callback) { |
|
| 191 | - $uri = trim($uri, $this->uriTrim); |
|
| 192 | - if(in_array($uri, $this->pattern)){ |
|
| 193 | - $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 194 | - } |
|
| 195 | - $this->pattern[] = $uri; |
|
| 196 | - $this->callback[] = $callback; |
|
| 197 | - return $this; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Remove the route configuration |
|
| 202 | - * |
|
| 203 | - * @param string $uri the URI |
|
| 204 | - * |
|
| 205 | - * @return object the current instance |
|
| 206 | - */ |
|
| 207 | - public function removeRoute($uri) { |
|
| 208 | - $index = array_search($uri, $this->pattern, true); |
|
| 209 | - if($index !== false){ |
|
| 210 | - $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 211 | - unset($this->pattern[$index]); |
|
| 212 | - unset($this->callback[$index]); |
|
| 213 | - } |
|
| 214 | - return $this; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Remove all the routes from the configuration |
|
| 220 | - * |
|
| 221 | - * @return object the current instance |
|
| 222 | - */ |
|
| 223 | - public function removeAllRoute() { |
|
| 224 | - $this->logger->info('Remove all routes from the configuration'); |
|
| 225 | - $this->pattern = array(); |
|
| 226 | - $this->callback = array(); |
|
| 227 | - $this->routes = array(); |
|
| 228 | - return $this; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Set the route URI to use later |
|
| 234 | - * @param string $uri the route URI, if is empty will determine automatically |
|
| 235 | - * @return object |
|
| 236 | - */ |
|
| 237 | - public function setRouteUri($uri = ''){ |
|
| 238 | - $routeUri = ''; |
|
| 239 | - if(! empty($uri)){ |
|
| 240 | - $routeUri = $uri; |
|
| 241 | - } |
|
| 242 | - //if the application is running in CLI mode use the first argument |
|
| 243 | - else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 244 | - $routeUri = $_SERVER['argv'][1]; |
|
| 245 | - } |
|
| 246 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 247 | - $routeUri = $_SERVER['REQUEST_URI']; |
|
| 248 | - } |
|
| 249 | - $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 250 | - //remove url suffix from the request URI |
|
| 251 | - $suffix = get_config('url_suffix'); |
|
| 252 | - if ($suffix) { |
|
| 253 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 254 | - $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 255 | - } |
|
| 256 | - if (strpos($routeUri, '?') !== false){ |
|
| 257 | - $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 258 | - } |
|
| 259 | - $this->uri = trim($routeUri, $this->uriTrim); |
|
| 260 | - return $this; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Set the route segments informations |
|
| 265 | - * @param array $segements the route segments information |
|
| 266 | - * |
|
| 267 | - * @return object |
|
| 268 | - */ |
|
| 269 | - public function setRouteSegments(array $segments = array()){ |
|
| 270 | - if(! empty($segments)){ |
|
| 271 | - $this->segments = $segments; |
|
| 272 | - } else if (!empty($this->uri)) { |
|
| 273 | - $this->segments = explode('/', $this->uri); |
|
| 274 | - } |
|
| 275 | - $segment = $this->segments; |
|
| 276 | - $baseUrl = get_config('base_url'); |
|
| 277 | - //check if the app is not in DOCUMENT_ROOT |
|
| 278 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 279 | - array_shift($segment); |
|
| 280 | - $this->segments = $segment; |
|
| 281 | - } |
|
| 282 | - $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 283 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 284 | - $this->logger->info('The request URI contains the front controller'); |
|
| 285 | - array_shift($segment); |
|
| 286 | - $this->segments = $segment; |
|
| 287 | - } |
|
| 288 | - return $this; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Setting the route parameters like module, controller, method, argument |
|
| 293 | - * @return object the current instance |
|
| 294 | - */ |
|
| 295 | - public function determineRouteParamsInformation() { |
|
| 296 | - $this->logger->debug('Routing process start ...'); |
|
| 134 | + /** |
|
| 135 | + * Get the controller name |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function getController(){ |
|
| 139 | + return $this->controller; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get the controller file path |
|
| 144 | + * @return string |
|
| 145 | + */ |
|
| 146 | + public function getControllerPath(){ |
|
| 147 | + return $this->controllerPath; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Get the controller method |
|
| 152 | + * @return string |
|
| 153 | + */ |
|
| 154 | + public function getMethod(){ |
|
| 155 | + return $this->method; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get the request arguments |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + public function getArgs(){ |
|
| 163 | + return $this->args; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get the URL segments array |
|
| 168 | + * @return array |
|
| 169 | + */ |
|
| 170 | + public function getSegments(){ |
|
| 171 | + return $this->segments; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Get the route URI |
|
| 176 | + * @return string |
|
| 177 | + */ |
|
| 178 | + public function getRouteUri(){ |
|
| 179 | + return $this->uri; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Add the URI and callback to the list of URIs to validate |
|
| 184 | + * |
|
| 185 | + * @param string $uri the request URI |
|
| 186 | + * @param string $callback the callback function |
|
| 187 | + * |
|
| 188 | + * @return object the current instance |
|
| 189 | + */ |
|
| 190 | + public function add($uri, $callback) { |
|
| 191 | + $uri = trim($uri, $this->uriTrim); |
|
| 192 | + if(in_array($uri, $this->pattern)){ |
|
| 193 | + $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 194 | + } |
|
| 195 | + $this->pattern[] = $uri; |
|
| 196 | + $this->callback[] = $callback; |
|
| 197 | + return $this; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Remove the route configuration |
|
| 202 | + * |
|
| 203 | + * @param string $uri the URI |
|
| 204 | + * |
|
| 205 | + * @return object the current instance |
|
| 206 | + */ |
|
| 207 | + public function removeRoute($uri) { |
|
| 208 | + $index = array_search($uri, $this->pattern, true); |
|
| 209 | + if($index !== false){ |
|
| 210 | + $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 211 | + unset($this->pattern[$index]); |
|
| 212 | + unset($this->callback[$index]); |
|
| 213 | + } |
|
| 214 | + return $this; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Remove all the routes from the configuration |
|
| 220 | + * |
|
| 221 | + * @return object the current instance |
|
| 222 | + */ |
|
| 223 | + public function removeAllRoute() { |
|
| 224 | + $this->logger->info('Remove all routes from the configuration'); |
|
| 225 | + $this->pattern = array(); |
|
| 226 | + $this->callback = array(); |
|
| 227 | + $this->routes = array(); |
|
| 228 | + return $this; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Set the route URI to use later |
|
| 234 | + * @param string $uri the route URI, if is empty will determine automatically |
|
| 235 | + * @return object |
|
| 236 | + */ |
|
| 237 | + public function setRouteUri($uri = ''){ |
|
| 238 | + $routeUri = ''; |
|
| 239 | + if(! empty($uri)){ |
|
| 240 | + $routeUri = $uri; |
|
| 241 | + } |
|
| 242 | + //if the application is running in CLI mode use the first argument |
|
| 243 | + else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 244 | + $routeUri = $_SERVER['argv'][1]; |
|
| 245 | + } |
|
| 246 | + else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 247 | + $routeUri = $_SERVER['REQUEST_URI']; |
|
| 248 | + } |
|
| 249 | + $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 250 | + //remove url suffix from the request URI |
|
| 251 | + $suffix = get_config('url_suffix'); |
|
| 252 | + if ($suffix) { |
|
| 253 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 254 | + $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 255 | + } |
|
| 256 | + if (strpos($routeUri, '?') !== false){ |
|
| 257 | + $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 258 | + } |
|
| 259 | + $this->uri = trim($routeUri, $this->uriTrim); |
|
| 260 | + return $this; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Set the route segments informations |
|
| 265 | + * @param array $segements the route segments information |
|
| 266 | + * |
|
| 267 | + * @return object |
|
| 268 | + */ |
|
| 269 | + public function setRouteSegments(array $segments = array()){ |
|
| 270 | + if(! empty($segments)){ |
|
| 271 | + $this->segments = $segments; |
|
| 272 | + } else if (!empty($this->uri)) { |
|
| 273 | + $this->segments = explode('/', $this->uri); |
|
| 274 | + } |
|
| 275 | + $segment = $this->segments; |
|
| 276 | + $baseUrl = get_config('base_url'); |
|
| 277 | + //check if the app is not in DOCUMENT_ROOT |
|
| 278 | + if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 279 | + array_shift($segment); |
|
| 280 | + $this->segments = $segment; |
|
| 281 | + } |
|
| 282 | + $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 283 | + if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 284 | + $this->logger->info('The request URI contains the front controller'); |
|
| 285 | + array_shift($segment); |
|
| 286 | + $this->segments = $segment; |
|
| 287 | + } |
|
| 288 | + return $this; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Setting the route parameters like module, controller, method, argument |
|
| 293 | + * @return object the current instance |
|
| 294 | + */ |
|
| 295 | + public function determineRouteParamsInformation() { |
|
| 296 | + $this->logger->debug('Routing process start ...'); |
|
| 297 | 297 | |
| 298 | - //determine route parameters using the config |
|
| 299 | - $this->determineRouteParamsFromConfig(); |
|
| 298 | + //determine route parameters using the config |
|
| 299 | + $this->determineRouteParamsFromConfig(); |
|
| 300 | 300 | |
| 301 | - //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 302 | - //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 303 | - if(! $this->controller){ |
|
| 304 | - $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 305 | - //determine route parameters using the REQUEST_URI param |
|
| 306 | - $this->determineRouteParamsFromRequestUri(); |
|
| 307 | - } |
|
| 308 | - //Set the controller file path if not yet set |
|
| 309 | - $this->setControllerFilePath(); |
|
| 310 | - $this->logger->debug('Routing process end.'); |
|
| 311 | - |
|
| 312 | - return $this; |
|
| 313 | - } |
|
| 301 | + //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 302 | + //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 303 | + if(! $this->controller){ |
|
| 304 | + $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 305 | + //determine route parameters using the REQUEST_URI param |
|
| 306 | + $this->determineRouteParamsFromRequestUri(); |
|
| 307 | + } |
|
| 308 | + //Set the controller file path if not yet set |
|
| 309 | + $this->setControllerFilePath(); |
|
| 310 | + $this->logger->debug('Routing process end.'); |
|
| 311 | + |
|
| 312 | + return $this; |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - /** |
|
| 316 | - * Routing the request to the correspondant module/controller/method if exists |
|
| 317 | - * otherwise send 404 error. |
|
| 318 | - */ |
|
| 319 | - public function processRequest(){ |
|
| 320 | - //Setting the route URI |
|
| 321 | - $this->setRouteUri(); |
|
| 322 | - |
|
| 323 | - //setting route segments |
|
| 324 | - $this->setRouteSegments(); |
|
| 325 | - |
|
| 326 | - $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 327 | - |
|
| 328 | - //determine the route parameters information |
|
| 329 | - $this->determineRouteParamsInformation(); |
|
| 330 | - |
|
| 331 | - $e404 = false; |
|
| 332 | - $classFilePath = $this->controllerPath; |
|
| 333 | - $controller = ucfirst($this->controller); |
|
| 334 | - $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 335 | - $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 315 | + /** |
|
| 316 | + * Routing the request to the correspondant module/controller/method if exists |
|
| 317 | + * otherwise send 404 error. |
|
| 318 | + */ |
|
| 319 | + public function processRequest(){ |
|
| 320 | + //Setting the route URI |
|
| 321 | + $this->setRouteUri(); |
|
| 322 | + |
|
| 323 | + //setting route segments |
|
| 324 | + $this->setRouteSegments(); |
|
| 325 | + |
|
| 326 | + $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 327 | + |
|
| 328 | + //determine the route parameters information |
|
| 329 | + $this->determineRouteParamsInformation(); |
|
| 330 | + |
|
| 331 | + $e404 = false; |
|
| 332 | + $classFilePath = $this->controllerPath; |
|
| 333 | + $controller = ucfirst($this->controller); |
|
| 334 | + $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 335 | + $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 336 | 336 | |
| 337 | - if(file_exists($classFilePath)){ |
|
| 338 | - require_once $classFilePath; |
|
| 339 | - if(! class_exists($controller, false)){ |
|
| 340 | - $e404 = true; |
|
| 341 | - $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 342 | - } |
|
| 343 | - else{ |
|
| 344 | - $controllerInstance = new $controller(); |
|
| 345 | - $controllerMethod = $this->getMethod(); |
|
| 346 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 347 | - $e404 = true; |
|
| 348 | - $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 349 | - } |
|
| 350 | - else{ |
|
| 351 | - $this->logger->info('Routing data is set correctly now GO!'); |
|
| 352 | - call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 353 | - //render the final page to user |
|
| 354 | - $this->logger->info('Render the final output to the browser'); |
|
| 355 | - get_instance()->response->renderFinalPage(); |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - else{ |
|
| 360 | - $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 361 | - $e404 = true; |
|
| 362 | - } |
|
| 363 | - if($e404){ |
|
| 364 | - if(IS_CLI){ |
|
| 365 | - set_http_status_header(404); |
|
| 366 | - echo 'Error 404: page not found.'; |
|
| 367 | - } else { |
|
| 368 | - $response =& class_loader('Response', 'classes'); |
|
| 369 | - $response->send404(); |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Setting the route configuration using the configuration file and additional configuration from param |
|
| 377 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 378 | - * @param boolean $useConfigFile whether to use route configuration file |
|
| 379 | - * @return object |
|
| 380 | - */ |
|
| 381 | - public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 382 | - $route = array(); |
|
| 383 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 384 | - require_once CONFIG_PATH . 'routes.php'; |
|
| 385 | - } |
|
| 386 | - $route = array_merge($route, $overwriteConfig); |
|
| 387 | - $this->routes = $route; |
|
| 388 | - //if route is empty remove all configuration |
|
| 389 | - if(empty($route)){ |
|
| 390 | - $this->removeAllRoute(); |
|
| 391 | - } |
|
| 392 | - return $this; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Get the route configuration |
|
| 397 | - * @return array |
|
| 398 | - */ |
|
| 399 | - public function getRouteConfiguration(){ |
|
| 400 | - return $this->routes; |
|
| 401 | - } |
|
| 337 | + if(file_exists($classFilePath)){ |
|
| 338 | + require_once $classFilePath; |
|
| 339 | + if(! class_exists($controller, false)){ |
|
| 340 | + $e404 = true; |
|
| 341 | + $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 342 | + } |
|
| 343 | + else{ |
|
| 344 | + $controllerInstance = new $controller(); |
|
| 345 | + $controllerMethod = $this->getMethod(); |
|
| 346 | + if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 347 | + $e404 = true; |
|
| 348 | + $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 349 | + } |
|
| 350 | + else{ |
|
| 351 | + $this->logger->info('Routing data is set correctly now GO!'); |
|
| 352 | + call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 353 | + //render the final page to user |
|
| 354 | + $this->logger->info('Render the final output to the browser'); |
|
| 355 | + get_instance()->response->renderFinalPage(); |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + else{ |
|
| 360 | + $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 361 | + $e404 = true; |
|
| 362 | + } |
|
| 363 | + if($e404){ |
|
| 364 | + if(IS_CLI){ |
|
| 365 | + set_http_status_header(404); |
|
| 366 | + echo 'Error 404: page not found.'; |
|
| 367 | + } else { |
|
| 368 | + $response =& class_loader('Response', 'classes'); |
|
| 369 | + $response->send404(); |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Setting the route configuration using the configuration file and additional configuration from param |
|
| 377 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 378 | + * @param boolean $useConfigFile whether to use route configuration file |
|
| 379 | + * @return object |
|
| 380 | + */ |
|
| 381 | + public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 382 | + $route = array(); |
|
| 383 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 384 | + require_once CONFIG_PATH . 'routes.php'; |
|
| 385 | + } |
|
| 386 | + $route = array_merge($route, $overwriteConfig); |
|
| 387 | + $this->routes = $route; |
|
| 388 | + //if route is empty remove all configuration |
|
| 389 | + if(empty($route)){ |
|
| 390 | + $this->removeAllRoute(); |
|
| 391 | + } |
|
| 392 | + return $this; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Get the route configuration |
|
| 397 | + * @return array |
|
| 398 | + */ |
|
| 399 | + public function getRouteConfiguration(){ |
|
| 400 | + return $this->routes; |
|
| 401 | + } |
|
| 402 | 402 | |
| 403 | 403 | |
| 404 | - /** |
|
| 405 | - * Set the controller file path if is not set |
|
| 406 | - * @param string $path the file path if is null will using the route |
|
| 407 | - * information |
|
| 408 | - * |
|
| 409 | - * @return object the current instance |
|
| 410 | - */ |
|
| 411 | - public function setControllerFilePath($path = null){ |
|
| 412 | - if($path !== null){ |
|
| 413 | - $this->controllerPath = $path; |
|
| 414 | - return $this; |
|
| 415 | - } |
|
| 416 | - //did we set the controller, so set the controller path |
|
| 417 | - if($this->controller && ! $this->controllerPath){ |
|
| 418 | - $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 419 | - $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 420 | - //if the controller is in module |
|
| 421 | - if($this->module){ |
|
| 422 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 423 | - if($path !== false){ |
|
| 424 | - $controllerPath = $path; |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - $this->controllerPath = $controllerPath; |
|
| 428 | - } |
|
| 429 | - return $this; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Determine the route parameters from route configuration |
|
| 434 | - * @return void |
|
| 435 | - */ |
|
| 436 | - protected function determineRouteParamsFromConfig(){ |
|
| 437 | - $uri = implode('/', $this->segments); |
|
| 438 | - /* |
|
| 404 | + /** |
|
| 405 | + * Set the controller file path if is not set |
|
| 406 | + * @param string $path the file path if is null will using the route |
|
| 407 | + * information |
|
| 408 | + * |
|
| 409 | + * @return object the current instance |
|
| 410 | + */ |
|
| 411 | + public function setControllerFilePath($path = null){ |
|
| 412 | + if($path !== null){ |
|
| 413 | + $this->controllerPath = $path; |
|
| 414 | + return $this; |
|
| 415 | + } |
|
| 416 | + //did we set the controller, so set the controller path |
|
| 417 | + if($this->controller && ! $this->controllerPath){ |
|
| 418 | + $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 419 | + $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 420 | + //if the controller is in module |
|
| 421 | + if($this->module){ |
|
| 422 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 423 | + if($path !== false){ |
|
| 424 | + $controllerPath = $path; |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + $this->controllerPath = $controllerPath; |
|
| 428 | + } |
|
| 429 | + return $this; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Determine the route parameters from route configuration |
|
| 434 | + * @return void |
|
| 435 | + */ |
|
| 436 | + protected function determineRouteParamsFromConfig(){ |
|
| 437 | + $uri = implode('/', $this->segments); |
|
| 438 | + /* |
|
| 439 | 439 | * Generics routes patterns |
| 440 | 440 | */ |
| 441 | - $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 442 | - $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 443 | - |
|
| 444 | - $this->logger->debug( |
|
| 445 | - 'Begin to loop in the predefined routes configuration ' |
|
| 446 | - . 'to check if the current request match' |
|
| 447 | - ); |
|
| 448 | - |
|
| 449 | - // Cycle through the URIs stored in the array |
|
| 450 | - foreach ($this->pattern as $index => $uriList) { |
|
| 451 | - $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 452 | - // Check for an existant matching URI |
|
| 453 | - if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 454 | - $this->logger->info( |
|
| 455 | - 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 456 | - . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 457 | - ); |
|
| 458 | - array_shift($args); |
|
| 459 | - //check if this contains an module |
|
| 460 | - $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 461 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 462 | - $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 463 | - $this->module = $moduleControllerMethod[0]; |
|
| 464 | - $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 465 | - } |
|
| 466 | - else{ |
|
| 467 | - $this->logger->info('The current request does not use the module'); |
|
| 468 | - $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 469 | - } |
|
| 470 | - if(is_array($moduleControllerMethod)){ |
|
| 471 | - if(isset($moduleControllerMethod[0])){ |
|
| 472 | - $this->controller = $moduleControllerMethod[0]; |
|
| 473 | - } |
|
| 474 | - if(isset($moduleControllerMethod[1])){ |
|
| 475 | - $this->method = $moduleControllerMethod[1]; |
|
| 476 | - } |
|
| 477 | - $this->args = $args; |
|
| 478 | - } |
|
| 479 | - // stop here |
|
| 480 | - break; |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - //first if the controller is not set and the module is set use the module name as the controller |
|
| 485 | - if(! $this->controller && $this->module){ |
|
| 486 | - $this->logger->info( |
|
| 487 | - 'After loop in predefined routes configuration, |
|
| 441 | + $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 442 | + $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 443 | + |
|
| 444 | + $this->logger->debug( |
|
| 445 | + 'Begin to loop in the predefined routes configuration ' |
|
| 446 | + . 'to check if the current request match' |
|
| 447 | + ); |
|
| 448 | + |
|
| 449 | + // Cycle through the URIs stored in the array |
|
| 450 | + foreach ($this->pattern as $index => $uriList) { |
|
| 451 | + $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 452 | + // Check for an existant matching URI |
|
| 453 | + if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 454 | + $this->logger->info( |
|
| 455 | + 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 456 | + . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 457 | + ); |
|
| 458 | + array_shift($args); |
|
| 459 | + //check if this contains an module |
|
| 460 | + $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 461 | + if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 462 | + $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 463 | + $this->module = $moduleControllerMethod[0]; |
|
| 464 | + $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 465 | + } |
|
| 466 | + else{ |
|
| 467 | + $this->logger->info('The current request does not use the module'); |
|
| 468 | + $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 469 | + } |
|
| 470 | + if(is_array($moduleControllerMethod)){ |
|
| 471 | + if(isset($moduleControllerMethod[0])){ |
|
| 472 | + $this->controller = $moduleControllerMethod[0]; |
|
| 473 | + } |
|
| 474 | + if(isset($moduleControllerMethod[1])){ |
|
| 475 | + $this->method = $moduleControllerMethod[1]; |
|
| 476 | + } |
|
| 477 | + $this->args = $args; |
|
| 478 | + } |
|
| 479 | + // stop here |
|
| 480 | + break; |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + //first if the controller is not set and the module is set use the module name as the controller |
|
| 485 | + if(! $this->controller && $this->module){ |
|
| 486 | + $this->logger->info( |
|
| 487 | + 'After loop in predefined routes configuration, |
|
| 488 | 488 | the module name is set but the controller is not set, |
| 489 | 489 | so we will use module as the controller' |
| 490 | - ); |
|
| 491 | - $this->controller = $this->module; |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 497 | - * @return void |
|
| 498 | - */ |
|
| 499 | - protected function determineRouteParamsFromRequestUri(){ |
|
| 500 | - $segment = $this->segments; |
|
| 501 | - $nbSegment = count($segment); |
|
| 502 | - //if segment is null so means no need to perform |
|
| 503 | - if($nbSegment > 0){ |
|
| 504 | - //get the module list |
|
| 505 | - $modules = Module::getModuleList(); |
|
| 506 | - //first check if no module |
|
| 507 | - if(empty($modules)){ |
|
| 508 | - $this->logger->info('No module was loaded will skip the module checking'); |
|
| 509 | - //the application don't use module |
|
| 510 | - //controller |
|
| 511 | - if(isset($segment[0])){ |
|
| 512 | - $this->controller = $segment[0]; |
|
| 513 | - array_shift($segment); |
|
| 514 | - } |
|
| 515 | - //method |
|
| 516 | - if(isset($segment[0])){ |
|
| 517 | - $this->method = $segment[0]; |
|
| 518 | - array_shift($segment); |
|
| 519 | - } |
|
| 520 | - //args |
|
| 521 | - $this->args = $segment; |
|
| 522 | - } |
|
| 523 | - else{ |
|
| 524 | - $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 525 | - if(in_array($segment[0], $modules)){ |
|
| 526 | - $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 527 | - $this->module = $segment[0]; |
|
| 528 | - array_shift($segment); |
|
| 529 | - //check if the second arg is the controller from module |
|
| 530 | - if(isset($segment[0])){ |
|
| 531 | - $this->controller = $segment[0]; |
|
| 532 | - //check if the request use the same module name and controller |
|
| 533 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 534 | - if(! $path){ |
|
| 535 | - $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 536 | - $this->controller = $this->module; |
|
| 537 | - } |
|
| 538 | - else{ |
|
| 539 | - $this->controllerPath = $path; |
|
| 540 | - array_shift($segment); |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - //check for method |
|
| 544 | - if(isset($segment[0])){ |
|
| 545 | - $this->method = $segment[0]; |
|
| 546 | - array_shift($segment); |
|
| 547 | - } |
|
| 548 | - //the remaining is for args |
|
| 549 | - $this->args = $segment; |
|
| 550 | - } |
|
| 551 | - else{ |
|
| 552 | - $this->logger->info('The current request information is not found in the module list'); |
|
| 553 | - //controller |
|
| 554 | - if(isset($segment[0])){ |
|
| 555 | - $this->controller = $segment[0]; |
|
| 556 | - array_shift($segment); |
|
| 557 | - } |
|
| 558 | - //method |
|
| 559 | - if(isset($segment[0])){ |
|
| 560 | - $this->method = $segment[0]; |
|
| 561 | - array_shift($segment); |
|
| 562 | - } |
|
| 563 | - //args |
|
| 564 | - $this->args = $segment; |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - if(! $this->controller && $this->module){ |
|
| 568 | - $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 569 | - $this->controller = $this->module; |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * Set the route informations using the configuration |
|
| 576 | - * |
|
| 577 | - * @return object the current instance |
|
| 578 | - */ |
|
| 579 | - protected function setRouteConfigurationInfos(){ |
|
| 580 | - //adding route |
|
| 581 | - foreach($this->routes as $pattern => $callback){ |
|
| 582 | - $this->add($pattern, $callback); |
|
| 583 | - } |
|
| 584 | - return $this; |
|
| 585 | - } |
|
| 586 | - } |
|
| 490 | + ); |
|
| 491 | + $this->controller = $this->module; |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 497 | + * @return void |
|
| 498 | + */ |
|
| 499 | + protected function determineRouteParamsFromRequestUri(){ |
|
| 500 | + $segment = $this->segments; |
|
| 501 | + $nbSegment = count($segment); |
|
| 502 | + //if segment is null so means no need to perform |
|
| 503 | + if($nbSegment > 0){ |
|
| 504 | + //get the module list |
|
| 505 | + $modules = Module::getModuleList(); |
|
| 506 | + //first check if no module |
|
| 507 | + if(empty($modules)){ |
|
| 508 | + $this->logger->info('No module was loaded will skip the module checking'); |
|
| 509 | + //the application don't use module |
|
| 510 | + //controller |
|
| 511 | + if(isset($segment[0])){ |
|
| 512 | + $this->controller = $segment[0]; |
|
| 513 | + array_shift($segment); |
|
| 514 | + } |
|
| 515 | + //method |
|
| 516 | + if(isset($segment[0])){ |
|
| 517 | + $this->method = $segment[0]; |
|
| 518 | + array_shift($segment); |
|
| 519 | + } |
|
| 520 | + //args |
|
| 521 | + $this->args = $segment; |
|
| 522 | + } |
|
| 523 | + else{ |
|
| 524 | + $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 525 | + if(in_array($segment[0], $modules)){ |
|
| 526 | + $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 527 | + $this->module = $segment[0]; |
|
| 528 | + array_shift($segment); |
|
| 529 | + //check if the second arg is the controller from module |
|
| 530 | + if(isset($segment[0])){ |
|
| 531 | + $this->controller = $segment[0]; |
|
| 532 | + //check if the request use the same module name and controller |
|
| 533 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 534 | + if(! $path){ |
|
| 535 | + $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 536 | + $this->controller = $this->module; |
|
| 537 | + } |
|
| 538 | + else{ |
|
| 539 | + $this->controllerPath = $path; |
|
| 540 | + array_shift($segment); |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + //check for method |
|
| 544 | + if(isset($segment[0])){ |
|
| 545 | + $this->method = $segment[0]; |
|
| 546 | + array_shift($segment); |
|
| 547 | + } |
|
| 548 | + //the remaining is for args |
|
| 549 | + $this->args = $segment; |
|
| 550 | + } |
|
| 551 | + else{ |
|
| 552 | + $this->logger->info('The current request information is not found in the module list'); |
|
| 553 | + //controller |
|
| 554 | + if(isset($segment[0])){ |
|
| 555 | + $this->controller = $segment[0]; |
|
| 556 | + array_shift($segment); |
|
| 557 | + } |
|
| 558 | + //method |
|
| 559 | + if(isset($segment[0])){ |
|
| 560 | + $this->method = $segment[0]; |
|
| 561 | + array_shift($segment); |
|
| 562 | + } |
|
| 563 | + //args |
|
| 564 | + $this->args = $segment; |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + if(! $this->controller && $this->module){ |
|
| 568 | + $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 569 | + $this->controller = $this->module; |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * Set the route informations using the configuration |
|
| 576 | + * |
|
| 577 | + * @return object the current instance |
|
| 578 | + */ |
|
| 579 | + protected function setRouteConfigurationInfos(){ |
|
| 580 | + //adding route |
|
| 581 | + foreach($this->routes as $pattern => $callback){ |
|
| 582 | + $this->add($pattern, $callback); |
|
| 583 | + } |
|
| 584 | + return $this; |
|
| 585 | + } |
|
| 586 | + } |
|
@@ -1,150 +1,150 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - class Session extends BaseStaticClass{ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + class Session extends BaseStaticClass{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The session flash key to use |
|
| 30 | - * @const |
|
| 31 | - */ |
|
| 32 | - const SESSION_FLASH_KEY = 'session_flash'; |
|
| 28 | + /** |
|
| 29 | + * The session flash key to use |
|
| 30 | + * @const |
|
| 31 | + */ |
|
| 32 | + const SESSION_FLASH_KEY = 'session_flash'; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the session item value |
|
| 36 | - * @param string $item the session item name to get |
|
| 37 | - * @param mixed $default the default value to use if can not find the session item in the list |
|
| 38 | - * @return mixed the session value if exist or the default value |
|
| 39 | - */ |
|
| 40 | - public static function get($item, $default = null){ |
|
| 41 | - $logger = self::getLogger(); |
|
| 42 | - $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
| 43 | - if(array_key_exists($item, $_SESSION)){ |
|
| 44 | - $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
|
| 45 | - return $_SESSION[$item]; |
|
| 46 | - } |
|
| 47 | - $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
| 48 | - return $default; |
|
| 49 | - } |
|
| 34 | + /** |
|
| 35 | + * Get the session item value |
|
| 36 | + * @param string $item the session item name to get |
|
| 37 | + * @param mixed $default the default value to use if can not find the session item in the list |
|
| 38 | + * @return mixed the session value if exist or the default value |
|
| 39 | + */ |
|
| 40 | + public static function get($item, $default = null){ |
|
| 41 | + $logger = self::getLogger(); |
|
| 42 | + $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
| 43 | + if(array_key_exists($item, $_SESSION)){ |
|
| 44 | + $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
|
| 45 | + return $_SESSION[$item]; |
|
| 46 | + } |
|
| 47 | + $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
| 48 | + return $default; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Set the session item value |
|
| 53 | - * @param string $item the session item name to set |
|
| 54 | - * @param mixed $value the session item value |
|
| 55 | - */ |
|
| 56 | - public static function set($item, $value){ |
|
| 57 | - $logger = self::getLogger(); |
|
| 58 | - $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
|
| 59 | - $_SESSION[$item] = $value; |
|
| 60 | - } |
|
| 51 | + /** |
|
| 52 | + * Set the session item value |
|
| 53 | + * @param string $item the session item name to set |
|
| 54 | + * @param mixed $value the session item value |
|
| 55 | + */ |
|
| 56 | + public static function set($item, $value){ |
|
| 57 | + $logger = self::getLogger(); |
|
| 58 | + $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
|
| 59 | + $_SESSION[$item] = $value; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get the session flash item value |
|
| 64 | - * @param string $item the session flash item name to get |
|
| 65 | - * @param mixed $default the default value to use if can not find the session flash item in the list |
|
| 66 | - * @return mixed the session flash value if exist or the default value |
|
| 67 | - */ |
|
| 68 | - public static function getFlash($item, $default = null){ |
|
| 69 | - $logger = self::getLogger(); |
|
| 70 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 71 | - $return = array_key_exists($key, $_SESSION) ? |
|
| 72 | - ($_SESSION[$key]) : $default; |
|
| 73 | - if(array_key_exists($key, $_SESSION)){ |
|
| 74 | - unset($_SESSION[$key]); |
|
| 75 | - } |
|
| 76 | - else{ |
|
| 77 | - $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
| 78 | - } |
|
| 79 | - return $return; |
|
| 80 | - } |
|
| 62 | + /** |
|
| 63 | + * Get the session flash item value |
|
| 64 | + * @param string $item the session flash item name to get |
|
| 65 | + * @param mixed $default the default value to use if can not find the session flash item in the list |
|
| 66 | + * @return mixed the session flash value if exist or the default value |
|
| 67 | + */ |
|
| 68 | + public static function getFlash($item, $default = null){ |
|
| 69 | + $logger = self::getLogger(); |
|
| 70 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 71 | + $return = array_key_exists($key, $_SESSION) ? |
|
| 72 | + ($_SESSION[$key]) : $default; |
|
| 73 | + if(array_key_exists($key, $_SESSION)){ |
|
| 74 | + unset($_SESSION[$key]); |
|
| 75 | + } |
|
| 76 | + else{ |
|
| 77 | + $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
| 78 | + } |
|
| 79 | + return $return; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Check whether the given session flash item exists |
|
| 84 | - * @param string $item the session flash item name |
|
| 85 | - * @return boolean |
|
| 86 | - */ |
|
| 87 | - public static function hasFlash($item){ |
|
| 88 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 89 | - return array_key_exists($key, $_SESSION); |
|
| 90 | - } |
|
| 82 | + /** |
|
| 83 | + * Check whether the given session flash item exists |
|
| 84 | + * @param string $item the session flash item name |
|
| 85 | + * @return boolean |
|
| 86 | + */ |
|
| 87 | + public static function hasFlash($item){ |
|
| 88 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 89 | + return array_key_exists($key, $_SESSION); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Set the session flash item value |
|
| 94 | - * @param string $item the session flash item name to set |
|
| 95 | - * @param mixed $value the session flash item value |
|
| 96 | - */ |
|
| 97 | - public static function setFlash($item, $value){ |
|
| 98 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 99 | - $_SESSION[$key] = $value; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * Set the session flash item value |
|
| 94 | + * @param string $item the session flash item name to set |
|
| 95 | + * @param mixed $value the session flash item value |
|
| 96 | + */ |
|
| 97 | + public static function setFlash($item, $value){ |
|
| 98 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 99 | + $_SESSION[$key] = $value; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Clear the session item in the list |
|
| 104 | - * @param string $item the session item name to be deleted |
|
| 105 | - */ |
|
| 106 | - public static function clear($item){ |
|
| 107 | - $logger = self::getLogger(); |
|
| 108 | - if(array_key_exists($item, $_SESSION)){ |
|
| 109 | - $logger->info('Deleting of session for item ['.$item.' ]'); |
|
| 110 | - unset($_SESSION[$item]); |
|
| 111 | - } |
|
| 112 | - else{ |
|
| 113 | - $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
| 114 | - } |
|
| 115 | - } |
|
| 102 | + /** |
|
| 103 | + * Clear the session item in the list |
|
| 104 | + * @param string $item the session item name to be deleted |
|
| 105 | + */ |
|
| 106 | + public static function clear($item){ |
|
| 107 | + $logger = self::getLogger(); |
|
| 108 | + if(array_key_exists($item, $_SESSION)){ |
|
| 109 | + $logger->info('Deleting of session for item ['.$item.' ]'); |
|
| 110 | + unset($_SESSION[$item]); |
|
| 111 | + } |
|
| 112 | + else{ |
|
| 113 | + $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Clear the session flash item in the list |
|
| 119 | - * @param string $item the session flash item name to be deleted |
|
| 120 | - */ |
|
| 121 | - public static function clearFlash($item){ |
|
| 122 | - $logger = self::getLogger(); |
|
| 123 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 124 | - if(array_key_exists($key, $_SESSION)){ |
|
| 125 | - $logger->info('Delete session flash for item ['.$item.']'); |
|
| 126 | - unset($_SESSION[$item]); |
|
| 127 | - } |
|
| 128 | - else{ |
|
| 129 | - $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
| 130 | - } |
|
| 131 | - } |
|
| 117 | + /** |
|
| 118 | + * Clear the session flash item in the list |
|
| 119 | + * @param string $item the session flash item name to be deleted |
|
| 120 | + */ |
|
| 121 | + public static function clearFlash($item){ |
|
| 122 | + $logger = self::getLogger(); |
|
| 123 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 124 | + if(array_key_exists($key, $_SESSION)){ |
|
| 125 | + $logger->info('Delete session flash for item ['.$item.']'); |
|
| 126 | + unset($_SESSION[$item]); |
|
| 127 | + } |
|
| 128 | + else{ |
|
| 129 | + $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Check whether the given session item exists |
|
| 135 | - * @param string $item the session item name |
|
| 136 | - * @return boolean |
|
| 137 | - */ |
|
| 138 | - public static function exists($item){ |
|
| 139 | - return array_key_exists($item, $_SESSION); |
|
| 140 | - } |
|
| 133 | + /** |
|
| 134 | + * Check whether the given session item exists |
|
| 135 | + * @param string $item the session item name |
|
| 136 | + * @return boolean |
|
| 137 | + */ |
|
| 138 | + public static function exists($item){ |
|
| 139 | + return array_key_exists($item, $_SESSION); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Destroy all session data values |
|
| 144 | - */ |
|
| 145 | - public static function clearAll(){ |
|
| 146 | - session_unset(); |
|
| 147 | - session_destroy(); |
|
| 148 | - } |
|
| 142 | + /** |
|
| 143 | + * Destroy all session data values |
|
| 144 | + */ |
|
| 145 | + public static function clearAll(){ |
|
| 146 | + session_unset(); |
|
| 147 | + session_destroy(); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - } |
|
| 150 | + } |
|
@@ -1,183 +1,183 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class ApcCache extends BaseClass implements CacheInterface{ |
|
| 27 | + class ApcCache extends BaseClass implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | 30 | |
| 31 | - public function __construct(){ |
|
| 32 | - parent::__construct(); |
|
| 33 | - if(! $this->isSupported()){ |
|
| 34 | - show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 35 | - } |
|
| 36 | - } |
|
| 31 | + public function __construct(){ |
|
| 32 | + parent::__construct(); |
|
| 33 | + if(! $this->isSupported()){ |
|
| 34 | + show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * This is used to get the cache data using the key |
|
| 40 | - * @param string $key the key to identify the cache data |
|
| 41 | - * @return mixed the cache data if exists else return false |
|
| 42 | - */ |
|
| 43 | - public function get($key){ |
|
| 44 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 45 | - $success = false; |
|
| 46 | - $data = apc_fetch($key, $success); |
|
| 47 | - if($success === false){ |
|
| 48 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 51 | - else{ |
|
| 52 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 53 | - $expire = time(); |
|
| 54 | - if($cacheInfo){ |
|
| 55 | - $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 56 | - } |
|
| 57 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 58 | - return $data; |
|
| 59 | - } |
|
| 60 | - } |
|
| 38 | + /** |
|
| 39 | + * This is used to get the cache data using the key |
|
| 40 | + * @param string $key the key to identify the cache data |
|
| 41 | + * @return mixed the cache data if exists else return false |
|
| 42 | + */ |
|
| 43 | + public function get($key){ |
|
| 44 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 45 | + $success = false; |
|
| 46 | + $data = apc_fetch($key, $success); |
|
| 47 | + if($success === false){ |
|
| 48 | + $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | + else{ |
|
| 52 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 53 | + $expire = time(); |
|
| 54 | + if($cacheInfo){ |
|
| 55 | + $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 56 | + } |
|
| 57 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 58 | + return $data; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Save data to the cache |
|
| 65 | - * @param string $key the key to identify this cache data |
|
| 66 | - * @param mixed $data the cache data to be saved |
|
| 67 | - * @param integer $ttl the cache life time |
|
| 68 | - * @return boolean true if success otherwise will return false |
|
| 69 | - */ |
|
| 70 | - public function set($key, $data, $ttl = 0){ |
|
| 71 | - $expire = time() + $ttl; |
|
| 72 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | - $result = apc_store($key, $data, $ttl); |
|
| 74 | - if($result === false){ |
|
| 75 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 78 | - else{ |
|
| 79 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 80 | - return true; |
|
| 81 | - } |
|
| 82 | - } |
|
| 63 | + /** |
|
| 64 | + * Save data to the cache |
|
| 65 | + * @param string $key the key to identify this cache data |
|
| 66 | + * @param mixed $data the cache data to be saved |
|
| 67 | + * @param integer $ttl the cache life time |
|
| 68 | + * @return boolean true if success otherwise will return false |
|
| 69 | + */ |
|
| 70 | + public function set($key, $data, $ttl = 0){ |
|
| 71 | + $expire = time() + $ttl; |
|
| 72 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | + $result = apc_store($key, $data, $ttl); |
|
| 74 | + if($result === false){ |
|
| 75 | + $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | + else{ |
|
| 79 | + $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 80 | + return true; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Delete the cache data for given key |
|
| 87 | - * @param string $key the key for cache to be deleted |
|
| 88 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 89 | - * the cache or the cache with the given key not exist |
|
| 90 | - */ |
|
| 91 | - public function delete($key){ |
|
| 92 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 93 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 94 | - if($cacheInfo === false){ |
|
| 95 | - $this->logger->info('This cache data does not exists skipping'); |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - else{ |
|
| 99 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 100 | - return apc_delete($key) === true; |
|
| 101 | - } |
|
| 102 | - } |
|
| 85 | + /** |
|
| 86 | + * Delete the cache data for given key |
|
| 87 | + * @param string $key the key for cache to be deleted |
|
| 88 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 89 | + * the cache or the cache with the given key not exist |
|
| 90 | + */ |
|
| 91 | + public function delete($key){ |
|
| 92 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 93 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 94 | + if($cacheInfo === false){ |
|
| 95 | + $this->logger->info('This cache data does not exists skipping'); |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + else{ |
|
| 99 | + $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 100 | + return apc_delete($key) === true; |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Get the cache information for given key |
|
| 106 | - * @param string $key the key for cache to get the information for |
|
| 107 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 108 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 109 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 110 | - * 'ttl' => the time to live of the cache in second |
|
| 111 | - */ |
|
| 112 | - public function getInfo($key){ |
|
| 113 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 114 | - $cacheInfos = $this->_getCacheInfo($key); |
|
| 115 | - if($cacheInfos){ |
|
| 116 | - $data = array( |
|
| 117 | - 'mtime' => $cacheInfos['creation_time'], |
|
| 118 | - 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 119 | - 'ttl' => $cacheInfos['ttl'] |
|
| 120 | - ); |
|
| 121 | - return $data; |
|
| 122 | - } |
|
| 123 | - else{ |
|
| 124 | - $this->logger->info('This cache does not exists skipping'); |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - } |
|
| 104 | + /** |
|
| 105 | + * Get the cache information for given key |
|
| 106 | + * @param string $key the key for cache to get the information for |
|
| 107 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 108 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 109 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 110 | + * 'ttl' => the time to live of the cache in second |
|
| 111 | + */ |
|
| 112 | + public function getInfo($key){ |
|
| 113 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 114 | + $cacheInfos = $this->_getCacheInfo($key); |
|
| 115 | + if($cacheInfos){ |
|
| 116 | + $data = array( |
|
| 117 | + 'mtime' => $cacheInfos['creation_time'], |
|
| 118 | + 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 119 | + 'ttl' => $cacheInfos['ttl'] |
|
| 120 | + ); |
|
| 121 | + return $data; |
|
| 122 | + } |
|
| 123 | + else{ |
|
| 124 | + $this->logger->info('This cache does not exists skipping'); |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Used to delete expired cache data |
|
| 132 | - */ |
|
| 133 | - public function deleteExpiredCache(){ |
|
| 134 | - //for APC[u] is done automatically |
|
| 135 | - return true; |
|
| 136 | - } |
|
| 130 | + /** |
|
| 131 | + * Used to delete expired cache data |
|
| 132 | + */ |
|
| 133 | + public function deleteExpiredCache(){ |
|
| 134 | + //for APC[u] is done automatically |
|
| 135 | + return true; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Remove all cache data |
|
| 140 | - */ |
|
| 141 | - public function clean(){ |
|
| 142 | - $this->logger->debug('Deleting of all cache data'); |
|
| 143 | - $cacheInfos = apc_cache_info('user'); |
|
| 144 | - if(empty($cacheInfos['cache_list'])){ |
|
| 145 | - $this->logger->info('No cache data were found skipping'); |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - else{ |
|
| 149 | - $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 150 | - return apc_clear_cache('user'); |
|
| 151 | - } |
|
| 152 | - } |
|
| 138 | + /** |
|
| 139 | + * Remove all cache data |
|
| 140 | + */ |
|
| 141 | + public function clean(){ |
|
| 142 | + $this->logger->debug('Deleting of all cache data'); |
|
| 143 | + $cacheInfos = apc_cache_info('user'); |
|
| 144 | + if(empty($cacheInfos['cache_list'])){ |
|
| 145 | + $this->logger->info('No cache data were found skipping'); |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + else{ |
|
| 149 | + $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 150 | + return apc_clear_cache('user'); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Check whether the cache feature for the handle is supported |
|
| 157 | - * |
|
| 158 | - * @return bool |
|
| 159 | - */ |
|
| 160 | - public function isSupported(){ |
|
| 161 | - return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 162 | - } |
|
| 155 | + /** |
|
| 156 | + * Check whether the cache feature for the handle is supported |
|
| 157 | + * |
|
| 158 | + * @return bool |
|
| 159 | + */ |
|
| 160 | + public function isSupported(){ |
|
| 161 | + return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Return the array of cache information |
|
| 166 | - * |
|
| 167 | - * @param string $key the cache key to get the cache information |
|
| 168 | - * @return boolean|array |
|
| 169 | - */ |
|
| 170 | - private function _getCacheInfo($key){ |
|
| 171 | - $caches = apc_cache_info('user'); |
|
| 172 | - if(! empty($caches['cache_list'])){ |
|
| 173 | - $cacheLists = $caches['cache_list']; |
|
| 174 | - foreach ($cacheLists as $c){ |
|
| 175 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 176 | - return $c; |
|
| 177 | - } |
|
| 178 | - } |
|
| 164 | + /** |
|
| 165 | + * Return the array of cache information |
|
| 166 | + * |
|
| 167 | + * @param string $key the cache key to get the cache information |
|
| 168 | + * @return boolean|array |
|
| 169 | + */ |
|
| 170 | + private function _getCacheInfo($key){ |
|
| 171 | + $caches = apc_cache_info('user'); |
|
| 172 | + if(! empty($caches['cache_list'])){ |
|
| 173 | + $cacheLists = $caches['cache_list']; |
|
| 174 | + foreach ($cacheLists as $c){ |
|
| 175 | + if(isset($c['info']) && $c['info'] === $key){ |
|
| 176 | + return $c; |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - } |
|
| 181 | - return false; |
|
| 182 | - } |
|
| 183 | - } |
|
| 180 | + } |
|
| 181 | + return false; |
|
| 182 | + } |
|
| 183 | + } |
|
@@ -1,283 +1,283 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class FileCache extends BaseClass implements CacheInterface{ |
|
| 27 | + class FileCache extends BaseClass implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Whether to enable compression of the cache data file. |
|
| 31 | - * @var boolean |
|
| 32 | - */ |
|
| 33 | - private $compressCacheData = true; |
|
| 29 | + /** |
|
| 30 | + * Whether to enable compression of the cache data file. |
|
| 31 | + * @var boolean |
|
| 32 | + */ |
|
| 33 | + private $compressCacheData = true; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Class constructor |
|
| 37 | - */ |
|
| 38 | - public function __construct(){ |
|
| 39 | - parent::__construct(); |
|
| 40 | - if(! $this->isSupported()){ |
|
| 41 | - show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * Class constructor |
|
| 37 | + */ |
|
| 38 | + public function __construct(){ |
|
| 39 | + parent::__construct(); |
|
| 40 | + if(! $this->isSupported()){ |
|
| 41 | + show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 45 | - if(! extension_loaded('zlib')){ |
|
| 46 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 47 | - $this->compressCacheData = false; |
|
| 48 | - } |
|
| 49 | - } |
|
| 44 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 45 | + if(! extension_loaded('zlib')){ |
|
| 46 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 47 | + $this->compressCacheData = false; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * This is used to get the cache data using the key |
|
| 53 | - * @param string $key the key to identify the cache data |
|
| 54 | - * @return mixed the cache data if exists else return false |
|
| 55 | - */ |
|
| 56 | - public function get($key){ |
|
| 57 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 58 | - $filePath = $this->getFilePath($key); |
|
| 59 | - if(! file_exists($filePath)){ |
|
| 60 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 64 | - $handle = fopen($filePath,'r'); |
|
| 65 | - if(! is_resource($handle)){ |
|
| 66 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 67 | - return false; |
|
| 68 | - } |
|
| 69 | - // Getting a shared lock |
|
| 70 | - flock($handle, LOCK_SH); |
|
| 71 | - $data = file_get_contents($filePath); |
|
| 72 | - fclose($handle); |
|
| 73 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 74 | - if (! $data) { |
|
| 75 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 76 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 77 | - unlink($filePath); |
|
| 78 | - return false; |
|
| 79 | - } |
|
| 80 | - if (time() > $data['expire']) { |
|
| 81 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 82 | - // Unlinking when the file was expired |
|
| 83 | - unlink($filePath); |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - else{ |
|
| 87 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 88 | - return $data['data']; |
|
| 89 | - } |
|
| 90 | - } |
|
| 51 | + /** |
|
| 52 | + * This is used to get the cache data using the key |
|
| 53 | + * @param string $key the key to identify the cache data |
|
| 54 | + * @return mixed the cache data if exists else return false |
|
| 55 | + */ |
|
| 56 | + public function get($key){ |
|
| 57 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 58 | + $filePath = $this->getFilePath($key); |
|
| 59 | + if(! file_exists($filePath)){ |
|
| 60 | + $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 64 | + $handle = fopen($filePath,'r'); |
|
| 65 | + if(! is_resource($handle)){ |
|
| 66 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 67 | + return false; |
|
| 68 | + } |
|
| 69 | + // Getting a shared lock |
|
| 70 | + flock($handle, LOCK_SH); |
|
| 71 | + $data = file_get_contents($filePath); |
|
| 72 | + fclose($handle); |
|
| 73 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 74 | + if (! $data) { |
|
| 75 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 76 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 77 | + unlink($filePath); |
|
| 78 | + return false; |
|
| 79 | + } |
|
| 80 | + if (time() > $data['expire']) { |
|
| 81 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 82 | + // Unlinking when the file was expired |
|
| 83 | + unlink($filePath); |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + else{ |
|
| 87 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 88 | + return $data['data']; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Save data to the cache |
|
| 95 | - * @param string $key the key to identify this cache data |
|
| 96 | - * @param mixed $data the cache data |
|
| 97 | - * @param integer $ttl the cache life time |
|
| 98 | - * @return boolean true if success otherwise will return false |
|
| 99 | - */ |
|
| 100 | - public function set($key, $data, $ttl = 0){ |
|
| 101 | - $expire = time() + $ttl; |
|
| 102 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 103 | - $filePath = $this->getFilePath($key); |
|
| 104 | - $handle = fopen($filePath,'w'); |
|
| 105 | - if(! is_resource($handle)){ |
|
| 106 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 110 | - //Serializing along with the TTL |
|
| 111 | - $cacheData = serialize(array( |
|
| 112 | - 'mtime' => time(), |
|
| 113 | - 'expire' => $expire, |
|
| 114 | - 'data' => $data, |
|
| 115 | - 'ttl' => $ttl |
|
| 116 | - ) |
|
| 117 | - ); |
|
| 118 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 119 | - if(! $result){ |
|
| 120 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 121 | - fclose($handle); |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - else{ |
|
| 125 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 126 | - fclose($handle); |
|
| 127 | - chmod($filePath, 0640); |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - } |
|
| 93 | + /** |
|
| 94 | + * Save data to the cache |
|
| 95 | + * @param string $key the key to identify this cache data |
|
| 96 | + * @param mixed $data the cache data |
|
| 97 | + * @param integer $ttl the cache life time |
|
| 98 | + * @return boolean true if success otherwise will return false |
|
| 99 | + */ |
|
| 100 | + public function set($key, $data, $ttl = 0){ |
|
| 101 | + $expire = time() + $ttl; |
|
| 102 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 103 | + $filePath = $this->getFilePath($key); |
|
| 104 | + $handle = fopen($filePath,'w'); |
|
| 105 | + if(! is_resource($handle)){ |
|
| 106 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 110 | + //Serializing along with the TTL |
|
| 111 | + $cacheData = serialize(array( |
|
| 112 | + 'mtime' => time(), |
|
| 113 | + 'expire' => $expire, |
|
| 114 | + 'data' => $data, |
|
| 115 | + 'ttl' => $ttl |
|
| 116 | + ) |
|
| 117 | + ); |
|
| 118 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 119 | + if(! $result){ |
|
| 120 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 121 | + fclose($handle); |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + else{ |
|
| 125 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 126 | + fclose($handle); |
|
| 127 | + chmod($filePath, 0640); |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Delete the cache data for given key |
|
| 135 | - * @param string $key the key for cache to be deleted |
|
| 136 | - * @return boolean true if the cache is delete, false if can't delete |
|
| 137 | - * the cache or the cache with the given key not exist |
|
| 138 | - */ |
|
| 139 | - public function delete($key){ |
|
| 140 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 141 | - $filePath = $this->getFilePath($key); |
|
| 142 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 143 | - if(! file_exists($filePath)){ |
|
| 144 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - else{ |
|
| 148 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 149 | - unlink($filePath); |
|
| 150 | - return true; |
|
| 151 | - } |
|
| 152 | - } |
|
| 133 | + /** |
|
| 134 | + * Delete the cache data for given key |
|
| 135 | + * @param string $key the key for cache to be deleted |
|
| 136 | + * @return boolean true if the cache is delete, false if can't delete |
|
| 137 | + * the cache or the cache with the given key not exist |
|
| 138 | + */ |
|
| 139 | + public function delete($key){ |
|
| 140 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 141 | + $filePath = $this->getFilePath($key); |
|
| 142 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 143 | + if(! file_exists($filePath)){ |
|
| 144 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + else{ |
|
| 148 | + $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 149 | + unlink($filePath); |
|
| 150 | + return true; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Get the cache information for given key |
|
| 156 | - * @param string $key the key for cache to get the information for |
|
| 157 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 158 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 159 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 160 | - * 'ttl' => the time to live of the cache in second |
|
| 161 | - */ |
|
| 162 | - public function getInfo($key){ |
|
| 163 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 164 | - $filePath = $this->getFilePath($key); |
|
| 165 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 166 | - if(! file_exists($filePath)){ |
|
| 167 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 168 | - return false; |
|
| 169 | - } |
|
| 170 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 171 | - $data = file_get_contents($filePath); |
|
| 172 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 173 | - if(! $data){ |
|
| 174 | - $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 177 | - $this->logger->info('This cache data is OK check for expire'); |
|
| 178 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 179 | - $this->logger->info('This cache not yet expired return cache informations'); |
|
| 180 | - $info = array( |
|
| 181 | - 'mtime' => $data['mtime'], |
|
| 182 | - 'expire' => $data['expire'], |
|
| 183 | - 'ttl' => $data['ttl'] |
|
| 184 | - ); |
|
| 185 | - return $info; |
|
| 186 | - } |
|
| 187 | - $this->logger->info('This cache already expired return false'); |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 154 | + /** |
|
| 155 | + * Get the cache information for given key |
|
| 156 | + * @param string $key the key for cache to get the information for |
|
| 157 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 158 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 159 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 160 | + * 'ttl' => the time to live of the cache in second |
|
| 161 | + */ |
|
| 162 | + public function getInfo($key){ |
|
| 163 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 164 | + $filePath = $this->getFilePath($key); |
|
| 165 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 166 | + if(! file_exists($filePath)){ |
|
| 167 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 168 | + return false; |
|
| 169 | + } |
|
| 170 | + $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 171 | + $data = file_get_contents($filePath); |
|
| 172 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 173 | + if(! $data){ |
|
| 174 | + $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | + $this->logger->info('This cache data is OK check for expire'); |
|
| 178 | + if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 179 | + $this->logger->info('This cache not yet expired return cache informations'); |
|
| 180 | + $info = array( |
|
| 181 | + 'mtime' => $data['mtime'], |
|
| 182 | + 'expire' => $data['expire'], |
|
| 183 | + 'ttl' => $data['ttl'] |
|
| 184 | + ); |
|
| 185 | + return $info; |
|
| 186 | + } |
|
| 187 | + $this->logger->info('This cache already expired return false'); |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Used to delete expired cache data |
|
| 194 | - */ |
|
| 195 | - public function deleteExpiredCache(){ |
|
| 196 | - $this->logger->debug('Deleting of expired cache files'); |
|
| 197 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 198 | - if(! $list){ |
|
| 199 | - $this->logger->info('No cache files were found skipping'); |
|
| 200 | - } |
|
| 201 | - else{ |
|
| 202 | - $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 203 | - foreach ($list as $file) { |
|
| 204 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 205 | - $data = file_get_contents($file); |
|
| 206 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 207 | - if(! $data){ |
|
| 208 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 209 | - } |
|
| 210 | - else if(time() > $data['expire']){ |
|
| 211 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 212 | - unlink($file); |
|
| 213 | - } |
|
| 214 | - else{ |
|
| 215 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - } |
|
| 192 | + /** |
|
| 193 | + * Used to delete expired cache data |
|
| 194 | + */ |
|
| 195 | + public function deleteExpiredCache(){ |
|
| 196 | + $this->logger->debug('Deleting of expired cache files'); |
|
| 197 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 198 | + if(! $list){ |
|
| 199 | + $this->logger->info('No cache files were found skipping'); |
|
| 200 | + } |
|
| 201 | + else{ |
|
| 202 | + $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 203 | + foreach ($list as $file) { |
|
| 204 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 205 | + $data = file_get_contents($file); |
|
| 206 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 207 | + if(! $data){ |
|
| 208 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 209 | + } |
|
| 210 | + else if(time() > $data['expire']){ |
|
| 211 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 212 | + unlink($file); |
|
| 213 | + } |
|
| 214 | + else{ |
|
| 215 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Remove all file from cache folder |
|
| 223 | - */ |
|
| 224 | - public function clean(){ |
|
| 225 | - $this->logger->debug('Deleting of all cache files'); |
|
| 226 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 227 | - if(! $list){ |
|
| 228 | - $this->logger->info('No cache files were found skipping'); |
|
| 229 | - } |
|
| 230 | - else{ |
|
| 231 | - $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 232 | - foreach ($list as $file) { |
|
| 233 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 234 | - unlink($file); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 221 | + /** |
|
| 222 | + * Remove all file from cache folder |
|
| 223 | + */ |
|
| 224 | + public function clean(){ |
|
| 225 | + $this->logger->debug('Deleting of all cache files'); |
|
| 226 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 227 | + if(! $list){ |
|
| 228 | + $this->logger->info('No cache files were found skipping'); |
|
| 229 | + } |
|
| 230 | + else{ |
|
| 231 | + $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 232 | + foreach ($list as $file) { |
|
| 233 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 234 | + unlink($file); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * @return boolean |
|
| 241 | - */ |
|
| 242 | - public function isCompressCacheData(){ |
|
| 243 | - return $this->compressCacheData; |
|
| 244 | - } |
|
| 239 | + /** |
|
| 240 | + * @return boolean |
|
| 241 | + */ |
|
| 242 | + public function isCompressCacheData(){ |
|
| 243 | + return $this->compressCacheData; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * @param boolean $compressCacheData |
|
| 248 | - * |
|
| 249 | - * @return object |
|
| 250 | - */ |
|
| 251 | - public function setCompressCacheData($status = true){ |
|
| 252 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 253 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 246 | + /** |
|
| 247 | + * @param boolean $compressCacheData |
|
| 248 | + * |
|
| 249 | + * @return object |
|
| 250 | + */ |
|
| 251 | + public function setCompressCacheData($status = true){ |
|
| 252 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 253 | + if($status === true && ! extension_loaded('zlib')){ |
|
| 254 | 254 | |
| 255 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 256 | - $this->compressCacheData = false; |
|
| 257 | - } |
|
| 258 | - else{ |
|
| 259 | - $this->compressCacheData = $status; |
|
| 260 | - } |
|
| 261 | - return $this; |
|
| 262 | - } |
|
| 255 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 256 | + $this->compressCacheData = false; |
|
| 257 | + } |
|
| 258 | + else{ |
|
| 259 | + $this->compressCacheData = $status; |
|
| 260 | + } |
|
| 261 | + return $this; |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - /** |
|
| 265 | - * Check whether the cache feature for the handle is supported |
|
| 266 | - * |
|
| 267 | - * @return bool |
|
| 268 | - */ |
|
| 269 | - public function isSupported(){ |
|
| 270 | - return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 271 | - } |
|
| 264 | + /** |
|
| 265 | + * Check whether the cache feature for the handle is supported |
|
| 266 | + * |
|
| 267 | + * @return bool |
|
| 268 | + */ |
|
| 269 | + public function isSupported(){ |
|
| 270 | + return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Get the cache file full path for the given key |
|
| 276 | - * |
|
| 277 | - * @param string $key the cache item key |
|
| 278 | - * @return string the full cache file path for this key |
|
| 279 | - */ |
|
| 280 | - private function getFilePath($key){ |
|
| 281 | - return CACHE_PATH . md5($key) . '.cache'; |
|
| 282 | - } |
|
| 283 | - } |
|
| 274 | + /** |
|
| 275 | + * Get the cache file full path for the given key |
|
| 276 | + * |
|
| 277 | + * @param string $key the cache item key |
|
| 278 | + * @return string the full cache file path for this key |
|
| 279 | + */ |
|
| 280 | + private function getFilePath($key){ |
|
| 281 | + return CACHE_PATH . md5($key) . '.cache'; |
|
| 282 | + } |
|
| 283 | + } |
|
@@ -1,126 +1,126 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Controller extends BaseClass{ |
|
| 27 | + class Controller extends BaseClass{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The name of the module if this controller belong to an module |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - public $moduleName = null; |
|
| 29 | + /** |
|
| 30 | + * The name of the module if this controller belong to an module |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + public $moduleName = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The singleton of the super object |
|
| 37 | - * @var Controller |
|
| 38 | - */ |
|
| 39 | - private static $instance; |
|
| 35 | + /** |
|
| 36 | + * The singleton of the super object |
|
| 37 | + * @var Controller |
|
| 38 | + */ |
|
| 39 | + private static $instance; |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Class constructor |
|
| 44 | - */ |
|
| 45 | - public function __construct(){ |
|
| 46 | - parent::__construct(); |
|
| 42 | + /** |
|
| 43 | + * Class constructor |
|
| 44 | + */ |
|
| 45 | + public function __construct(){ |
|
| 46 | + parent::__construct(); |
|
| 47 | 47 | |
| 48 | - //instance of the super object |
|
| 49 | - self::$instance = & $this; |
|
| 48 | + //instance of the super object |
|
| 49 | + self::$instance = & $this; |
|
| 50 | 50 | |
| 51 | - //Load the resources loaded during the application bootstrap |
|
| 52 | - $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 53 | - foreach (class_loaded() as $var => $class){ |
|
| 54 | - $this->$var =& class_loader($class); |
|
| 55 | - } |
|
| 51 | + //Load the resources loaded during the application bootstrap |
|
| 52 | + $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 53 | + foreach (class_loaded() as $var => $class){ |
|
| 54 | + $this->$var =& class_loader($class); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - //set module using the router |
|
| 58 | - $this->setModuleNameFromRouter(); |
|
| 57 | + //set module using the router |
|
| 58 | + $this->setModuleNameFromRouter(); |
|
| 59 | 59 | |
| 60 | - //load the required resources |
|
| 61 | - $this->loadRequiredResources(); |
|
| 60 | + //load the required resources |
|
| 61 | + $this->loadRequiredResources(); |
|
| 62 | 62 | |
| 63 | - //set the cache using the configuration |
|
| 64 | - $this->setCacheFromParamOrConfig(null); |
|
| 63 | + //set the cache using the configuration |
|
| 64 | + $this->setCacheFromParamOrConfig(null); |
|
| 65 | 65 | |
| 66 | - //set application session configuration |
|
| 67 | - $this->logger->debug('Setting PHP application session handler'); |
|
| 68 | - set_session_config(); |
|
| 66 | + //set application session configuration |
|
| 67 | + $this->logger->debug('Setting PHP application session handler'); |
|
| 68 | + set_session_config(); |
|
| 69 | 69 | |
| 70 | - //dispatch the loaded instance of super controller event |
|
| 71 | - $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 72 | - } |
|
| 70 | + //dispatch the loaded instance of super controller event |
|
| 71 | + $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * This is a very useful method it's used to get the super object instance |
|
| 77 | - * @return Controller the super object instance |
|
| 78 | - */ |
|
| 79 | - public static function &get_instance(){ |
|
| 80 | - return self::$instance; |
|
| 81 | - } |
|
| 75 | + /** |
|
| 76 | + * This is a very useful method it's used to get the super object instance |
|
| 77 | + * @return Controller the super object instance |
|
| 78 | + */ |
|
| 79 | + public static function &get_instance(){ |
|
| 80 | + return self::$instance; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * This method is used to set the module name |
|
| 85 | - */ |
|
| 86 | - protected function setModuleNameFromRouter(){ |
|
| 87 | - //set the module using the router instance |
|
| 88 | - if(isset($this->router) && $this->router->getModule()){ |
|
| 89 | - $this->moduleName = $this->router->getModule(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * This method is used to set the module name |
|
| 85 | + */ |
|
| 86 | + protected function setModuleNameFromRouter(){ |
|
| 87 | + //set the module using the router instance |
|
| 88 | + if(isset($this->router) && $this->router->getModule()){ |
|
| 89 | + $this->moduleName = $this->router->getModule(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Set the cache using the argument otherwise will use the configuration |
|
| 95 | - * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 96 | - */ |
|
| 97 | - protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 98 | - $this->logger->debug('Setting the cache handler instance'); |
|
| 99 | - //set cache handler instance |
|
| 100 | - if(get_config('cache_enable', false)){ |
|
| 101 | - if ($cache !== null){ |
|
| 102 | - $this->cache = $cache; |
|
| 103 | - } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 104 | - $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 105 | - unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 93 | + /** |
|
| 94 | + * Set the cache using the argument otherwise will use the configuration |
|
| 95 | + * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 96 | + */ |
|
| 97 | + protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 98 | + $this->logger->debug('Setting the cache handler instance'); |
|
| 99 | + //set cache handler instance |
|
| 100 | + if(get_config('cache_enable', false)){ |
|
| 101 | + if ($cache !== null){ |
|
| 102 | + $this->cache = $cache; |
|
| 103 | + } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 104 | + $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 105 | + unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * This method is used to load the required resources for framework to work |
|
| 113 | - * @return void |
|
| 114 | - */ |
|
| 115 | - private function loadRequiredResources(){ |
|
| 116 | - $this->logger->debug('Loading the required classes into super instance'); |
|
| 117 | - $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 118 | - $this->loader =& class_loader('Loader', 'classes'); |
|
| 119 | - $this->lang =& class_loader('Lang', 'classes'); |
|
| 120 | - $this->request =& class_loader('Request', 'classes'); |
|
| 121 | - //dispatch the request instance created event |
|
| 122 | - $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 123 | - $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 124 | - } |
|
| 111 | + /** |
|
| 112 | + * This method is used to load the required resources for framework to work |
|
| 113 | + * @return void |
|
| 114 | + */ |
|
| 115 | + private function loadRequiredResources(){ |
|
| 116 | + $this->logger->debug('Loading the required classes into super instance'); |
|
| 117 | + $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 118 | + $this->loader =& class_loader('Loader', 'classes'); |
|
| 119 | + $this->lang =& class_loader('Lang', 'classes'); |
|
| 120 | + $this->request =& class_loader('Request', 'classes'); |
|
| 121 | + //dispatch the request instance created event |
|
| 122 | + $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 123 | + $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - } |
|
| 126 | + } |
|
@@ -1,138 +1,138 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Security extends BaseStaticClass{ |
|
| 27 | + class Security extends BaseStaticClass{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * This method is used to generate the CSRF token |
|
| 31 | - * @return string the generated CSRF token |
|
| 32 | - */ |
|
| 33 | - public static function generateCSRF(){ |
|
| 34 | - $logger = self::getLogger(); |
|
| 35 | - $logger->debug('Generation of CSRF ...'); |
|
| 29 | + /** |
|
| 30 | + * This method is used to generate the CSRF token |
|
| 31 | + * @return string the generated CSRF token |
|
| 32 | + */ |
|
| 33 | + public static function generateCSRF(){ |
|
| 34 | + $logger = self::getLogger(); |
|
| 35 | + $logger->debug('Generation of CSRF ...'); |
|
| 36 | 36 | |
| 37 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 38 | - $expire = get_config('csrf_expire', 60); |
|
| 39 | - $keyExpire = 'csrf_expire'; |
|
| 40 | - $currentTime = time(); |
|
| 41 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 42 | - $logger->info('The CSRF token not yet expire just return it'); |
|
| 43 | - return Session::get($key); |
|
| 44 | - } |
|
| 45 | - else{ |
|
| 46 | - $newTime = $currentTime + $expire; |
|
| 47 | - $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 48 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 49 | - Session::set($keyExpire, $newTime); |
|
| 50 | - Session::set($key, $token); |
|
| 51 | - return Session::get($key); |
|
| 52 | - } |
|
| 53 | - } |
|
| 37 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 38 | + $expire = get_config('csrf_expire', 60); |
|
| 39 | + $keyExpire = 'csrf_expire'; |
|
| 40 | + $currentTime = time(); |
|
| 41 | + if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 42 | + $logger->info('The CSRF token not yet expire just return it'); |
|
| 43 | + return Session::get($key); |
|
| 44 | + } |
|
| 45 | + else{ |
|
| 46 | + $newTime = $currentTime + $expire; |
|
| 47 | + $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 48 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 49 | + Session::set($keyExpire, $newTime); |
|
| 50 | + Session::set($key, $token); |
|
| 51 | + return Session::get($key); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 57 | - * @return boolean true if valid, false if not valid |
|
| 58 | - */ |
|
| 59 | - public static function validateCSRF(){ |
|
| 60 | - $logger = self::getLogger(); |
|
| 61 | - $logger->debug('Validation of CSRF ...'); |
|
| 55 | + /** |
|
| 56 | + * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 57 | + * @return boolean true if valid, false if not valid |
|
| 58 | + */ |
|
| 59 | + public static function validateCSRF(){ |
|
| 60 | + $logger = self::getLogger(); |
|
| 61 | + $logger->debug('Validation of CSRF ...'); |
|
| 62 | 62 | |
| 63 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 64 | - $expire = get_config('csrf_expire', 60); |
|
| 65 | - $keyExpire = 'csrf_expire'; |
|
| 66 | - $currentTime = time(); |
|
| 67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 68 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 69 | - $logger->warning('The CSRF session data is not valide'); |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - //perform form data |
|
| 73 | - //need use request->query() for best retrieve |
|
| 74 | - //super instance |
|
| 75 | - $obj = & get_instance(); |
|
| 76 | - $token = $obj->request->query($key); |
|
| 77 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 78 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 82 | - //remove the token from session |
|
| 83 | - Session::clear($key); |
|
| 84 | - Session::clear($keyExpire); |
|
| 85 | - return true; |
|
| 86 | - } |
|
| 63 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 64 | + $expire = get_config('csrf_expire', 60); |
|
| 65 | + $keyExpire = 'csrf_expire'; |
|
| 66 | + $currentTime = time(); |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 68 | + if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 69 | + $logger->warning('The CSRF session data is not valide'); |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + //perform form data |
|
| 73 | + //need use request->query() for best retrieve |
|
| 74 | + //super instance |
|
| 75 | + $obj = & get_instance(); |
|
| 76 | + $token = $obj->request->query($key); |
|
| 77 | + if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 78 | + $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 82 | + //remove the token from session |
|
| 83 | + Session::clear($key); |
|
| 84 | + Session::clear($keyExpire); |
|
| 85 | + return true; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * This method is used to check the whitelist IP address access |
|
| 90 | - */ |
|
| 91 | - public static function checkWhiteListIpAccess(){ |
|
| 92 | - $logger = self::getLogger(); |
|
| 93 | - $logger->debug('Validation of the IP address access ...'); |
|
| 94 | - $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 95 | - $isEnable = get_config('white_list_ip_enable', false); |
|
| 96 | - if($isEnable){ |
|
| 97 | - $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 98 | - $list = get_config('white_list_ip_addresses', array()); |
|
| 99 | - if(! empty($list)){ |
|
| 100 | - //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 101 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 102 | - $ip = get_ip(); |
|
| 103 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 104 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 105 | - //wildcard to access all ip address |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - else{ |
|
| 109 | - // go through all whitelisted ips |
|
| 110 | - foreach ($list as $ipaddr) { |
|
| 111 | - // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 112 | - $wildcardPosition = strpos($ipaddr, '*'); |
|
| 113 | - if ($wildcardPosition === false) { |
|
| 114 | - // no wild card in whitelisted ip --continue searching |
|
| 115 | - continue; |
|
| 116 | - } |
|
| 117 | - // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 118 | - // and add the wold card to get the same pattern |
|
| 119 | - if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 120 | - // f.e. we got |
|
| 121 | - // ip "127.0.0.1" |
|
| 122 | - // whitelisted ip "127.0.*" |
|
| 123 | - // then we compared "127.0.*" with "127.0.*" |
|
| 124 | - // return success |
|
| 125 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 126 | - return; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 130 | - show_error('Access to this application is not allowed'); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - else{ |
|
| 135 | - $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 88 | + /** |
|
| 89 | + * This method is used to check the whitelist IP address access |
|
| 90 | + */ |
|
| 91 | + public static function checkWhiteListIpAccess(){ |
|
| 92 | + $logger = self::getLogger(); |
|
| 93 | + $logger->debug('Validation of the IP address access ...'); |
|
| 94 | + $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 95 | + $isEnable = get_config('white_list_ip_enable', false); |
|
| 96 | + if($isEnable){ |
|
| 97 | + $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 98 | + $list = get_config('white_list_ip_addresses', array()); |
|
| 99 | + if(! empty($list)){ |
|
| 100 | + //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 101 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 102 | + $ip = get_ip(); |
|
| 103 | + if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 104 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 105 | + //wildcard to access all ip address |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + else{ |
|
| 109 | + // go through all whitelisted ips |
|
| 110 | + foreach ($list as $ipaddr) { |
|
| 111 | + // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 112 | + $wildcardPosition = strpos($ipaddr, '*'); |
|
| 113 | + if ($wildcardPosition === false) { |
|
| 114 | + // no wild card in whitelisted ip --continue searching |
|
| 115 | + continue; |
|
| 116 | + } |
|
| 117 | + // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 118 | + // and add the wold card to get the same pattern |
|
| 119 | + if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 120 | + // f.e. we got |
|
| 121 | + // ip "127.0.0.1" |
|
| 122 | + // whitelisted ip "127.0.*" |
|
| 123 | + // then we compared "127.0.*" with "127.0.*" |
|
| 124 | + // return success |
|
| 125 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 126 | + return; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 130 | + show_error('Access to this application is not allowed'); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + else{ |
|
| 135 | + $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
@@ -1,93 +1,93 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class BaseClass{ |
|
| 28 | - /** |
|
| 29 | - * The logger instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 32 | - protected $logger; |
|
| 27 | + class BaseClass{ |
|
| 28 | + /** |
|
| 29 | + * The logger instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | + protected $logger; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Class constructor |
|
| 36 | - */ |
|
| 37 | - public function __construct(){ |
|
| 38 | - //Set Log instance to use |
|
| 39 | - $this->setLoggerFromParamOrCreate(null); |
|
| 40 | - } |
|
| 34 | + /** |
|
| 35 | + * Class constructor |
|
| 36 | + */ |
|
| 37 | + public function __construct(){ |
|
| 38 | + //Set Log instance to use |
|
| 39 | + $this->setLoggerFromParamOrCreate(null); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Set the dependencies instance using argument or create new instance if is null |
|
| 44 | - * @param string $name this class property name. |
|
| 45 | - * @param object $instance the instance. If is not null will use it |
|
| 46 | - * otherwise will create new instance. |
|
| 47 | - * @param string $loadClassName the name of class to load using class_loader function. |
|
| 48 | - * @param string $loadClassPath the path of class to load using class_loader function. |
|
| 49 | - * |
|
| 50 | - * @return object this current instance |
|
| 51 | - */ |
|
| 52 | - protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes'){ |
|
| 53 | - if ($instance !== null){ |
|
| 54 | - $this->{$name} = $instance; |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - $this->{$name} =& class_loader($loadClassName, $loadClassePath); |
|
| 58 | - return $this; |
|
| 59 | - } |
|
| 42 | + /** |
|
| 43 | + * Set the dependencies instance using argument or create new instance if is null |
|
| 44 | + * @param string $name this class property name. |
|
| 45 | + * @param object $instance the instance. If is not null will use it |
|
| 46 | + * otherwise will create new instance. |
|
| 47 | + * @param string $loadClassName the name of class to load using class_loader function. |
|
| 48 | + * @param string $loadClassPath the path of class to load using class_loader function. |
|
| 49 | + * |
|
| 50 | + * @return object this current instance |
|
| 51 | + */ |
|
| 52 | + protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes'){ |
|
| 53 | + if ($instance !== null){ |
|
| 54 | + $this->{$name} = $instance; |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + $this->{$name} =& class_loader($loadClassName, $loadClassePath); |
|
| 58 | + return $this; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Return the Log instance |
|
| 63 | - * @return object |
|
| 64 | - */ |
|
| 65 | - public function getLogger(){ |
|
| 66 | - return $this->logger; |
|
| 67 | - } |
|
| 61 | + /** |
|
| 62 | + * Return the Log instance |
|
| 63 | + * @return object |
|
| 64 | + */ |
|
| 65 | + public function getLogger(){ |
|
| 66 | + return $this->logger; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Set the log instance |
|
| 71 | - * @param object $logger the log object |
|
| 72 | - * @return object Database |
|
| 73 | - */ |
|
| 74 | - public function setLogger($logger){ |
|
| 75 | - $this->logger = $logger; |
|
| 76 | - return $this; |
|
| 77 | - } |
|
| 69 | + /** |
|
| 70 | + * Set the log instance |
|
| 71 | + * @param object $logger the log object |
|
| 72 | + * @return object Database |
|
| 73 | + */ |
|
| 74 | + public function setLogger($logger){ |
|
| 75 | + $this->logger = $logger; |
|
| 76 | + return $this; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Set the Log instance using argument or create new instance |
|
| 81 | - * @param object $logger the Log instance if not null |
|
| 82 | - * |
|
| 83 | - * @return object this current instance |
|
| 84 | - */ |
|
| 85 | - protected function setLoggerFromParamOrCreate(Log $logger = null){ |
|
| 86 | - $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes'); |
|
| 87 | - if ($logger === null){ |
|
| 88 | - $this->logger->setLogger('Class::' . get_class($this)); |
|
| 89 | - } |
|
| 90 | - return $this; |
|
| 91 | - } |
|
| 79 | + /** |
|
| 80 | + * Set the Log instance using argument or create new instance |
|
| 81 | + * @param object $logger the Log instance if not null |
|
| 82 | + * |
|
| 83 | + * @return object this current instance |
|
| 84 | + */ |
|
| 85 | + protected function setLoggerFromParamOrCreate(Log $logger = null){ |
|
| 86 | + $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes'); |
|
| 87 | + if ($logger === null){ |
|
| 88 | + $this->logger->setLogger('Class::' . get_class($this)); |
|
| 89 | + } |
|
| 90 | + return $this; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - } |
|
| 93 | + } |
|
@@ -1,175 +1,175 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | - * also to dispatch the event |
|
| 30 | - */ |
|
| 27 | + /** |
|
| 28 | + * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | + * also to dispatch the event |
|
| 30 | + */ |
|
| 31 | 31 | |
| 32 | - class EventDispatcher extends BaseClass{ |
|
| 32 | + class EventDispatcher extends BaseClass{ |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The list of the registered listeners |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private $listeners = array(); |
|
| 34 | + /** |
|
| 35 | + * The list of the registered listeners |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private $listeners = array(); |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - public function __construct(){ |
|
| 42 | - parent::__construct(); |
|
| 43 | - } |
|
| 41 | + public function __construct(){ |
|
| 42 | + parent::__construct(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Register new listener |
|
| 47 | - * @param string $eventName the name of the event to register for |
|
| 48 | - * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 49 | - */ |
|
| 50 | - public function addListener($eventName, callable $listener){ |
|
| 51 | - $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 52 | - if(! isset($this->listeners[$eventName])){ |
|
| 53 | - $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 54 | - $this->listeners[$eventName] = array(); |
|
| 55 | - } |
|
| 56 | - else{ |
|
| 57 | - $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 58 | - } |
|
| 59 | - $this->listeners[$eventName][] = $listener; |
|
| 60 | - } |
|
| 45 | + /** |
|
| 46 | + * Register new listener |
|
| 47 | + * @param string $eventName the name of the event to register for |
|
| 48 | + * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 49 | + */ |
|
| 50 | + public function addListener($eventName, callable $listener){ |
|
| 51 | + $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 52 | + if(! isset($this->listeners[$eventName])){ |
|
| 53 | + $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 54 | + $this->listeners[$eventName] = array(); |
|
| 55 | + } |
|
| 56 | + else{ |
|
| 57 | + $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 58 | + } |
|
| 59 | + $this->listeners[$eventName][] = $listener; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Remove the event listener from list |
|
| 64 | - * @param string $eventName the event name |
|
| 65 | - * @param callable $listener the listener callback |
|
| 66 | - */ |
|
| 67 | - public function removeListener($eventName, callable $listener){ |
|
| 68 | - $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 69 | - if(isset($this->listeners[$eventName])){ |
|
| 70 | - $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 71 | - if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 72 | - $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 73 | - unset($this->listeners[$eventName][$index]); |
|
| 74 | - } |
|
| 75 | - else{ |
|
| 76 | - $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - else{ |
|
| 80 | - $this->logger->info('This event does not have this listener ignore remove'); |
|
| 81 | - } |
|
| 82 | - } |
|
| 62 | + /** |
|
| 63 | + * Remove the event listener from list |
|
| 64 | + * @param string $eventName the event name |
|
| 65 | + * @param callable $listener the listener callback |
|
| 66 | + */ |
|
| 67 | + public function removeListener($eventName, callable $listener){ |
|
| 68 | + $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 69 | + if(isset($this->listeners[$eventName])){ |
|
| 70 | + $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 71 | + if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 72 | + $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 73 | + unset($this->listeners[$eventName][$index]); |
|
| 74 | + } |
|
| 75 | + else{ |
|
| 76 | + $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + else{ |
|
| 80 | + $this->logger->info('This event does not have this listener ignore remove'); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 86 | - * remove all listeners for this event |
|
| 87 | - * @param string $eventName the event name |
|
| 88 | - */ |
|
| 89 | - public function removeAllListener($eventName = null){ |
|
| 90 | - $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 91 | - if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 92 | - $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 93 | - unset($this->listeners[$eventName]); |
|
| 94 | - } |
|
| 95 | - else{ |
|
| 96 | - $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 97 | - $this->listeners = array(); |
|
| 98 | - } |
|
| 99 | - } |
|
| 84 | + /** |
|
| 85 | + * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 86 | + * remove all listeners for this event |
|
| 87 | + * @param string $eventName the event name |
|
| 88 | + */ |
|
| 89 | + public function removeAllListener($eventName = null){ |
|
| 90 | + $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 91 | + if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 92 | + $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 93 | + unset($this->listeners[$eventName]); |
|
| 94 | + } |
|
| 95 | + else{ |
|
| 96 | + $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 97 | + $this->listeners = array(); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Get the list of listener for this event |
|
| 103 | - * @param string $eventName the event name |
|
| 104 | - * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 105 | - */ |
|
| 106 | - public function getListeners($eventName){ |
|
| 107 | - return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 108 | - } |
|
| 101 | + /** |
|
| 102 | + * Get the list of listener for this event |
|
| 103 | + * @param string $eventName the event name |
|
| 104 | + * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 105 | + */ |
|
| 106 | + public function getListeners($eventName){ |
|
| 107 | + return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Dispatch the event to the registered listeners. |
|
| 112 | - * @param mixed|object $event the event information |
|
| 113 | - * @return void|object if event need return, will return the final EventInfo object. |
|
| 114 | - */ |
|
| 115 | - public function dispatch($event){ |
|
| 116 | - if(! $event || !$event instanceof EventInfo){ |
|
| 117 | - $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 118 | - $event = new EventInfo((string) $event); |
|
| 119 | - } |
|
| 120 | - $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 121 | - if(isset($event->stop) && $event->stop){ |
|
| 122 | - $this->logger->info('This event need stopped, no need call any listener'); |
|
| 123 | - return; |
|
| 124 | - } |
|
| 125 | - if($event->returnBack){ |
|
| 126 | - $this->logger->info('This event need return back, return the result for future use'); |
|
| 127 | - return $this->dispatchToListerners($event); |
|
| 128 | - } |
|
| 129 | - else{ |
|
| 130 | - $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 131 | - $this->dispatchToListerners($event); |
|
| 132 | - } |
|
| 133 | - } |
|
| 110 | + /** |
|
| 111 | + * Dispatch the event to the registered listeners. |
|
| 112 | + * @param mixed|object $event the event information |
|
| 113 | + * @return void|object if event need return, will return the final EventInfo object. |
|
| 114 | + */ |
|
| 115 | + public function dispatch($event){ |
|
| 116 | + if(! $event || !$event instanceof EventInfo){ |
|
| 117 | + $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 118 | + $event = new EventInfo((string) $event); |
|
| 119 | + } |
|
| 120 | + $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 121 | + if(isset($event->stop) && $event->stop){ |
|
| 122 | + $this->logger->info('This event need stopped, no need call any listener'); |
|
| 123 | + return; |
|
| 124 | + } |
|
| 125 | + if($event->returnBack){ |
|
| 126 | + $this->logger->info('This event need return back, return the result for future use'); |
|
| 127 | + return $this->dispatchToListerners($event); |
|
| 128 | + } |
|
| 129 | + else{ |
|
| 130 | + $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 131 | + $this->dispatchToListerners($event); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Dispatch the event to the registered listeners. |
|
| 137 | - * @param object EventInfo $event the event information |
|
| 138 | - * @return void|object if event need return, will return the final EventInfo instance. |
|
| 139 | - */ |
|
| 140 | - private function dispatchToListerners(EventInfo $event){ |
|
| 141 | - $eBackup = $event; |
|
| 142 | - $list = $this->getListeners($event->name); |
|
| 143 | - if(empty($list)){ |
|
| 144 | - $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 145 | - if($event->returnBack){ |
|
| 146 | - return $event; |
|
| 147 | - } |
|
| 148 | - return; |
|
| 149 | - } |
|
| 150 | - else{ |
|
| 151 | - $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 152 | - } |
|
| 153 | - foreach($list as $listener){ |
|
| 154 | - if($eBackup->returnBack){ |
|
| 155 | - $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 156 | - if($returnedEvent instanceof EventInfo){ |
|
| 157 | - $event = $returnedEvent; |
|
| 158 | - } |
|
| 159 | - else{ |
|
| 160 | - show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - call_user_func_array($listener, array($event)); |
|
| 165 | - } |
|
| 166 | - if($event->stop){ |
|
| 167 | - break; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - //only test for original event may be during the flow some listeners change this parameter |
|
| 171 | - if($eBackup->returnBack){ |
|
| 172 | - return $event; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - } |
|
| 135 | + /** |
|
| 136 | + * Dispatch the event to the registered listeners. |
|
| 137 | + * @param object EventInfo $event the event information |
|
| 138 | + * @return void|object if event need return, will return the final EventInfo instance. |
|
| 139 | + */ |
|
| 140 | + private function dispatchToListerners(EventInfo $event){ |
|
| 141 | + $eBackup = $event; |
|
| 142 | + $list = $this->getListeners($event->name); |
|
| 143 | + if(empty($list)){ |
|
| 144 | + $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 145 | + if($event->returnBack){ |
|
| 146 | + return $event; |
|
| 147 | + } |
|
| 148 | + return; |
|
| 149 | + } |
|
| 150 | + else{ |
|
| 151 | + $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 152 | + } |
|
| 153 | + foreach($list as $listener){ |
|
| 154 | + if($eBackup->returnBack){ |
|
| 155 | + $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 156 | + if($returnedEvent instanceof EventInfo){ |
|
| 157 | + $event = $returnedEvent; |
|
| 158 | + } |
|
| 159 | + else{ |
|
| 160 | + show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + call_user_func_array($listener, array($event)); |
|
| 165 | + } |
|
| 166 | + if($event->stop){ |
|
| 167 | + break; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + //only test for original event may be during the flow some listeners change this parameter |
|
| 171 | + if($eBackup->returnBack){ |
|
| 172 | + return $event; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + } |
|