@@ -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 | + } |
|
@@ -1,511 +1,511 @@ |
||
| 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 Response extends BaseStaticClass{ |
|
| 27 | + class Response extends BaseStaticClass{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of request header to send with response |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $headers = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of request header to send with response |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $headers = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The final page content to display to user |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - private $_pageRender = null; |
|
| 35 | + /** |
|
| 36 | + * The final page content to display to user |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + private $_pageRender = null; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The current request URL |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - private $_currentUrl = null; |
|
| 41 | + /** |
|
| 42 | + * The current request URL |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + private $_currentUrl = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The current request URL cache key |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - private $_currentUrlCacheKey = null; |
|
| 47 | + /** |
|
| 48 | + * The current request URL cache key |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + private $_currentUrlCacheKey = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Whether we can compress the output using Gzip |
|
| 55 | - * @var boolean |
|
| 56 | - */ |
|
| 57 | - private static $_canCompressOutput = false; |
|
| 53 | + /** |
|
| 54 | + * Whether we can compress the output using Gzip |
|
| 55 | + * @var boolean |
|
| 56 | + */ |
|
| 57 | + private static $_canCompressOutput = false; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Construct new response instance |
|
| 61 | - */ |
|
| 62 | - public function __construct(){ |
|
| 63 | - $currentUrl = ''; |
|
| 64 | - if (! empty($_SERVER['REQUEST_URI'])){ |
|
| 65 | - $currentUrl = $_SERVER['REQUEST_URI']; |
|
| 66 | - } |
|
| 67 | - if (! empty($_SERVER['QUERY_STRING'])){ |
|
| 68 | - $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
| 69 | - } |
|
| 70 | - $this->_currentUrl = $currentUrl; |
|
| 59 | + /** |
|
| 60 | + * Construct new response instance |
|
| 61 | + */ |
|
| 62 | + public function __construct(){ |
|
| 63 | + $currentUrl = ''; |
|
| 64 | + if (! empty($_SERVER['REQUEST_URI'])){ |
|
| 65 | + $currentUrl = $_SERVER['REQUEST_URI']; |
|
| 66 | + } |
|
| 67 | + if (! empty($_SERVER['QUERY_STRING'])){ |
|
| 68 | + $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
| 69 | + } |
|
| 70 | + $this->_currentUrl = $currentUrl; |
|
| 71 | 71 | |
| 72 | - $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 72 | + $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 73 | 73 | |
| 74 | - self::$_canCompressOutput = get_config('compress_output') |
|
| 75 | - && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 76 | - && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 77 | - && extension_loaded('zlib') |
|
| 78 | - && (bool) ini_get('zlib.output_compression') === false; |
|
| 79 | - } |
|
| 74 | + self::$_canCompressOutput = get_config('compress_output') |
|
| 75 | + && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 76 | + && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 77 | + && extension_loaded('zlib') |
|
| 78 | + && (bool) ini_get('zlib.output_compression') === false; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Send the HTTP Response headers |
|
| 84 | - * @param integer $httpCode the HTTP status code |
|
| 85 | - * @param array $headers the additional headers to add to the existing headers list |
|
| 86 | - */ |
|
| 87 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 88 | - set_http_status_header($httpCode); |
|
| 89 | - self::setHeaders($headers); |
|
| 90 | - if(! headers_sent()){ |
|
| 91 | - foreach(self::getHeaders() as $key => $value){ |
|
| 92 | - header($key .': '.$value); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } |
|
| 82 | + /** |
|
| 83 | + * Send the HTTP Response headers |
|
| 84 | + * @param integer $httpCode the HTTP status code |
|
| 85 | + * @param array $headers the additional headers to add to the existing headers list |
|
| 86 | + */ |
|
| 87 | + public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 88 | + set_http_status_header($httpCode); |
|
| 89 | + self::setHeaders($headers); |
|
| 90 | + if(! headers_sent()){ |
|
| 91 | + foreach(self::getHeaders() as $key => $value){ |
|
| 92 | + header($key .': '.$value); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Get the list of the headers |
|
| 99 | - * @return array the headers list |
|
| 100 | - */ |
|
| 101 | - public static function getHeaders(){ |
|
| 102 | - return self::$headers; |
|
| 103 | - } |
|
| 97 | + /** |
|
| 98 | + * Get the list of the headers |
|
| 99 | + * @return array the headers list |
|
| 100 | + */ |
|
| 101 | + public static function getHeaders(){ |
|
| 102 | + return self::$headers; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Get the header value for the given name |
|
| 107 | - * @param string $name the header name |
|
| 108 | - * @return string|null the header value |
|
| 109 | - */ |
|
| 110 | - public static function getHeader($name){ |
|
| 111 | - if(array_key_exists($name, self::$headers)){ |
|
| 112 | - return self::$headers[$name]; |
|
| 113 | - } |
|
| 114 | - return null; |
|
| 115 | - } |
|
| 105 | + /** |
|
| 106 | + * Get the header value for the given name |
|
| 107 | + * @param string $name the header name |
|
| 108 | + * @return string|null the header value |
|
| 109 | + */ |
|
| 110 | + public static function getHeader($name){ |
|
| 111 | + if(array_key_exists($name, self::$headers)){ |
|
| 112 | + return self::$headers[$name]; |
|
| 113 | + } |
|
| 114 | + return null; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - /** |
|
| 119 | - * Set the header value for the specified name |
|
| 120 | - * @param string $name the header name |
|
| 121 | - * @param string $value the header value to be set |
|
| 122 | - */ |
|
| 123 | - public static function setHeader($name, $value){ |
|
| 124 | - self::$headers[$name] = $value; |
|
| 125 | - } |
|
| 118 | + /** |
|
| 119 | + * Set the header value for the specified name |
|
| 120 | + * @param string $name the header name |
|
| 121 | + * @param string $value the header value to be set |
|
| 122 | + */ |
|
| 123 | + public static function setHeader($name, $value){ |
|
| 124 | + self::$headers[$name] = $value; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Set the headers using array |
|
| 129 | - * @param array $headers the list of the headers to set. |
|
| 130 | - * Note: this will merge with the existing headers |
|
| 131 | - */ |
|
| 132 | - public static function setHeaders(array $headers){ |
|
| 133 | - self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 134 | - } |
|
| 127 | + /** |
|
| 128 | + * Set the headers using array |
|
| 129 | + * @param array $headers the list of the headers to set. |
|
| 130 | + * Note: this will merge with the existing headers |
|
| 131 | + */ |
|
| 132 | + public static function setHeaders(array $headers){ |
|
| 133 | + self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Redirect user to the specified page |
|
| 138 | - * @param string $path the URL or URI to be redirect to |
|
| 139 | - */ |
|
| 140 | - public static function redirect($path = ''){ |
|
| 141 | - $logger = self::getLogger(); |
|
| 142 | - $url = Url::site_url($path); |
|
| 143 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 144 | - if(! headers_sent()){ |
|
| 145 | - header('Location: '.$url); |
|
| 146 | - exit; |
|
| 147 | - } |
|
| 148 | - echo '<script> |
|
| 136 | + /** |
|
| 137 | + * Redirect user to the specified page |
|
| 138 | + * @param string $path the URL or URI to be redirect to |
|
| 139 | + */ |
|
| 140 | + public static function redirect($path = ''){ |
|
| 141 | + $logger = self::getLogger(); |
|
| 142 | + $url = Url::site_url($path); |
|
| 143 | + $logger->info('Redirect to URL [' .$url. ']'); |
|
| 144 | + if(! headers_sent()){ |
|
| 145 | + header('Location: '.$url); |
|
| 146 | + exit; |
|
| 147 | + } |
|
| 148 | + echo '<script> |
|
| 149 | 149 | location.href = "'.$url.'"; |
| 150 | 150 | </script>'; |
| 151 | - } |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Render the view to display later or return the content |
|
| 155 | - * @param string $view the view name or path |
|
| 156 | - * @param array|object $data the variable data to use in the view |
|
| 157 | - * @param boolean $return whether to return the view generated content or display it directly |
|
| 158 | - * @return void|string if $return is true will return the view content otherwise |
|
| 159 | - * will display the view content. |
|
| 160 | - */ |
|
| 161 | - public function render($view, $data = null, $return = false){ |
|
| 162 | - $logger = self::getLogger(); |
|
| 163 | - //convert data to an array |
|
| 164 | - $data = (array) $data; |
|
| 165 | - $view = str_ireplace('.php', '', $view); |
|
| 166 | - $view = trim($view, '/\\'); |
|
| 167 | - $viewFile = $view . '.php'; |
|
| 168 | - $path = APPS_VIEWS_PATH . $viewFile; |
|
| 153 | + /** |
|
| 154 | + * Render the view to display later or return the content |
|
| 155 | + * @param string $view the view name or path |
|
| 156 | + * @param array|object $data the variable data to use in the view |
|
| 157 | + * @param boolean $return whether to return the view generated content or display it directly |
|
| 158 | + * @return void|string if $return is true will return the view content otherwise |
|
| 159 | + * will display the view content. |
|
| 160 | + */ |
|
| 161 | + public function render($view, $data = null, $return = false){ |
|
| 162 | + $logger = self::getLogger(); |
|
| 163 | + //convert data to an array |
|
| 164 | + $data = (array) $data; |
|
| 165 | + $view = str_ireplace('.php', '', $view); |
|
| 166 | + $view = trim($view, '/\\'); |
|
| 167 | + $viewFile = $view . '.php'; |
|
| 168 | + $path = APPS_VIEWS_PATH . $viewFile; |
|
| 169 | 169 | |
| 170 | - //check in module first |
|
| 171 | - $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 172 | - $moduleInfo = $this->getModuleInfoForView($view); |
|
| 173 | - $module = $moduleInfo['module']; |
|
| 174 | - $view = $moduleInfo['view']; |
|
| 170 | + //check in module first |
|
| 171 | + $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 172 | + $moduleInfo = $this->getModuleInfoForView($view); |
|
| 173 | + $module = $moduleInfo['module']; |
|
| 174 | + $view = $moduleInfo['view']; |
|
| 175 | 175 | |
| 176 | - $moduleViewPath = Module::findViewFullPath($view, $module); |
|
| 177 | - if($moduleViewPath){ |
|
| 178 | - $path = $moduleViewPath; |
|
| 179 | - $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 180 | - } |
|
| 181 | - else{ |
|
| 182 | - $logger->info('Cannot find view [' . $view . '] in module [' .$module. '] using the default location'); |
|
| 183 | - } |
|
| 176 | + $moduleViewPath = Module::findViewFullPath($view, $module); |
|
| 177 | + if($moduleViewPath){ |
|
| 178 | + $path = $moduleViewPath; |
|
| 179 | + $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 180 | + } |
|
| 181 | + else{ |
|
| 182 | + $logger->info('Cannot find view [' . $view . '] in module [' .$module. '] using the default location'); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 185 | + $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 186 | 186 | |
| 187 | - ///////// |
|
| 188 | - if($return){ |
|
| 189 | - return $this->loadView($path, $data, true); |
|
| 190 | - } |
|
| 191 | - $this->loadView($path, $data, false); |
|
| 192 | - } |
|
| 187 | + ///////// |
|
| 188 | + if($return){ |
|
| 189 | + return $this->loadView($path, $data, true); |
|
| 190 | + } |
|
| 191 | + $this->loadView($path, $data, false); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Send the final page output to user |
|
| 197 | - */ |
|
| 198 | - public function renderFinalPage(){ |
|
| 199 | - $logger = self::getLogger(); |
|
| 200 | - $obj = & get_instance(); |
|
| 201 | - $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 202 | - $dispatcher = $obj->eventdispatcher; |
|
| 203 | - $content = $this->_pageRender; |
|
| 204 | - if(! $content){ |
|
| 205 | - $logger->warning('The final view content is empty.'); |
|
| 206 | - return; |
|
| 207 | - } |
|
| 208 | - //dispatch |
|
| 209 | - $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 210 | - $content = null; |
|
| 211 | - if(! empty($event->payload)){ |
|
| 212 | - $content = $event->payload; |
|
| 213 | - } |
|
| 214 | - if(empty($content)){ |
|
| 215 | - $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 216 | - } |
|
| 217 | - //remove unsed space in the content |
|
| 218 | - $content = preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 219 | - //check whether need save the page into cache. |
|
| 220 | - if($cachePageStatus){ |
|
| 221 | - $this->savePageContentIntoCache($content); |
|
| 222 | - } |
|
| 223 | - $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 195 | + /** |
|
| 196 | + * Send the final page output to user |
|
| 197 | + */ |
|
| 198 | + public function renderFinalPage(){ |
|
| 199 | + $logger = self::getLogger(); |
|
| 200 | + $obj = & get_instance(); |
|
| 201 | + $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 202 | + $dispatcher = $obj->eventdispatcher; |
|
| 203 | + $content = $this->_pageRender; |
|
| 204 | + if(! $content){ |
|
| 205 | + $logger->warning('The final view content is empty.'); |
|
| 206 | + return; |
|
| 207 | + } |
|
| 208 | + //dispatch |
|
| 209 | + $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 210 | + $content = null; |
|
| 211 | + if(! empty($event->payload)){ |
|
| 212 | + $content = $event->payload; |
|
| 213 | + } |
|
| 214 | + if(empty($content)){ |
|
| 215 | + $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 216 | + } |
|
| 217 | + //remove unsed space in the content |
|
| 218 | + $content = preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 219 | + //check whether need save the page into cache. |
|
| 220 | + if($cachePageStatus){ |
|
| 221 | + $this->savePageContentIntoCache($content); |
|
| 222 | + } |
|
| 223 | + $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 224 | 224 | |
| 225 | - //compress the output if is available |
|
| 226 | - $type = null; |
|
| 227 | - if (self::$_canCompressOutput){ |
|
| 228 | - $type = 'ob_gzhandler'; |
|
| 229 | - } |
|
| 230 | - ob_start($type); |
|
| 231 | - self::sendHeaders(200); |
|
| 232 | - echo $content; |
|
| 233 | - ob_end_flush(); |
|
| 234 | - } |
|
| 225 | + //compress the output if is available |
|
| 226 | + $type = null; |
|
| 227 | + if (self::$_canCompressOutput){ |
|
| 228 | + $type = 'ob_gzhandler'; |
|
| 229 | + } |
|
| 230 | + ob_start($type); |
|
| 231 | + self::sendHeaders(200); |
|
| 232 | + echo $content; |
|
| 233 | + ob_end_flush(); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Send the final page output to user if is cached |
|
| 239 | - * @param object $cache the cache instance |
|
| 240 | - * |
|
| 241 | - * @return boolean whether the page content if available or not |
|
| 242 | - */ |
|
| 243 | - public function renderFinalPageFromCache(&$cache){ |
|
| 244 | - $logger = self::getLogger(); |
|
| 245 | - //the current page cache key for identification |
|
| 246 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 237 | + /** |
|
| 238 | + * Send the final page output to user if is cached |
|
| 239 | + * @param object $cache the cache instance |
|
| 240 | + * |
|
| 241 | + * @return boolean whether the page content if available or not |
|
| 242 | + */ |
|
| 243 | + public function renderFinalPageFromCache(&$cache){ |
|
| 244 | + $logger = self::getLogger(); |
|
| 245 | + //the current page cache key for identification |
|
| 246 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 247 | 247 | |
| 248 | - $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
| 249 | - //get the cache information to prepare header to send to browser |
|
| 250 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 251 | - if($cacheInfo){ |
|
| 252 | - $status = $this->sendCacheNotYetExpireInfoToBrowser($cacheInfo); |
|
| 253 | - if($status === false){ |
|
| 254 | - return $this->sendCachePageContentToBrowser($cache); |
|
| 255 | - } |
|
| 256 | - return true; |
|
| 257 | - } |
|
| 258 | - return false; |
|
| 259 | - } |
|
| 248 | + $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
| 249 | + //get the cache information to prepare header to send to browser |
|
| 250 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 251 | + if($cacheInfo){ |
|
| 252 | + $status = $this->sendCacheNotYetExpireInfoToBrowser($cacheInfo); |
|
| 253 | + if($status === false){ |
|
| 254 | + return $this->sendCachePageContentToBrowser($cache); |
|
| 255 | + } |
|
| 256 | + return true; |
|
| 257 | + } |
|
| 258 | + return false; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | 261 | |
| 262 | - /** |
|
| 263 | - * Get the final page to be rendered |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - public function getFinalPageRendered(){ |
|
| 267 | - return $this->_pageRender; |
|
| 268 | - } |
|
| 262 | + /** |
|
| 263 | + * Get the final page to be rendered |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + public function getFinalPageRendered(){ |
|
| 267 | + return $this->_pageRender; |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Send the HTTP 404 error if can not found the |
|
| 272 | - * routing information for the current request |
|
| 273 | - */ |
|
| 274 | - public static function send404(){ |
|
| 275 | - /********* for logs **************/ |
|
| 276 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 277 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
| 278 | - $logger = self::getLogger(); |
|
| 279 | - $request =& class_loader('Request', 'classes'); |
|
| 280 | - $userAgent =& class_loader('Browser'); |
|
| 281 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 270 | + /** |
|
| 271 | + * Send the HTTP 404 error if can not found the |
|
| 272 | + * routing information for the current request |
|
| 273 | + */ |
|
| 274 | + public static function send404(){ |
|
| 275 | + /********* for logs **************/ |
|
| 276 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 277 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
| 278 | + $logger = self::getLogger(); |
|
| 279 | + $request =& class_loader('Request', 'classes'); |
|
| 280 | + $userAgent =& class_loader('Browser'); |
|
| 281 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 282 | 282 | |
| 283 | - //here can't use Loader::functions just include the helper manually |
|
| 284 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 283 | + //here can't use Loader::functions just include the helper manually |
|
| 284 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 285 | 285 | |
| 286 | - $str = '[404 page not found] : '; |
|
| 287 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 288 | - $logger->error($str); |
|
| 289 | - /***********************************/ |
|
| 290 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
| 291 | - if(file_exists($path)){ |
|
| 292 | - //compress the output if is available |
|
| 293 | - $type = null; |
|
| 294 | - if (self::$_canCompressOutput){ |
|
| 295 | - $type = 'ob_gzhandler'; |
|
| 296 | - } |
|
| 297 | - ob_start($type); |
|
| 298 | - require_once $path; |
|
| 299 | - $output = ob_get_clean(); |
|
| 300 | - self::sendHeaders(404); |
|
| 301 | - echo $output; |
|
| 302 | - } |
|
| 303 | - else{ |
|
| 304 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 305 | - } |
|
| 306 | - } |
|
| 286 | + $str = '[404 page not found] : '; |
|
| 287 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 288 | + $logger->error($str); |
|
| 289 | + /***********************************/ |
|
| 290 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
| 291 | + if(file_exists($path)){ |
|
| 292 | + //compress the output if is available |
|
| 293 | + $type = null; |
|
| 294 | + if (self::$_canCompressOutput){ |
|
| 295 | + $type = 'ob_gzhandler'; |
|
| 296 | + } |
|
| 297 | + ob_start($type); |
|
| 298 | + require_once $path; |
|
| 299 | + $output = ob_get_clean(); |
|
| 300 | + self::sendHeaders(404); |
|
| 301 | + echo $output; |
|
| 302 | + } |
|
| 303 | + else{ |
|
| 304 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | - /** |
|
| 309 | - * Display the error to user |
|
| 310 | - * @param array $data the error information |
|
| 311 | - */ |
|
| 312 | - public static function sendError(array $data = array()){ |
|
| 313 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 314 | - if(file_exists($path)){ |
|
| 315 | - //compress the output if is available |
|
| 316 | - $type = null; |
|
| 317 | - if (self::$_canCompressOutput){ |
|
| 318 | - $type = 'ob_gzhandler'; |
|
| 319 | - } |
|
| 320 | - ob_start($type); |
|
| 321 | - extract($data); |
|
| 322 | - require_once $path; |
|
| 323 | - $output = ob_get_clean(); |
|
| 324 | - self::sendHeaders(503); |
|
| 325 | - echo $output; |
|
| 326 | - } |
|
| 327 | - else{ |
|
| 328 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 329 | - set_http_status_header(503); |
|
| 330 | - echo 'The error view [' . $path . '] does not exist'; |
|
| 331 | - } |
|
| 332 | - } |
|
| 308 | + /** |
|
| 309 | + * Display the error to user |
|
| 310 | + * @param array $data the error information |
|
| 311 | + */ |
|
| 312 | + public static function sendError(array $data = array()){ |
|
| 313 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 314 | + if(file_exists($path)){ |
|
| 315 | + //compress the output if is available |
|
| 316 | + $type = null; |
|
| 317 | + if (self::$_canCompressOutput){ |
|
| 318 | + $type = 'ob_gzhandler'; |
|
| 319 | + } |
|
| 320 | + ob_start($type); |
|
| 321 | + extract($data); |
|
| 322 | + require_once $path; |
|
| 323 | + $output = ob_get_clean(); |
|
| 324 | + self::sendHeaders(503); |
|
| 325 | + echo $output; |
|
| 326 | + } |
|
| 327 | + else{ |
|
| 328 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 329 | + set_http_status_header(503); |
|
| 330 | + echo 'The error view [' . $path . '] does not exist'; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | 333 | |
| 334 | - /** |
|
| 335 | - * Send the cache not yet expire to browser |
|
| 336 | - * @param array $cacheInfo the cache information |
|
| 337 | - * @return boolean true if the information is sent otherwise false |
|
| 338 | - */ |
|
| 339 | - protected function sendCacheNotYetExpireInfoToBrowser($cacheInfo){ |
|
| 340 | - if(! empty($cacheInfo)){ |
|
| 341 | - $logger = self::getLogger(); |
|
| 342 | - $lastModified = $cacheInfo['mtime']; |
|
| 343 | - $expire = $cacheInfo['expire']; |
|
| 344 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 345 | - self::setHeader('Pragma', 'public'); |
|
| 346 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 347 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 348 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 349 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 350 | - $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
| 351 | - self::sendHeaders(304); |
|
| 352 | - return true; |
|
| 353 | - } |
|
| 354 | - } |
|
| 355 | - return false; |
|
| 356 | - } |
|
| 334 | + /** |
|
| 335 | + * Send the cache not yet expire to browser |
|
| 336 | + * @param array $cacheInfo the cache information |
|
| 337 | + * @return boolean true if the information is sent otherwise false |
|
| 338 | + */ |
|
| 339 | + protected function sendCacheNotYetExpireInfoToBrowser($cacheInfo){ |
|
| 340 | + if(! empty($cacheInfo)){ |
|
| 341 | + $logger = self::getLogger(); |
|
| 342 | + $lastModified = $cacheInfo['mtime']; |
|
| 343 | + $expire = $cacheInfo['expire']; |
|
| 344 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 345 | + self::setHeader('Pragma', 'public'); |
|
| 346 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 347 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 348 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 349 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 350 | + $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
| 351 | + self::sendHeaders(304); |
|
| 352 | + return true; |
|
| 353 | + } |
|
| 354 | + } |
|
| 355 | + return false; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Set the value of '{elapsed_time}' and '{memory_usage}' |
|
| 360 | - * @param string $content the page content |
|
| 361 | - * @return string the page content after replace |
|
| 362 | - * '{elapsed_time}', '{memory_usage}' |
|
| 363 | - */ |
|
| 364 | - protected function replaceElapseTimeAndMemoryUsage($content){ |
|
| 365 | - //load benchmark class |
|
| 366 | - $benchmark = & class_loader('Benchmark'); |
|
| 358 | + /** |
|
| 359 | + * Set the value of '{elapsed_time}' and '{memory_usage}' |
|
| 360 | + * @param string $content the page content |
|
| 361 | + * @return string the page content after replace |
|
| 362 | + * '{elapsed_time}', '{memory_usage}' |
|
| 363 | + */ |
|
| 364 | + protected function replaceElapseTimeAndMemoryUsage($content){ |
|
| 365 | + //load benchmark class |
|
| 366 | + $benchmark = & class_loader('Benchmark'); |
|
| 367 | 367 | |
| 368 | - // Parse out the elapsed time and memory usage, |
|
| 369 | - // then swap the pseudo-variables with the data |
|
| 370 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 371 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 372 | - return str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 373 | - } |
|
| 368 | + // Parse out the elapsed time and memory usage, |
|
| 369 | + // then swap the pseudo-variables with the data |
|
| 370 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 371 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 372 | + return str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 373 | + } |
|
| 374 | 374 | |
| 375 | - /** |
|
| 376 | - * Send the page content from cache to browser |
|
| 377 | - * @param object $cache the cache instance |
|
| 378 | - * @return boolean the status of the operation |
|
| 379 | - */ |
|
| 380 | - protected function sendCachePageContentToBrowser(&$cache){ |
|
| 381 | - $logger = self::getLogger(); |
|
| 382 | - $logger->info('The cache page content is expired or the browser does not send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $this->_currentUrl . '] send cache headers to tell the browser'); |
|
| 383 | - self::sendHeaders(200); |
|
| 384 | - //current page cache key |
|
| 385 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 386 | - //get the cache content |
|
| 387 | - $content = $cache->get($pageCacheKey); |
|
| 388 | - if($content){ |
|
| 389 | - $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
| 390 | - $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 391 | - ///display the final output |
|
| 392 | - //compress the output if is available |
|
| 393 | - $type = null; |
|
| 394 | - if (self::$_canCompressOutput){ |
|
| 395 | - $type = 'ob_gzhandler'; |
|
| 396 | - } |
|
| 397 | - ob_start($type); |
|
| 398 | - echo $content; |
|
| 399 | - ob_end_flush(); |
|
| 400 | - return true; |
|
| 401 | - } |
|
| 402 | - $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
| 403 | - $cache->delete($pageCacheKey); |
|
| 404 | - return false; |
|
| 405 | - } |
|
| 375 | + /** |
|
| 376 | + * Send the page content from cache to browser |
|
| 377 | + * @param object $cache the cache instance |
|
| 378 | + * @return boolean the status of the operation |
|
| 379 | + */ |
|
| 380 | + protected function sendCachePageContentToBrowser(&$cache){ |
|
| 381 | + $logger = self::getLogger(); |
|
| 382 | + $logger->info('The cache page content is expired or the browser does not send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $this->_currentUrl . '] send cache headers to tell the browser'); |
|
| 383 | + self::sendHeaders(200); |
|
| 384 | + //current page cache key |
|
| 385 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 386 | + //get the cache content |
|
| 387 | + $content = $cache->get($pageCacheKey); |
|
| 388 | + if($content){ |
|
| 389 | + $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
| 390 | + $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 391 | + ///display the final output |
|
| 392 | + //compress the output if is available |
|
| 393 | + $type = null; |
|
| 394 | + if (self::$_canCompressOutput){ |
|
| 395 | + $type = 'ob_gzhandler'; |
|
| 396 | + } |
|
| 397 | + ob_start($type); |
|
| 398 | + echo $content; |
|
| 399 | + ob_end_flush(); |
|
| 400 | + return true; |
|
| 401 | + } |
|
| 402 | + $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
| 403 | + $cache->delete($pageCacheKey); |
|
| 404 | + return false; |
|
| 405 | + } |
|
| 406 | 406 | |
| 407 | - /** |
|
| 408 | - * Save the content of page into cache |
|
| 409 | - * @param string $content the page content to be saved |
|
| 410 | - * @return void |
|
| 411 | - */ |
|
| 412 | - protected function savePageContentIntoCache($content){ |
|
| 413 | - $obj = & get_instance(); |
|
| 414 | - $logger = self::getLogger(); |
|
| 407 | + /** |
|
| 408 | + * Save the content of page into cache |
|
| 409 | + * @param string $content the page content to be saved |
|
| 410 | + * @return void |
|
| 411 | + */ |
|
| 412 | + protected function savePageContentIntoCache($content){ |
|
| 413 | + $obj = & get_instance(); |
|
| 414 | + $logger = self::getLogger(); |
|
| 415 | 415 | |
| 416 | - //current page URL |
|
| 417 | - $url = $this->_currentUrl; |
|
| 418 | - //Cache view Time to live in second |
|
| 419 | - $viewCacheTtl = get_config('cache_ttl'); |
|
| 420 | - if (!empty($obj->view_cache_ttl)){ |
|
| 421 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
| 422 | - } |
|
| 423 | - //the cache handler instance |
|
| 424 | - $cacheInstance = $obj->cache; |
|
| 425 | - //the current page cache key for identification |
|
| 426 | - $cacheKey = $this->_currentUrlCacheKey; |
|
| 427 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 428 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 416 | + //current page URL |
|
| 417 | + $url = $this->_currentUrl; |
|
| 418 | + //Cache view Time to live in second |
|
| 419 | + $viewCacheTtl = get_config('cache_ttl'); |
|
| 420 | + if (!empty($obj->view_cache_ttl)){ |
|
| 421 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
| 422 | + } |
|
| 423 | + //the cache handler instance |
|
| 424 | + $cacheInstance = $obj->cache; |
|
| 425 | + //the current page cache key for identification |
|
| 426 | + $cacheKey = $this->_currentUrlCacheKey; |
|
| 427 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 428 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 429 | 429 | |
| 430 | - //get the cache information to prepare header to send to browser |
|
| 431 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 432 | - if($cacheInfo){ |
|
| 433 | - $lastModified = $cacheInfo['mtime']; |
|
| 434 | - $expire = $cacheInfo['expire']; |
|
| 435 | - $maxAge = $expire - time(); |
|
| 436 | - self::setHeader('Pragma', 'public'); |
|
| 437 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 438 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 439 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 440 | - } |
|
| 441 | - } |
|
| 430 | + //get the cache information to prepare header to send to browser |
|
| 431 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 432 | + if($cacheInfo){ |
|
| 433 | + $lastModified = $cacheInfo['mtime']; |
|
| 434 | + $expire = $cacheInfo['expire']; |
|
| 435 | + $maxAge = $expire - time(); |
|
| 436 | + self::setHeader('Pragma', 'public'); |
|
| 437 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 438 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 439 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | 442 | |
| 443 | 443 | |
| 444 | - /** |
|
| 445 | - * Get the module information for the view to load |
|
| 446 | - * @param string $view the view name like moduleName/viewName, viewName |
|
| 447 | - * |
|
| 448 | - * @return array the module information |
|
| 449 | - * array( |
|
| 450 | - * 'module'=> 'module_name' |
|
| 451 | - * 'view' => 'view_name' |
|
| 452 | - * 'viewFile' => 'view_file' |
|
| 453 | - * ) |
|
| 454 | - */ |
|
| 455 | - protected function getModuleInfoForView($view){ |
|
| 456 | - $module = null; |
|
| 457 | - $viewFile = null; |
|
| 458 | - $obj = & get_instance(); |
|
| 459 | - //check if the request class contains module name |
|
| 460 | - if(strpos($view, '/') !== false){ |
|
| 461 | - $viewPath = explode('/', $view); |
|
| 462 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 463 | - $module = $viewPath[0]; |
|
| 464 | - array_shift($viewPath); |
|
| 465 | - $view = implode('/', $viewPath); |
|
| 466 | - $viewFile = $view . '.php'; |
|
| 467 | - } |
|
| 468 | - } |
|
| 469 | - if(! $module && !empty($obj->moduleName)){ |
|
| 470 | - $module = $obj->moduleName; |
|
| 471 | - } |
|
| 472 | - return array( |
|
| 473 | - 'view' => $view, |
|
| 474 | - 'module' => $module, |
|
| 475 | - 'viewFile' => $viewFile |
|
| 476 | - ); |
|
| 477 | - } |
|
| 444 | + /** |
|
| 445 | + * Get the module information for the view to load |
|
| 446 | + * @param string $view the view name like moduleName/viewName, viewName |
|
| 447 | + * |
|
| 448 | + * @return array the module information |
|
| 449 | + * array( |
|
| 450 | + * 'module'=> 'module_name' |
|
| 451 | + * 'view' => 'view_name' |
|
| 452 | + * 'viewFile' => 'view_file' |
|
| 453 | + * ) |
|
| 454 | + */ |
|
| 455 | + protected function getModuleInfoForView($view){ |
|
| 456 | + $module = null; |
|
| 457 | + $viewFile = null; |
|
| 458 | + $obj = & get_instance(); |
|
| 459 | + //check if the request class contains module name |
|
| 460 | + if(strpos($view, '/') !== false){ |
|
| 461 | + $viewPath = explode('/', $view); |
|
| 462 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 463 | + $module = $viewPath[0]; |
|
| 464 | + array_shift($viewPath); |
|
| 465 | + $view = implode('/', $viewPath); |
|
| 466 | + $viewFile = $view . '.php'; |
|
| 467 | + } |
|
| 468 | + } |
|
| 469 | + if(! $module && !empty($obj->moduleName)){ |
|
| 470 | + $module = $obj->moduleName; |
|
| 471 | + } |
|
| 472 | + return array( |
|
| 473 | + 'view' => $view, |
|
| 474 | + 'module' => $module, |
|
| 475 | + 'viewFile' => $viewFile |
|
| 476 | + ); |
|
| 477 | + } |
|
| 478 | 478 | |
| 479 | - /** |
|
| 480 | - * Render the view page |
|
| 481 | - * @see Response::render |
|
| 482 | - * @return void|string |
|
| 483 | - */ |
|
| 484 | - protected function loadView($path, array $data = array(), $return = false){ |
|
| 485 | - $found = false; |
|
| 486 | - if(file_exists($path)){ |
|
| 487 | - //super instance |
|
| 488 | - $obj = & get_instance(); |
|
| 489 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 490 | - if(! isset($this->{$key})){ |
|
| 491 | - $this->{$key} = & $obj->{$key}; |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - ob_start(); |
|
| 495 | - extract($data); |
|
| 496 | - //need use require() instead of require_once because can load this view many time |
|
| 497 | - require $path; |
|
| 498 | - $content = ob_get_clean(); |
|
| 499 | - if($return){ |
|
| 500 | - //remove unused html space |
|
| 501 | - return preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 502 | - } |
|
| 503 | - $this->_pageRender .= $content; |
|
| 504 | - $found = true; |
|
| 505 | - } |
|
| 506 | - if(! $found){ |
|
| 507 | - show_error('Unable to find view [' .$path . ']'); |
|
| 508 | - } |
|
| 509 | - } |
|
| 479 | + /** |
|
| 480 | + * Render the view page |
|
| 481 | + * @see Response::render |
|
| 482 | + * @return void|string |
|
| 483 | + */ |
|
| 484 | + protected function loadView($path, array $data = array(), $return = false){ |
|
| 485 | + $found = false; |
|
| 486 | + if(file_exists($path)){ |
|
| 487 | + //super instance |
|
| 488 | + $obj = & get_instance(); |
|
| 489 | + foreach(get_object_vars($obj) as $key => $value){ |
|
| 490 | + if(! isset($this->{$key})){ |
|
| 491 | + $this->{$key} = & $obj->{$key}; |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + ob_start(); |
|
| 495 | + extract($data); |
|
| 496 | + //need use require() instead of require_once because can load this view many time |
|
| 497 | + require $path; |
|
| 498 | + $content = ob_get_clean(); |
|
| 499 | + if($return){ |
|
| 500 | + //remove unused html space |
|
| 501 | + return preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 502 | + } |
|
| 503 | + $this->_pageRender .= $content; |
|
| 504 | + $found = true; |
|
| 505 | + } |
|
| 506 | + if(! $found){ |
|
| 507 | + show_error('Unable to find view [' .$path . ']'); |
|
| 508 | + } |
|
| 509 | + } |
|
| 510 | 510 | |
| 511 | - } |
|
| 511 | + } |
|
@@ -1,58 +1,58 @@ |
||
| 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 BaseStaticClass{ |
|
| 28 | - /** |
|
| 29 | - * The logger instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 32 | - protected static $logger; |
|
| 27 | + class BaseStaticClass{ |
|
| 28 | + /** |
|
| 29 | + * The logger instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | + protected static $logger; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The signleton of the logger |
|
| 36 | - * @return Object the Log instance |
|
| 37 | - */ |
|
| 38 | - public static function getLogger(){ |
|
| 39 | - if(self::$logger == null){ |
|
| 40 | - $logger = array(); |
|
| 41 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | - $logger[0]->setLogger('Class::' . get_called_class()); |
|
| 43 | - self::$logger = $logger[0]; |
|
| 44 | - } |
|
| 45 | - return self::$logger; |
|
| 46 | - } |
|
| 34 | + /** |
|
| 35 | + * The signleton of the logger |
|
| 36 | + * @return Object the Log instance |
|
| 37 | + */ |
|
| 38 | + public static function getLogger(){ |
|
| 39 | + if(self::$logger == null){ |
|
| 40 | + $logger = array(); |
|
| 41 | + $logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | + $logger[0]->setLogger('Class::' . get_called_class()); |
|
| 43 | + self::$logger = $logger[0]; |
|
| 44 | + } |
|
| 45 | + return self::$logger; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Set the log instance for future use |
|
| 50 | - * @param object $logger the log object |
|
| 51 | - * @return object the log instance |
|
| 52 | - */ |
|
| 53 | - public static function setLogger($logger){ |
|
| 54 | - self::$logger = $logger; |
|
| 55 | - return self::$logger; |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Set the log instance for future use |
|
| 50 | + * @param object $logger the log object |
|
| 51 | + * @return object the log instance |
|
| 52 | + */ |
|
| 53 | + public static function setLogger($logger){ |
|
| 54 | + self::$logger = $logger; |
|
| 55 | + return self::$logger; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
@@ -1,627 +1,627 @@ |
||
| 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 | - class Loader extends BaseStaticClass{ |
|
| 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 | + class Loader extends BaseStaticClass{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * List of loaded resources |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - public static $loaded = array(); |
|
| 28 | + /** |
|
| 29 | + * List of loaded resources |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + public static $loaded = array(); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - public function __construct(){ |
|
| 36 | - //add the resources already loaded during application bootstrap |
|
| 37 | - //in the list to prevent duplicate or loading the resources again. |
|
| 38 | - static::$loaded = class_loaded(); |
|
| 35 | + public function __construct(){ |
|
| 36 | + //add the resources already loaded during application bootstrap |
|
| 37 | + //in the list to prevent duplicate or loading the resources again. |
|
| 38 | + static::$loaded = class_loaded(); |
|
| 39 | 39 | |
| 40 | - //Load resources from autoload configuration |
|
| 41 | - $this->loadResourcesFromAutoloadConfig(); |
|
| 42 | - } |
|
| 40 | + //Load resources from autoload configuration |
|
| 41 | + $this->loadResourcesFromAutoloadConfig(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Load the model class |
|
| 47 | - * |
|
| 48 | - * @param string $class the class name to be loaded |
|
| 49 | - * @param string $instance the name of the instance to use in super object |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public static function model($class, $instance = null){ |
|
| 54 | - $logger = static::getLogger(); |
|
| 55 | - $class = str_ireplace('.php', '', $class); |
|
| 56 | - $class = trim($class, '/\\'); |
|
| 57 | - $file = ucfirst($class).'.php'; |
|
| 58 | - $logger->debug('Loading model [' . $class . '] ...'); |
|
| 59 | - //************ |
|
| 60 | - if (! $instance){ |
|
| 61 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 62 | - } |
|
| 63 | - //**************** |
|
| 64 | - if (isset(static::$loaded[$instance])){ |
|
| 65 | - $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - $classFilePath = APPS_MODEL_PATH . $file; |
|
| 69 | - //first check if this model is in the module |
|
| 70 | - $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 71 | - //check if the request class contains module name |
|
| 72 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 73 | - $module = $moduleInfo['module']; |
|
| 74 | - $class = $moduleInfo['class']; |
|
| 45 | + /** |
|
| 46 | + * Load the model class |
|
| 47 | + * |
|
| 48 | + * @param string $class the class name to be loaded |
|
| 49 | + * @param string $instance the name of the instance to use in super object |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public static function model($class, $instance = null){ |
|
| 54 | + $logger = static::getLogger(); |
|
| 55 | + $class = str_ireplace('.php', '', $class); |
|
| 56 | + $class = trim($class, '/\\'); |
|
| 57 | + $file = ucfirst($class).'.php'; |
|
| 58 | + $logger->debug('Loading model [' . $class . '] ...'); |
|
| 59 | + //************ |
|
| 60 | + if (! $instance){ |
|
| 61 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 62 | + } |
|
| 63 | + //**************** |
|
| 64 | + if (isset(static::$loaded[$instance])){ |
|
| 65 | + $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + $classFilePath = APPS_MODEL_PATH . $file; |
|
| 69 | + //first check if this model is in the module |
|
| 70 | + $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 71 | + //check if the request class contains module name |
|
| 72 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 73 | + $module = $moduleInfo['module']; |
|
| 74 | + $class = $moduleInfo['class']; |
|
| 75 | 75 | |
| 76 | - $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 77 | - if ($moduleModelFilePath){ |
|
| 78 | - $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 79 | - $classFilePath = $moduleModelFilePath; |
|
| 80 | - } |
|
| 81 | - else{ |
|
| 82 | - $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 83 | - } |
|
| 84 | - $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 85 | - if (file_exists($classFilePath)){ |
|
| 86 | - require_once $classFilePath; |
|
| 87 | - if (class_exists($class)){ |
|
| 88 | - $c = new $class(); |
|
| 89 | - $obj = & get_instance(); |
|
| 90 | - $obj->{$instance} = $c; |
|
| 91 | - static::$loaded[$instance] = $class; |
|
| 92 | - $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 93 | - } |
|
| 94 | - else{ |
|
| 95 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - else{ |
|
| 99 | - show_error('Unable to find the model [' . $class . ']'); |
|
| 100 | - } |
|
| 101 | - } |
|
| 76 | + $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 77 | + if ($moduleModelFilePath){ |
|
| 78 | + $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 79 | + $classFilePath = $moduleModelFilePath; |
|
| 80 | + } |
|
| 81 | + else{ |
|
| 82 | + $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 83 | + } |
|
| 84 | + $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 85 | + if (file_exists($classFilePath)){ |
|
| 86 | + require_once $classFilePath; |
|
| 87 | + if (class_exists($class)){ |
|
| 88 | + $c = new $class(); |
|
| 89 | + $obj = & get_instance(); |
|
| 90 | + $obj->{$instance} = $c; |
|
| 91 | + static::$loaded[$instance] = $class; |
|
| 92 | + $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 93 | + } |
|
| 94 | + else{ |
|
| 95 | + show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + else{ |
|
| 99 | + show_error('Unable to find the model [' . $class . ']'); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Load the library class |
|
| 106 | - * |
|
| 107 | - * @param string $class the library class name to be loaded |
|
| 108 | - * @param string $instance the instance name to use in super object |
|
| 109 | - * @param mixed $params the arguments to pass to the constructor |
|
| 110 | - * |
|
| 111 | - * @return void |
|
| 112 | - */ |
|
| 113 | - public static function library($class, $instance = null, array $params = array()){ |
|
| 114 | - $logger = static::getLogger(); |
|
| 115 | - $class = str_ireplace('.php', '', $class); |
|
| 116 | - $class = trim($class, '/\\'); |
|
| 117 | - $file = ucfirst($class) .'.php'; |
|
| 118 | - $logger->debug('Loading library [' . $class . '] ...'); |
|
| 119 | - if (! $instance){ |
|
| 120 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 121 | - } |
|
| 122 | - if (isset(static::$loaded[$instance])){ |
|
| 123 | - $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 124 | - return; |
|
| 125 | - } |
|
| 126 | - $obj = & get_instance(); |
|
| 127 | - //Check and load Database library |
|
| 128 | - if (strtolower($class) == 'database'){ |
|
| 129 | - $logger->info('This is the Database library ...'); |
|
| 130 | - $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 131 | - static::$loaded[$instance] = $class; |
|
| 132 | - $logger->info('Library Database loaded successfully.'); |
|
| 133 | - return; |
|
| 134 | - } |
|
| 135 | - $libraryFilePath = null; |
|
| 136 | - $logger->debug('Check if this is a system library ...'); |
|
| 137 | - if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 138 | - $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 139 | - $class = ucfirst($class); |
|
| 140 | - $logger->info('This library is a system library'); |
|
| 141 | - } |
|
| 142 | - else{ |
|
| 143 | - $logger->info('This library is not a system library'); |
|
| 144 | - //first check if this library is in the module |
|
| 145 | - $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 146 | - //*************** |
|
| 147 | - } |
|
| 148 | - if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 149 | - $libraryFilePath = LIBRARY_PATH . $file; |
|
| 150 | - } |
|
| 151 | - $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 152 | - //************************* |
|
| 153 | - self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 154 | - } |
|
| 104 | + /** |
|
| 105 | + * Load the library class |
|
| 106 | + * |
|
| 107 | + * @param string $class the library class name to be loaded |
|
| 108 | + * @param string $instance the instance name to use in super object |
|
| 109 | + * @param mixed $params the arguments to pass to the constructor |
|
| 110 | + * |
|
| 111 | + * @return void |
|
| 112 | + */ |
|
| 113 | + public static function library($class, $instance = null, array $params = array()){ |
|
| 114 | + $logger = static::getLogger(); |
|
| 115 | + $class = str_ireplace('.php', '', $class); |
|
| 116 | + $class = trim($class, '/\\'); |
|
| 117 | + $file = ucfirst($class) .'.php'; |
|
| 118 | + $logger->debug('Loading library [' . $class . '] ...'); |
|
| 119 | + if (! $instance){ |
|
| 120 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 121 | + } |
|
| 122 | + if (isset(static::$loaded[$instance])){ |
|
| 123 | + $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 124 | + return; |
|
| 125 | + } |
|
| 126 | + $obj = & get_instance(); |
|
| 127 | + //Check and load Database library |
|
| 128 | + if (strtolower($class) == 'database'){ |
|
| 129 | + $logger->info('This is the Database library ...'); |
|
| 130 | + $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 131 | + static::$loaded[$instance] = $class; |
|
| 132 | + $logger->info('Library Database loaded successfully.'); |
|
| 133 | + return; |
|
| 134 | + } |
|
| 135 | + $libraryFilePath = null; |
|
| 136 | + $logger->debug('Check if this is a system library ...'); |
|
| 137 | + if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 138 | + $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 139 | + $class = ucfirst($class); |
|
| 140 | + $logger->info('This library is a system library'); |
|
| 141 | + } |
|
| 142 | + else{ |
|
| 143 | + $logger->info('This library is not a system library'); |
|
| 144 | + //first check if this library is in the module |
|
| 145 | + $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 146 | + //*************** |
|
| 147 | + } |
|
| 148 | + if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 149 | + $libraryFilePath = LIBRARY_PATH . $file; |
|
| 150 | + } |
|
| 151 | + $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 152 | + //************************* |
|
| 153 | + self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Load the helper |
|
| 158 | - * |
|
| 159 | - * @param string $function the helper name to be loaded |
|
| 160 | - * |
|
| 161 | - * @return void |
|
| 162 | - */ |
|
| 163 | - public static function functions($function){ |
|
| 164 | - $logger = static::getLogger(); |
|
| 165 | - $function = str_ireplace('.php', '', $function); |
|
| 166 | - $function = trim($function, '/\\'); |
|
| 167 | - $function = str_ireplace('function_', '', $function); |
|
| 168 | - $file = 'function_'.$function.'.php'; |
|
| 169 | - $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 170 | - if (isset(static::$loaded['function_' . $function])){ |
|
| 171 | - $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 172 | - return; |
|
| 173 | - } |
|
| 174 | - $functionFilePath = null; |
|
| 175 | - //first check if this helper is in the module |
|
| 176 | - $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 177 | - $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 178 | - $module = $moduleInfo['module']; |
|
| 179 | - $function = $moduleInfo['function']; |
|
| 180 | - if(! empty($moduleInfo['file'])){ |
|
| 181 | - $file = $moduleInfo['file']; |
|
| 182 | - } |
|
| 183 | - $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 184 | - if ($moduleFunctionPath){ |
|
| 185 | - $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 186 | - $functionFilePath = $moduleFunctionPath; |
|
| 187 | - } |
|
| 188 | - else{ |
|
| 189 | - $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 190 | - } |
|
| 191 | - if (! $functionFilePath){ |
|
| 192 | - $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 193 | - foreach($searchDir as $dir){ |
|
| 194 | - $filePath = $dir . $file; |
|
| 195 | - if (file_exists($filePath)){ |
|
| 196 | - $functionFilePath = $filePath; |
|
| 197 | - //is already found not to continue |
|
| 198 | - break; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 203 | - if ($functionFilePath){ |
|
| 204 | - require_once $functionFilePath; |
|
| 205 | - static::$loaded['function_' . $function] = $functionFilePath; |
|
| 206 | - $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 207 | - } |
|
| 208 | - else{ |
|
| 209 | - show_error('Unable to find helper file [' . $file . ']'); |
|
| 210 | - } |
|
| 211 | - } |
|
| 156 | + /** |
|
| 157 | + * Load the helper |
|
| 158 | + * |
|
| 159 | + * @param string $function the helper name to be loaded |
|
| 160 | + * |
|
| 161 | + * @return void |
|
| 162 | + */ |
|
| 163 | + public static function functions($function){ |
|
| 164 | + $logger = static::getLogger(); |
|
| 165 | + $function = str_ireplace('.php', '', $function); |
|
| 166 | + $function = trim($function, '/\\'); |
|
| 167 | + $function = str_ireplace('function_', '', $function); |
|
| 168 | + $file = 'function_'.$function.'.php'; |
|
| 169 | + $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 170 | + if (isset(static::$loaded['function_' . $function])){ |
|
| 171 | + $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 172 | + return; |
|
| 173 | + } |
|
| 174 | + $functionFilePath = null; |
|
| 175 | + //first check if this helper is in the module |
|
| 176 | + $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 177 | + $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 178 | + $module = $moduleInfo['module']; |
|
| 179 | + $function = $moduleInfo['function']; |
|
| 180 | + if(! empty($moduleInfo['file'])){ |
|
| 181 | + $file = $moduleInfo['file']; |
|
| 182 | + } |
|
| 183 | + $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 184 | + if ($moduleFunctionPath){ |
|
| 185 | + $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 186 | + $functionFilePath = $moduleFunctionPath; |
|
| 187 | + } |
|
| 188 | + else{ |
|
| 189 | + $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 190 | + } |
|
| 191 | + if (! $functionFilePath){ |
|
| 192 | + $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 193 | + foreach($searchDir as $dir){ |
|
| 194 | + $filePath = $dir . $file; |
|
| 195 | + if (file_exists($filePath)){ |
|
| 196 | + $functionFilePath = $filePath; |
|
| 197 | + //is already found not to continue |
|
| 198 | + break; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 203 | + if ($functionFilePath){ |
|
| 204 | + require_once $functionFilePath; |
|
| 205 | + static::$loaded['function_' . $function] = $functionFilePath; |
|
| 206 | + $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 207 | + } |
|
| 208 | + else{ |
|
| 209 | + show_error('Unable to find helper file [' . $file . ']'); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * Load the configuration file |
|
| 215 | - * |
|
| 216 | - * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 217 | - * |
|
| 218 | - * @return void |
|
| 219 | - */ |
|
| 220 | - public static function config($filename){ |
|
| 221 | - $logger = static::getLogger(); |
|
| 222 | - $filename = str_ireplace('.php', '', $filename); |
|
| 223 | - $filename = trim($filename, '/\\'); |
|
| 224 | - $filename = str_ireplace('config_', '', $filename); |
|
| 225 | - $file = 'config_'.$filename.'.php'; |
|
| 226 | - $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 227 | - if (isset(static::$loaded['config_' . $filename])){ |
|
| 228 | - $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 229 | - return; |
|
| 230 | - } |
|
| 231 | - $configFilePath = CONFIG_PATH . $file; |
|
| 232 | - //first check if this config is in the module |
|
| 233 | - $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 234 | - $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 235 | - $module = $moduleInfo['module']; |
|
| 236 | - $filename = $moduleInfo['filename']; |
|
| 237 | - $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 238 | - if ($moduleConfigPath){ |
|
| 239 | - $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 240 | - $configFilePath = $moduleConfigPath; |
|
| 241 | - } |
|
| 242 | - else{ |
|
| 243 | - $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 244 | - } |
|
| 245 | - $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 246 | - $config = array(); |
|
| 247 | - if (file_exists($configFilePath)){ |
|
| 248 | - require_once $configFilePath; |
|
| 249 | - if (! empty($config) && is_array($config)){ |
|
| 250 | - Config::setAll($config); |
|
| 251 | - static::$loaded['config_' . $filename] = $configFilePath; |
|
| 252 | - $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 253 | - $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 254 | - unset($config); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - else{ |
|
| 258 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 259 | - } |
|
| 260 | - } |
|
| 213 | + /** |
|
| 214 | + * Load the configuration file |
|
| 215 | + * |
|
| 216 | + * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 217 | + * |
|
| 218 | + * @return void |
|
| 219 | + */ |
|
| 220 | + public static function config($filename){ |
|
| 221 | + $logger = static::getLogger(); |
|
| 222 | + $filename = str_ireplace('.php', '', $filename); |
|
| 223 | + $filename = trim($filename, '/\\'); |
|
| 224 | + $filename = str_ireplace('config_', '', $filename); |
|
| 225 | + $file = 'config_'.$filename.'.php'; |
|
| 226 | + $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 227 | + if (isset(static::$loaded['config_' . $filename])){ |
|
| 228 | + $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 229 | + return; |
|
| 230 | + } |
|
| 231 | + $configFilePath = CONFIG_PATH . $file; |
|
| 232 | + //first check if this config is in the module |
|
| 233 | + $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 234 | + $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 235 | + $module = $moduleInfo['module']; |
|
| 236 | + $filename = $moduleInfo['filename']; |
|
| 237 | + $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 238 | + if ($moduleConfigPath){ |
|
| 239 | + $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 240 | + $configFilePath = $moduleConfigPath; |
|
| 241 | + } |
|
| 242 | + else{ |
|
| 243 | + $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 244 | + } |
|
| 245 | + $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 246 | + $config = array(); |
|
| 247 | + if (file_exists($configFilePath)){ |
|
| 248 | + require_once $configFilePath; |
|
| 249 | + if (! empty($config) && is_array($config)){ |
|
| 250 | + Config::setAll($config); |
|
| 251 | + static::$loaded['config_' . $filename] = $configFilePath; |
|
| 252 | + $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 253 | + $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 254 | + unset($config); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + else{ |
|
| 258 | + show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Load the language |
|
| 265 | - * |
|
| 266 | - * @param string $language the language name to be loaded |
|
| 267 | - * |
|
| 268 | - * @return void |
|
| 269 | - */ |
|
| 270 | - public static function lang($language){ |
|
| 271 | - $logger = static::getLogger(); |
|
| 272 | - $language = str_ireplace('.php', '', $language); |
|
| 273 | - $language = trim($language, '/\\'); |
|
| 274 | - $language = str_ireplace('lang_', '', $language); |
|
| 275 | - $file = 'lang_'.$language.'.php'; |
|
| 276 | - $logger->debug('Loading language [' . $language . '] ...'); |
|
| 277 | - if (isset(static::$loaded['lang_' . $language])){ |
|
| 278 | - $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 279 | - return; |
|
| 280 | - } |
|
| 281 | - //get the current language |
|
| 282 | - $appLang = self::getAppLang(); |
|
| 283 | - $languageFilePath = null; |
|
| 284 | - //first check if this language is in the module |
|
| 285 | - $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 286 | - $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 287 | - $module = $moduleInfo['module']; |
|
| 288 | - $language = $moduleInfo['language']; |
|
| 289 | - if(! empty($moduleInfo['file'])){ |
|
| 290 | - $file = $moduleInfo['file']; |
|
| 291 | - } |
|
| 292 | - $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 293 | - if ($moduleLanguagePath){ |
|
| 294 | - $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 295 | - $languageFilePath = $moduleLanguagePath; |
|
| 296 | - } |
|
| 297 | - else{ |
|
| 298 | - $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 299 | - } |
|
| 300 | - if (! $languageFilePath){ |
|
| 301 | - $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 302 | - foreach($searchDir as $dir){ |
|
| 303 | - $filePath = $dir . $appLang . DS . $file; |
|
| 304 | - if (file_exists($filePath)){ |
|
| 305 | - $languageFilePath = $filePath; |
|
| 306 | - //already found no need continue |
|
| 307 | - break; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 312 | - self::loadLanguage($languageFilePath, $language); |
|
| 313 | - } |
|
| 263 | + /** |
|
| 264 | + * Load the language |
|
| 265 | + * |
|
| 266 | + * @param string $language the language name to be loaded |
|
| 267 | + * |
|
| 268 | + * @return void |
|
| 269 | + */ |
|
| 270 | + public static function lang($language){ |
|
| 271 | + $logger = static::getLogger(); |
|
| 272 | + $language = str_ireplace('.php', '', $language); |
|
| 273 | + $language = trim($language, '/\\'); |
|
| 274 | + $language = str_ireplace('lang_', '', $language); |
|
| 275 | + $file = 'lang_'.$language.'.php'; |
|
| 276 | + $logger->debug('Loading language [' . $language . '] ...'); |
|
| 277 | + if (isset(static::$loaded['lang_' . $language])){ |
|
| 278 | + $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 279 | + return; |
|
| 280 | + } |
|
| 281 | + //get the current language |
|
| 282 | + $appLang = self::getAppLang(); |
|
| 283 | + $languageFilePath = null; |
|
| 284 | + //first check if this language is in the module |
|
| 285 | + $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 286 | + $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 287 | + $module = $moduleInfo['module']; |
|
| 288 | + $language = $moduleInfo['language']; |
|
| 289 | + if(! empty($moduleInfo['file'])){ |
|
| 290 | + $file = $moduleInfo['file']; |
|
| 291 | + } |
|
| 292 | + $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 293 | + if ($moduleLanguagePath){ |
|
| 294 | + $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 295 | + $languageFilePath = $moduleLanguagePath; |
|
| 296 | + } |
|
| 297 | + else{ |
|
| 298 | + $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 299 | + } |
|
| 300 | + if (! $languageFilePath){ |
|
| 301 | + $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 302 | + foreach($searchDir as $dir){ |
|
| 303 | + $filePath = $dir . $appLang . DS . $file; |
|
| 304 | + if (file_exists($filePath)){ |
|
| 305 | + $languageFilePath = $filePath; |
|
| 306 | + //already found no need continue |
|
| 307 | + break; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 312 | + self::loadLanguage($languageFilePath, $language); |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - /** |
|
| 316 | - * Return the current app language by default will use the value from cookie |
|
| 317 | - * if can not found will use the default value from configuration |
|
| 318 | - * @return string the app language like "en", "fr" |
|
| 319 | - */ |
|
| 320 | - protected static function getAppLang(){ |
|
| 321 | - //determine the current language |
|
| 322 | - $appLang = get_config('default_language'); |
|
| 323 | - //if the language exists in the cookie use it |
|
| 324 | - $cfgKey = get_config('language_cookie_name'); |
|
| 325 | - $objCookie = & class_loader('Cookie'); |
|
| 326 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 327 | - if ($cookieLang){ |
|
| 328 | - $appLang = $cookieLang; |
|
| 329 | - } |
|
| 330 | - return $appLang; |
|
| 331 | - } |
|
| 332 | - /** |
|
| 333 | - * Get the module information for the model and library to load |
|
| 334 | - * @param string $class the full class name like moduleName/className, className, |
|
| 335 | - * @return array the module information |
|
| 336 | - * array( |
|
| 337 | - * 'module'=> 'module_name' |
|
| 338 | - * 'class' => 'class_name' |
|
| 339 | - * ) |
|
| 340 | - */ |
|
| 341 | - protected static function getModuleInfoForModelLibrary($class){ |
|
| 342 | - $module = null; |
|
| 343 | - $obj = & get_instance(); |
|
| 344 | - if (strpos($class, '/') !== false){ |
|
| 345 | - $path = explode('/', $class); |
|
| 346 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 347 | - $module = $path[0]; |
|
| 348 | - $class = ucfirst($path[1]); |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - else{ |
|
| 352 | - $class = ucfirst($class); |
|
| 353 | - } |
|
| 354 | - if (! $module && !empty($obj->moduleName)){ |
|
| 355 | - $module = $obj->moduleName; |
|
| 356 | - } |
|
| 357 | - return array( |
|
| 358 | - 'class' => $class, |
|
| 359 | - 'module' => $module |
|
| 360 | - ); |
|
| 361 | - } |
|
| 315 | + /** |
|
| 316 | + * Return the current app language by default will use the value from cookie |
|
| 317 | + * if can not found will use the default value from configuration |
|
| 318 | + * @return string the app language like "en", "fr" |
|
| 319 | + */ |
|
| 320 | + protected static function getAppLang(){ |
|
| 321 | + //determine the current language |
|
| 322 | + $appLang = get_config('default_language'); |
|
| 323 | + //if the language exists in the cookie use it |
|
| 324 | + $cfgKey = get_config('language_cookie_name'); |
|
| 325 | + $objCookie = & class_loader('Cookie'); |
|
| 326 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 327 | + if ($cookieLang){ |
|
| 328 | + $appLang = $cookieLang; |
|
| 329 | + } |
|
| 330 | + return $appLang; |
|
| 331 | + } |
|
| 332 | + /** |
|
| 333 | + * Get the module information for the model and library to load |
|
| 334 | + * @param string $class the full class name like moduleName/className, className, |
|
| 335 | + * @return array the module information |
|
| 336 | + * array( |
|
| 337 | + * 'module'=> 'module_name' |
|
| 338 | + * 'class' => 'class_name' |
|
| 339 | + * ) |
|
| 340 | + */ |
|
| 341 | + protected static function getModuleInfoForModelLibrary($class){ |
|
| 342 | + $module = null; |
|
| 343 | + $obj = & get_instance(); |
|
| 344 | + if (strpos($class, '/') !== false){ |
|
| 345 | + $path = explode('/', $class); |
|
| 346 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 347 | + $module = $path[0]; |
|
| 348 | + $class = ucfirst($path[1]); |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + else{ |
|
| 352 | + $class = ucfirst($class); |
|
| 353 | + } |
|
| 354 | + if (! $module && !empty($obj->moduleName)){ |
|
| 355 | + $module = $obj->moduleName; |
|
| 356 | + } |
|
| 357 | + return array( |
|
| 358 | + 'class' => $class, |
|
| 359 | + 'module' => $module |
|
| 360 | + ); |
|
| 361 | + } |
|
| 362 | 362 | |
| 363 | - /** |
|
| 364 | - * Get the module information for the function to load |
|
| 365 | - * @param string $function the function name like moduleName/functionName, functionName, |
|
| 366 | - * @return array the module information |
|
| 367 | - * array( |
|
| 368 | - * 'module'=> 'module_name' |
|
| 369 | - * 'function' => 'function' |
|
| 370 | - * 'file' => 'file' |
|
| 371 | - * ) |
|
| 372 | - */ |
|
| 373 | - protected static function getModuleInfoForFunction($function){ |
|
| 374 | - $module = null; |
|
| 375 | - $file = null; |
|
| 376 | - $obj = & get_instance(); |
|
| 377 | - //check if the request class contains module name |
|
| 378 | - if (strpos($function, '/') !== false){ |
|
| 379 | - $path = explode('/', $function); |
|
| 380 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 381 | - $module = $path[0]; |
|
| 382 | - $function = 'function_' . $path[1]; |
|
| 383 | - $file = $path[0] . DS . $function.'.php'; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - if (! $module && !empty($obj->moduleName)){ |
|
| 387 | - $module = $obj->moduleName; |
|
| 388 | - } |
|
| 389 | - return array( |
|
| 390 | - 'function' => $function, |
|
| 391 | - 'module' => $module, |
|
| 392 | - 'file' => $file |
|
| 393 | - ); |
|
| 394 | - } |
|
| 363 | + /** |
|
| 364 | + * Get the module information for the function to load |
|
| 365 | + * @param string $function the function name like moduleName/functionName, functionName, |
|
| 366 | + * @return array the module information |
|
| 367 | + * array( |
|
| 368 | + * 'module'=> 'module_name' |
|
| 369 | + * 'function' => 'function' |
|
| 370 | + * 'file' => 'file' |
|
| 371 | + * ) |
|
| 372 | + */ |
|
| 373 | + protected static function getModuleInfoForFunction($function){ |
|
| 374 | + $module = null; |
|
| 375 | + $file = null; |
|
| 376 | + $obj = & get_instance(); |
|
| 377 | + //check if the request class contains module name |
|
| 378 | + if (strpos($function, '/') !== false){ |
|
| 379 | + $path = explode('/', $function); |
|
| 380 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 381 | + $module = $path[0]; |
|
| 382 | + $function = 'function_' . $path[1]; |
|
| 383 | + $file = $path[0] . DS . $function.'.php'; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + if (! $module && !empty($obj->moduleName)){ |
|
| 387 | + $module = $obj->moduleName; |
|
| 388 | + } |
|
| 389 | + return array( |
|
| 390 | + 'function' => $function, |
|
| 391 | + 'module' => $module, |
|
| 392 | + 'file' => $file |
|
| 393 | + ); |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | - /** |
|
| 397 | - * Get the module information for the language to load |
|
| 398 | - * @param string $language the language name like moduleName/languageName, languageName, |
|
| 399 | - * @return array the module information |
|
| 400 | - * array( |
|
| 401 | - * 'module'=> 'module_name' |
|
| 402 | - * 'language' => 'language' |
|
| 403 | - * 'file' => 'file' |
|
| 404 | - * ) |
|
| 405 | - */ |
|
| 406 | - protected static function getModuleInfoForLanguage($language){ |
|
| 407 | - $module = null; |
|
| 408 | - $file = null; |
|
| 409 | - $obj = & get_instance(); |
|
| 410 | - //check if the request class contains module name |
|
| 411 | - if (strpos($language, '/') !== false){ |
|
| 412 | - $path = explode('/', $language); |
|
| 413 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 414 | - $module = $path[0]; |
|
| 415 | - $language = 'lang_' . $path[1] . '.php'; |
|
| 416 | - $file = $path[0] . DS .$language; |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - if (! $module && !empty($obj->moduleName)){ |
|
| 420 | - $module = $obj->moduleName; |
|
| 421 | - } |
|
| 422 | - return array( |
|
| 423 | - 'language' => $language, |
|
| 424 | - 'module' => $module, |
|
| 425 | - 'file' => $file |
|
| 426 | - ); |
|
| 427 | - } |
|
| 396 | + /** |
|
| 397 | + * Get the module information for the language to load |
|
| 398 | + * @param string $language the language name like moduleName/languageName, languageName, |
|
| 399 | + * @return array the module information |
|
| 400 | + * array( |
|
| 401 | + * 'module'=> 'module_name' |
|
| 402 | + * 'language' => 'language' |
|
| 403 | + * 'file' => 'file' |
|
| 404 | + * ) |
|
| 405 | + */ |
|
| 406 | + protected static function getModuleInfoForLanguage($language){ |
|
| 407 | + $module = null; |
|
| 408 | + $file = null; |
|
| 409 | + $obj = & get_instance(); |
|
| 410 | + //check if the request class contains module name |
|
| 411 | + if (strpos($language, '/') !== false){ |
|
| 412 | + $path = explode('/', $language); |
|
| 413 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 414 | + $module = $path[0]; |
|
| 415 | + $language = 'lang_' . $path[1] . '.php'; |
|
| 416 | + $file = $path[0] . DS .$language; |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + if (! $module && !empty($obj->moduleName)){ |
|
| 420 | + $module = $obj->moduleName; |
|
| 421 | + } |
|
| 422 | + return array( |
|
| 423 | + 'language' => $language, |
|
| 424 | + 'module' => $module, |
|
| 425 | + 'file' => $file |
|
| 426 | + ); |
|
| 427 | + } |
|
| 428 | 428 | |
| 429 | 429 | |
| 430 | - /** |
|
| 431 | - * Get the module information for the config to load |
|
| 432 | - * @param string $filename the filename of the configuration file, |
|
| 433 | - * @return array the module information |
|
| 434 | - * array( |
|
| 435 | - * 'module'=> 'module_name' |
|
| 436 | - * 'filename' => 'filename' |
|
| 437 | - * ) |
|
| 438 | - */ |
|
| 439 | - protected static function getModuleInfoForConfig($filename){ |
|
| 440 | - $module = null; |
|
| 441 | - $obj = & get_instance(); |
|
| 442 | - //check if the request class contains module name |
|
| 443 | - if (strpos($filename, '/') !== false){ |
|
| 444 | - $path = explode('/', $filename); |
|
| 445 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 446 | - $module = $path[0]; |
|
| 447 | - $filename = $path[1] . '.php'; |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - if (! $module && !empty($obj->moduleName)){ |
|
| 451 | - $module = $obj->moduleName; |
|
| 452 | - } |
|
| 453 | - return array( |
|
| 454 | - 'filename' => $filename, |
|
| 455 | - 'module' => $module |
|
| 456 | - ); |
|
| 457 | - } |
|
| 430 | + /** |
|
| 431 | + * Get the module information for the config to load |
|
| 432 | + * @param string $filename the filename of the configuration file, |
|
| 433 | + * @return array the module information |
|
| 434 | + * array( |
|
| 435 | + * 'module'=> 'module_name' |
|
| 436 | + * 'filename' => 'filename' |
|
| 437 | + * ) |
|
| 438 | + */ |
|
| 439 | + protected static function getModuleInfoForConfig($filename){ |
|
| 440 | + $module = null; |
|
| 441 | + $obj = & get_instance(); |
|
| 442 | + //check if the request class contains module name |
|
| 443 | + if (strpos($filename, '/') !== false){ |
|
| 444 | + $path = explode('/', $filename); |
|
| 445 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 446 | + $module = $path[0]; |
|
| 447 | + $filename = $path[1] . '.php'; |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + if (! $module && !empty($obj->moduleName)){ |
|
| 451 | + $module = $obj->moduleName; |
|
| 452 | + } |
|
| 453 | + return array( |
|
| 454 | + 'filename' => $filename, |
|
| 455 | + 'module' => $module |
|
| 456 | + ); |
|
| 457 | + } |
|
| 458 | 458 | |
| 459 | - /** |
|
| 460 | - * Get the name of model or library instance if is null |
|
| 461 | - * @param string $class the class name to determine the instance |
|
| 462 | - * @return string the instance name |
|
| 463 | - */ |
|
| 464 | - protected static function getModelLibraryInstanceName($class){ |
|
| 465 | - //for module |
|
| 466 | - $instance = null; |
|
| 467 | - if (strpos($class, '/') !== false){ |
|
| 468 | - $path = explode('/', $class); |
|
| 469 | - if (isset($path[1])){ |
|
| 470 | - $instance = strtolower($path[1]); |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - else{ |
|
| 474 | - $instance = strtolower($class); |
|
| 475 | - } |
|
| 476 | - return $instance; |
|
| 477 | - } |
|
| 459 | + /** |
|
| 460 | + * Get the name of model or library instance if is null |
|
| 461 | + * @param string $class the class name to determine the instance |
|
| 462 | + * @return string the instance name |
|
| 463 | + */ |
|
| 464 | + protected static function getModelLibraryInstanceName($class){ |
|
| 465 | + //for module |
|
| 466 | + $instance = null; |
|
| 467 | + if (strpos($class, '/') !== false){ |
|
| 468 | + $path = explode('/', $class); |
|
| 469 | + if (isset($path[1])){ |
|
| 470 | + $instance = strtolower($path[1]); |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + else{ |
|
| 474 | + $instance = strtolower($class); |
|
| 475 | + } |
|
| 476 | + return $instance; |
|
| 477 | + } |
|
| 478 | 478 | |
| 479 | - /** |
|
| 480 | - * Get the library file path using the module information |
|
| 481 | - * @param string $class the class name |
|
| 482 | - * @return string|null the library file path otherwise null will be returned |
|
| 483 | - */ |
|
| 484 | - protected static function getLibraryPathUsingModuleInfo($class){ |
|
| 485 | - $logger = static::getLogger(); |
|
| 486 | - $libraryFilePath = null; |
|
| 487 | - $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 488 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 489 | - $module = $moduleInfo['module']; |
|
| 490 | - $class = $moduleInfo['class']; |
|
| 491 | - $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 492 | - if ($moduleLibraryPath){ |
|
| 493 | - $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 494 | - $libraryFilePath = $moduleLibraryPath; |
|
| 495 | - } |
|
| 496 | - else{ |
|
| 497 | - $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 498 | - } |
|
| 499 | - return $libraryFilePath; |
|
| 500 | - } |
|
| 479 | + /** |
|
| 480 | + * Get the library file path using the module information |
|
| 481 | + * @param string $class the class name |
|
| 482 | + * @return string|null the library file path otherwise null will be returned |
|
| 483 | + */ |
|
| 484 | + protected static function getLibraryPathUsingModuleInfo($class){ |
|
| 485 | + $logger = static::getLogger(); |
|
| 486 | + $libraryFilePath = null; |
|
| 487 | + $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 488 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 489 | + $module = $moduleInfo['module']; |
|
| 490 | + $class = $moduleInfo['class']; |
|
| 491 | + $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 492 | + if ($moduleLibraryPath){ |
|
| 493 | + $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 494 | + $libraryFilePath = $moduleLibraryPath; |
|
| 495 | + } |
|
| 496 | + else{ |
|
| 497 | + $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 498 | + } |
|
| 499 | + return $libraryFilePath; |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - /** |
|
| 503 | - * Load the library |
|
| 504 | - * @param string $libraryFilePath the file path of the library to load |
|
| 505 | - * @param string $class the class name |
|
| 506 | - * @param string $instance the instance |
|
| 507 | - * @param array $params the parameter to use |
|
| 508 | - * @return void |
|
| 509 | - */ |
|
| 510 | - protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 511 | - if ($libraryFilePath){ |
|
| 512 | - $logger = static::getLogger(); |
|
| 513 | - require_once $libraryFilePath; |
|
| 514 | - if (class_exists($class)){ |
|
| 515 | - $c = $params ? new $class($params) : new $class(); |
|
| 516 | - $obj = & get_instance(); |
|
| 517 | - $obj->{$instance} = $c; |
|
| 518 | - static::$loaded[$instance] = $class; |
|
| 519 | - $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 520 | - } |
|
| 521 | - else{ |
|
| 522 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - else{ |
|
| 526 | - show_error('Unable to find library class [' . $class . ']'); |
|
| 527 | - } |
|
| 528 | - } |
|
| 502 | + /** |
|
| 503 | + * Load the library |
|
| 504 | + * @param string $libraryFilePath the file path of the library to load |
|
| 505 | + * @param string $class the class name |
|
| 506 | + * @param string $instance the instance |
|
| 507 | + * @param array $params the parameter to use |
|
| 508 | + * @return void |
|
| 509 | + */ |
|
| 510 | + protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 511 | + if ($libraryFilePath){ |
|
| 512 | + $logger = static::getLogger(); |
|
| 513 | + require_once $libraryFilePath; |
|
| 514 | + if (class_exists($class)){ |
|
| 515 | + $c = $params ? new $class($params) : new $class(); |
|
| 516 | + $obj = & get_instance(); |
|
| 517 | + $obj->{$instance} = $c; |
|
| 518 | + static::$loaded[$instance] = $class; |
|
| 519 | + $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 520 | + } |
|
| 521 | + else{ |
|
| 522 | + show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + else{ |
|
| 526 | + show_error('Unable to find library class [' . $class . ']'); |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | 529 | |
| 530 | - /** |
|
| 531 | - * Load the language |
|
| 532 | - * @param string $languageFilePath the file path of the language to load |
|
| 533 | - * @param string $language the language name |
|
| 534 | - * @return void |
|
| 535 | - */ |
|
| 536 | - protected static function loadLanguage($languageFilePath, $language){ |
|
| 537 | - if ($languageFilePath){ |
|
| 538 | - $logger = static::getLogger(); |
|
| 539 | - $lang = array(); |
|
| 540 | - require_once $languageFilePath; |
|
| 541 | - if (! empty($lang) && is_array($lang)){ |
|
| 542 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 543 | - //Note: may be here the class 'Lang' not yet loaded |
|
| 544 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 545 | - $langObj->addLangMessages($lang); |
|
| 546 | - //free the memory |
|
| 547 | - unset($lang); |
|
| 548 | - } |
|
| 549 | - static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 550 | - $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 551 | - } |
|
| 552 | - else{ |
|
| 553 | - show_error('Unable to find language [' . $language . ']'); |
|
| 554 | - } |
|
| 555 | - } |
|
| 530 | + /** |
|
| 531 | + * Load the language |
|
| 532 | + * @param string $languageFilePath the file path of the language to load |
|
| 533 | + * @param string $language the language name |
|
| 534 | + * @return void |
|
| 535 | + */ |
|
| 536 | + protected static function loadLanguage($languageFilePath, $language){ |
|
| 537 | + if ($languageFilePath){ |
|
| 538 | + $logger = static::getLogger(); |
|
| 539 | + $lang = array(); |
|
| 540 | + require_once $languageFilePath; |
|
| 541 | + if (! empty($lang) && is_array($lang)){ |
|
| 542 | + $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 543 | + //Note: may be here the class 'Lang' not yet loaded |
|
| 544 | + $langObj =& class_loader('Lang', 'classes'); |
|
| 545 | + $langObj->addLangMessages($lang); |
|
| 546 | + //free the memory |
|
| 547 | + unset($lang); |
|
| 548 | + } |
|
| 549 | + static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 550 | + $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 551 | + } |
|
| 552 | + else{ |
|
| 553 | + show_error('Unable to find language [' . $language . ']'); |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | 556 | |
| 557 | - /** |
|
| 558 | - * Get all the autoload using the configuration file |
|
| 559 | - * @return array |
|
| 560 | - */ |
|
| 561 | - private function getResourcesFromAutoloadConfig(){ |
|
| 562 | - $autoloads = array(); |
|
| 563 | - $autoloads['config'] = array(); |
|
| 564 | - $autoloads['languages'] = array(); |
|
| 565 | - $autoloads['libraries'] = array(); |
|
| 566 | - $autoloads['models'] = array(); |
|
| 567 | - $autoloads['functions'] = array(); |
|
| 568 | - //loading of the resources from autoload configuration file |
|
| 569 | - if (file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 570 | - $autoload = array(); |
|
| 571 | - require_once CONFIG_PATH . 'autoload.php'; |
|
| 572 | - if (! empty($autoload) && is_array($autoload)){ |
|
| 573 | - $autoloads = array_merge($autoloads, $autoload); |
|
| 574 | - unset($autoload); |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - //loading autoload configuration for modules |
|
| 578 | - $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 579 | - if (! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 580 | - $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 581 | - } |
|
| 582 | - return $autoloads; |
|
| 583 | - } |
|
| 557 | + /** |
|
| 558 | + * Get all the autoload using the configuration file |
|
| 559 | + * @return array |
|
| 560 | + */ |
|
| 561 | + private function getResourcesFromAutoloadConfig(){ |
|
| 562 | + $autoloads = array(); |
|
| 563 | + $autoloads['config'] = array(); |
|
| 564 | + $autoloads['languages'] = array(); |
|
| 565 | + $autoloads['libraries'] = array(); |
|
| 566 | + $autoloads['models'] = array(); |
|
| 567 | + $autoloads['functions'] = array(); |
|
| 568 | + //loading of the resources from autoload configuration file |
|
| 569 | + if (file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 570 | + $autoload = array(); |
|
| 571 | + require_once CONFIG_PATH . 'autoload.php'; |
|
| 572 | + if (! empty($autoload) && is_array($autoload)){ |
|
| 573 | + $autoloads = array_merge($autoloads, $autoload); |
|
| 574 | + unset($autoload); |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + //loading autoload configuration for modules |
|
| 578 | + $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 579 | + if (! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 580 | + $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 581 | + } |
|
| 582 | + return $autoloads; |
|
| 583 | + } |
|
| 584 | 584 | |
| 585 | - /** |
|
| 586 | - * Load the autoload configuration |
|
| 587 | - * @return void |
|
| 588 | - */ |
|
| 589 | - private function loadResourcesFromAutoloadConfig(){ |
|
| 590 | - $autoloads = array(); |
|
| 591 | - $autoloads['config'] = array(); |
|
| 592 | - $autoloads['languages'] = array(); |
|
| 593 | - $autoloads['libraries'] = array(); |
|
| 594 | - $autoloads['models'] = array(); |
|
| 595 | - $autoloads['functions'] = array(); |
|
| 585 | + /** |
|
| 586 | + * Load the autoload configuration |
|
| 587 | + * @return void |
|
| 588 | + */ |
|
| 589 | + private function loadResourcesFromAutoloadConfig(){ |
|
| 590 | + $autoloads = array(); |
|
| 591 | + $autoloads['config'] = array(); |
|
| 592 | + $autoloads['languages'] = array(); |
|
| 593 | + $autoloads['libraries'] = array(); |
|
| 594 | + $autoloads['models'] = array(); |
|
| 595 | + $autoloads['functions'] = array(); |
|
| 596 | 596 | |
| 597 | - $list = $this->getResourcesFromAutoloadConfig(); |
|
| 598 | - $autoloads = array_merge($autoloads, $list); |
|
| 597 | + $list = $this->getResourcesFromAutoloadConfig(); |
|
| 598 | + $autoloads = array_merge($autoloads, $list); |
|
| 599 | 599 | |
| 600 | - //config autoload |
|
| 601 | - $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 600 | + //config autoload |
|
| 601 | + $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 602 | 602 | |
| 603 | - //languages autoload |
|
| 604 | - $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 603 | + //languages autoload |
|
| 604 | + $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 605 | 605 | |
| 606 | - //libraries autoload |
|
| 607 | - $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 606 | + //libraries autoload |
|
| 607 | + $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 608 | 608 | |
| 609 | - //models autoload |
|
| 610 | - $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 609 | + //models autoload |
|
| 610 | + $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 611 | 611 | |
| 612 | - //functions autoload |
|
| 613 | - $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 614 | - } |
|
| 612 | + //functions autoload |
|
| 613 | + $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 614 | + } |
|
| 615 | 615 | |
| 616 | - /** |
|
| 617 | - * Load the resources autoload array |
|
| 618 | - * @param string $method this object method name to call |
|
| 619 | - * @param array $resources the resource to load |
|
| 620 | - * @return void |
|
| 621 | - */ |
|
| 622 | - private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 623 | - foreach ($resources as $name) { |
|
| 624 | - $this->{$method}($name); |
|
| 625 | - } |
|
| 626 | - } |
|
| 627 | - } |
|
| 616 | + /** |
|
| 617 | + * Load the resources autoload array |
|
| 618 | + * @param string $method this object method name to call |
|
| 619 | + * @param array $resources the resource to load |
|
| 620 | + * @return void |
|
| 621 | + */ |
|
| 622 | + private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 623 | + foreach ($resources as $name) { |
|
| 624 | + $this->{$method}($name); |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + } |
|
@@ -1,293 +1,293 @@ |
||
| 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 Log{ |
|
| 27 | + class Log{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The defined constante for Log level |
|
| 31 | - */ |
|
| 32 | - const NONE = 99999999; |
|
| 33 | - const FATAL = 500; |
|
| 34 | - const ERROR = 400; |
|
| 35 | - const WARNING = 300; |
|
| 36 | - const INFO = 200; |
|
| 37 | - const DEBUG = 100; |
|
| 38 | - const ALL = -99999999; |
|
| 29 | + /** |
|
| 30 | + * The defined constante for Log level |
|
| 31 | + */ |
|
| 32 | + const NONE = 99999999; |
|
| 33 | + const FATAL = 500; |
|
| 34 | + const ERROR = 400; |
|
| 35 | + const WARNING = 300; |
|
| 36 | + const INFO = 200; |
|
| 37 | + const DEBUG = 100; |
|
| 38 | + const ALL = -99999999; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The logger name |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private $logger = 'ROOT_LOGGER'; |
|
| 40 | + /** |
|
| 41 | + * The logger name |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private $logger = 'ROOT_LOGGER'; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * List of valid log level to be checked for the configuration |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
| 46 | + /** |
|
| 47 | + * List of valid log level to be checked for the configuration |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Create new Log instance |
|
| 54 | - */ |
|
| 55 | - public function __construct(){ |
|
| 56 | - } |
|
| 52 | + /** |
|
| 53 | + * Create new Log instance |
|
| 54 | + */ |
|
| 55 | + public function __construct(){ |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Set the logger to identify each message in the log |
|
| 60 | - * @param string $logger the logger name |
|
| 61 | - */ |
|
| 62 | - public function setLogger($logger){ |
|
| 63 | - $this->logger = $logger; |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * Set the logger to identify each message in the log |
|
| 60 | + * @param string $logger the logger name |
|
| 61 | + */ |
|
| 62 | + public function setLogger($logger){ |
|
| 63 | + $this->logger = $logger; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Save the fatal message in the log |
|
| 68 | - * @see Log::writeLog for more detail |
|
| 69 | - * @param string $message the log message to save |
|
| 70 | - */ |
|
| 71 | - public function fatal($message){ |
|
| 72 | - $this->writeLog($message, self::FATAL); |
|
| 73 | - } |
|
| 66 | + /** |
|
| 67 | + * Save the fatal message in the log |
|
| 68 | + * @see Log::writeLog for more detail |
|
| 69 | + * @param string $message the log message to save |
|
| 70 | + */ |
|
| 71 | + public function fatal($message){ |
|
| 72 | + $this->writeLog($message, self::FATAL); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Save the error message in the log |
|
| 77 | - * @see Log::writeLog for more detail |
|
| 78 | - * @param string $message the log message to save |
|
| 79 | - */ |
|
| 80 | - public function error($message){ |
|
| 81 | - $this->writeLog($message, self::ERROR); |
|
| 82 | - } |
|
| 75 | + /** |
|
| 76 | + * Save the error message in the log |
|
| 77 | + * @see Log::writeLog for more detail |
|
| 78 | + * @param string $message the log message to save |
|
| 79 | + */ |
|
| 80 | + public function error($message){ |
|
| 81 | + $this->writeLog($message, self::ERROR); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Save the warning message in the log |
|
| 86 | - * @see Log::writeLog for more detail |
|
| 87 | - * @param string $message the log message to save |
|
| 88 | - */ |
|
| 89 | - public function warning($message){ |
|
| 90 | - $this->writeLog($message, self::WARNING); |
|
| 91 | - } |
|
| 84 | + /** |
|
| 85 | + * Save the warning message in the log |
|
| 86 | + * @see Log::writeLog for more detail |
|
| 87 | + * @param string $message the log message to save |
|
| 88 | + */ |
|
| 89 | + public function warning($message){ |
|
| 90 | + $this->writeLog($message, self::WARNING); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Save the info message in the log |
|
| 95 | - * @see Log::writeLog for more detail |
|
| 96 | - * @param string $message the log message to save |
|
| 97 | - */ |
|
| 98 | - public function info($message){ |
|
| 99 | - $this->writeLog($message, self::INFO); |
|
| 100 | - } |
|
| 93 | + /** |
|
| 94 | + * Save the info message in the log |
|
| 95 | + * @see Log::writeLog for more detail |
|
| 96 | + * @param string $message the log message to save |
|
| 97 | + */ |
|
| 98 | + public function info($message){ |
|
| 99 | + $this->writeLog($message, self::INFO); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Save the debug message in the log |
|
| 104 | - * @see Log::writeLog for more detail |
|
| 105 | - * @param string $message the log message to save |
|
| 106 | - */ |
|
| 107 | - public function debug($message){ |
|
| 108 | - $this->writeLog($message, self::DEBUG); |
|
| 109 | - } |
|
| 102 | + /** |
|
| 103 | + * Save the debug message in the log |
|
| 104 | + * @see Log::writeLog for more detail |
|
| 105 | + * @param string $message the log message to save |
|
| 106 | + */ |
|
| 107 | + public function debug($message){ |
|
| 108 | + $this->writeLog($message, self::DEBUG); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Save the log message |
|
| 114 | - * @param string $message the log message to be saved |
|
| 115 | - * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 116 | - * to allow check the log level threshold. |
|
| 117 | - */ |
|
| 118 | - public function writeLog($message, $level = self::INFO){ |
|
| 119 | - $configLogLevel = get_config('log_level'); |
|
| 120 | - if(! $configLogLevel){ |
|
| 121 | - //so means no need log just stop here |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - //check config log level |
|
| 125 | - if(! self::isValidConfigLevel($configLogLevel)){ |
|
| 126 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 127 | - show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
| 128 | - } |
|
| 112 | + /** |
|
| 113 | + * Save the log message |
|
| 114 | + * @param string $message the log message to be saved |
|
| 115 | + * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 116 | + * to allow check the log level threshold. |
|
| 117 | + */ |
|
| 118 | + public function writeLog($message, $level = self::INFO){ |
|
| 119 | + $configLogLevel = get_config('log_level'); |
|
| 120 | + if(! $configLogLevel){ |
|
| 121 | + //so means no need log just stop here |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + //check config log level |
|
| 125 | + if(! self::isValidConfigLevel($configLogLevel)){ |
|
| 126 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 127 | + show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - //check if config log_logger_name and current log can save log data |
|
| 131 | - if(! $this->canSaveLogDataForLogger()){ |
|
| 132 | - return; |
|
| 133 | - } |
|
| 130 | + //check if config log_logger_name and current log can save log data |
|
| 131 | + if(! $this->canSaveLogDataForLogger()){ |
|
| 132 | + return; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - //if $level is not an integer |
|
| 136 | - if(! is_numeric($level)){ |
|
| 137 | - $level = self::getLevelValue($level); |
|
| 138 | - } |
|
| 135 | + //if $level is not an integer |
|
| 136 | + if(! is_numeric($level)){ |
|
| 137 | + $level = self::getLevelValue($level); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - //check if can logging regarding the log level config |
|
| 141 | - $configLevel = self::getLevelValue($configLogLevel); |
|
| 142 | - if($configLevel > $level){ |
|
| 143 | - //can't log |
|
| 144 | - return; |
|
| 145 | - } |
|
| 146 | - //check log file and directory |
|
| 147 | - $path = $this->checkAndSetLogFileDirectory(); |
|
| 148 | - //save the log data |
|
| 149 | - $this->saveLogData($path, $level, $message); |
|
| 150 | - } |
|
| 140 | + //check if can logging regarding the log level config |
|
| 141 | + $configLevel = self::getLevelValue($configLogLevel); |
|
| 142 | + if($configLevel > $level){ |
|
| 143 | + //can't log |
|
| 144 | + return; |
|
| 145 | + } |
|
| 146 | + //check log file and directory |
|
| 147 | + $path = $this->checkAndSetLogFileDirectory(); |
|
| 148 | + //save the log data |
|
| 149 | + $this->saveLogData($path, $level, $message); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Save the log data into file |
|
| 154 | - * @param string $path the path of the log file |
|
| 155 | - * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 156 | - * @param string $message the log message to save |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - protected function saveLogData($path, $level, $message){ |
|
| 160 | - //may be at this time helper user_agent not yet included |
|
| 161 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 152 | + /** |
|
| 153 | + * Save the log data into file |
|
| 154 | + * @param string $path the path of the log file |
|
| 155 | + * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 156 | + * @param string $message the log message to save |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + protected function saveLogData($path, $level, $message){ |
|
| 160 | + //may be at this time helper user_agent not yet included |
|
| 161 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 162 | 162 | |
| 163 | - ///////////////////// date ////////////// |
|
| 164 | - $timestampWithMicro = microtime(true); |
|
| 165 | - $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 166 | - $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 167 | - $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 168 | - //ip |
|
| 169 | - $ip = get_ip(); |
|
| 163 | + ///////////////////// date ////////////// |
|
| 164 | + $timestampWithMicro = microtime(true); |
|
| 165 | + $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 166 | + $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 167 | + $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 168 | + //ip |
|
| 169 | + $ip = get_ip(); |
|
| 170 | 170 | |
| 171 | - //if $level is not an integer |
|
| 172 | - if(! is_numeric($level)){ |
|
| 173 | - $level = self::getLevelValue($level); |
|
| 174 | - } |
|
| 171 | + //if $level is not an integer |
|
| 172 | + if(! is_numeric($level)){ |
|
| 173 | + $level = self::getLevelValue($level); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - //level name |
|
| 177 | - $levelName = self::getLevelName($level); |
|
| 176 | + //level name |
|
| 177 | + $levelName = self::getLevelName($level); |
|
| 178 | 178 | |
| 179 | - //debug info |
|
| 180 | - $dtrace = debug_backtrace(); |
|
| 181 | - $fileInfo = $dtrace[0]; |
|
| 182 | - if ($dtrace[0]['file'] == __FILE__ || $dtrace[1]['file'] == __FILE__){ |
|
| 183 | - $fileInfo = $dtrace[2]; |
|
| 184 | - } |
|
| 179 | + //debug info |
|
| 180 | + $dtrace = debug_backtrace(); |
|
| 181 | + $fileInfo = $dtrace[0]; |
|
| 182 | + if ($dtrace[0]['file'] == __FILE__ || $dtrace[1]['file'] == __FILE__){ |
|
| 183 | + $fileInfo = $dtrace[2]; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 187 | - $fp = fopen($path, 'a+'); |
|
| 188 | - if(is_resource($fp)){ |
|
| 189 | - flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 190 | - fwrite($fp, $str); |
|
| 191 | - fclose($fp); |
|
| 192 | - } |
|
| 193 | - } |
|
| 186 | + $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 187 | + $fp = fopen($path, 'a+'); |
|
| 188 | + if(is_resource($fp)){ |
|
| 189 | + flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 190 | + fwrite($fp, $str); |
|
| 191 | + fclose($fp); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Check if the current logger can save log data regarding the configuration |
|
| 197 | - * of logger filter |
|
| 198 | - * @return boolean |
|
| 199 | - */ |
|
| 200 | - protected function canSaveLogDataForLogger(){ |
|
| 201 | - if(! empty($this->logger)){ |
|
| 202 | - $configLoggersName = get_config('log_logger_name', array()); |
|
| 203 | - if (!empty($configLoggersName)) { |
|
| 204 | - //for best comparaison put all string to lowercase |
|
| 205 | - $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 206 | - if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - return true; |
|
| 212 | - } |
|
| 195 | + /** |
|
| 196 | + * Check if the current logger can save log data regarding the configuration |
|
| 197 | + * of logger filter |
|
| 198 | + * @return boolean |
|
| 199 | + */ |
|
| 200 | + protected function canSaveLogDataForLogger(){ |
|
| 201 | + if(! empty($this->logger)){ |
|
| 202 | + $configLoggersName = get_config('log_logger_name', array()); |
|
| 203 | + if (!empty($configLoggersName)) { |
|
| 204 | + //for best comparaison put all string to lowercase |
|
| 205 | + $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 206 | + if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + return true; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Check the file and directory |
|
| 216 | - * @return string the log file path |
|
| 217 | - */ |
|
| 218 | - protected function checkAndSetLogFileDirectory(){ |
|
| 219 | - $logSavePath = get_config('log_save_path'); |
|
| 220 | - if(! $logSavePath){ |
|
| 221 | - $logSavePath = LOGS_PATH; |
|
| 222 | - } |
|
| 214 | + /** |
|
| 215 | + * Check the file and directory |
|
| 216 | + * @return string the log file path |
|
| 217 | + */ |
|
| 218 | + protected function checkAndSetLogFileDirectory(){ |
|
| 219 | + $logSavePath = get_config('log_save_path'); |
|
| 220 | + if(! $logSavePath){ |
|
| 221 | + $logSavePath = LOGS_PATH; |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 225 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 226 | - show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 227 | - } |
|
| 224 | + if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 225 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 226 | + show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 230 | - if(! file_exists($path)){ |
|
| 231 | - touch($path); |
|
| 232 | - } |
|
| 233 | - return $path; |
|
| 234 | - } |
|
| 229 | + $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 230 | + if(! file_exists($path)){ |
|
| 231 | + touch($path); |
|
| 232 | + } |
|
| 233 | + return $path; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Check if the given log level is valid |
|
| 238 | - * |
|
| 239 | - * @param string $level the log level |
|
| 240 | - * |
|
| 241 | - * @return boolean true if the given log level is valid, false if not |
|
| 242 | - */ |
|
| 243 | - protected static function isValidConfigLevel($level){ |
|
| 244 | - $level = strtolower($level); |
|
| 245 | - return in_array($level, self::$validConfigLevel); |
|
| 246 | - } |
|
| 236 | + /** |
|
| 237 | + * Check if the given log level is valid |
|
| 238 | + * |
|
| 239 | + * @param string $level the log level |
|
| 240 | + * |
|
| 241 | + * @return boolean true if the given log level is valid, false if not |
|
| 242 | + */ |
|
| 243 | + protected static function isValidConfigLevel($level){ |
|
| 244 | + $level = strtolower($level); |
|
| 245 | + return in_array($level, self::$validConfigLevel); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * Get the log level number for the given level string |
|
| 250 | - * @param string $level the log level in string format |
|
| 251 | - * |
|
| 252 | - * @return int the log level in integer format using the predefined constants |
|
| 253 | - */ |
|
| 254 | - protected static function getLevelValue($level){ |
|
| 255 | - $level = strtolower($level); |
|
| 256 | - $levelMaps = array( |
|
| 257 | - 'fatal' => self::FATAL, |
|
| 258 | - 'error' => self::ERROR, |
|
| 259 | - 'warning' => self::WARNING, |
|
| 260 | - 'warn' => self::WARNING, |
|
| 261 | - 'info' => self::INFO, |
|
| 262 | - 'debug' => self::DEBUG, |
|
| 263 | - 'all' => self::ALL |
|
| 264 | - ); |
|
| 265 | - //the default value is NONE, so means no need test for NONE |
|
| 266 | - $value = self::NONE; |
|
| 267 | - if(isset($levelMaps[$level])){ |
|
| 268 | - $value = $levelMaps[$level]; |
|
| 269 | - } |
|
| 270 | - return $value; |
|
| 271 | - } |
|
| 248 | + /** |
|
| 249 | + * Get the log level number for the given level string |
|
| 250 | + * @param string $level the log level in string format |
|
| 251 | + * |
|
| 252 | + * @return int the log level in integer format using the predefined constants |
|
| 253 | + */ |
|
| 254 | + protected static function getLevelValue($level){ |
|
| 255 | + $level = strtolower($level); |
|
| 256 | + $levelMaps = array( |
|
| 257 | + 'fatal' => self::FATAL, |
|
| 258 | + 'error' => self::ERROR, |
|
| 259 | + 'warning' => self::WARNING, |
|
| 260 | + 'warn' => self::WARNING, |
|
| 261 | + 'info' => self::INFO, |
|
| 262 | + 'debug' => self::DEBUG, |
|
| 263 | + 'all' => self::ALL |
|
| 264 | + ); |
|
| 265 | + //the default value is NONE, so means no need test for NONE |
|
| 266 | + $value = self::NONE; |
|
| 267 | + if(isset($levelMaps[$level])){ |
|
| 268 | + $value = $levelMaps[$level]; |
|
| 269 | + } |
|
| 270 | + return $value; |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - /** |
|
| 274 | - * Get the log level string for the given log level integer |
|
| 275 | - * @param integer $level the log level in integer format |
|
| 276 | - * @return string the log level in string format |
|
| 277 | - */ |
|
| 278 | - protected static function getLevelName($level){ |
|
| 279 | - $levelMaps = array( |
|
| 280 | - self::FATAL => 'FATAL', |
|
| 281 | - self::ERROR => 'ERROR', |
|
| 282 | - self::WARNING => 'WARNING', |
|
| 283 | - self::INFO => 'INFO', |
|
| 284 | - self::DEBUG => 'DEBUG' |
|
| 285 | - ); |
|
| 286 | - $value = ''; |
|
| 287 | - if(isset($levelMaps[$level])){ |
|
| 288 | - $value = $levelMaps[$level]; |
|
| 289 | - } |
|
| 290 | - return $value; |
|
| 291 | - } |
|
| 273 | + /** |
|
| 274 | + * Get the log level string for the given log level integer |
|
| 275 | + * @param integer $level the log level in integer format |
|
| 276 | + * @return string the log level in string format |
|
| 277 | + */ |
|
| 278 | + protected static function getLevelName($level){ |
|
| 279 | + $levelMaps = array( |
|
| 280 | + self::FATAL => 'FATAL', |
|
| 281 | + self::ERROR => 'ERROR', |
|
| 282 | + self::WARNING => 'WARNING', |
|
| 283 | + self::INFO => 'INFO', |
|
| 284 | + self::DEBUG => 'DEBUG' |
|
| 285 | + ); |
|
| 286 | + $value = ''; |
|
| 287 | + if(isset($levelMaps[$level])){ |
|
| 288 | + $value = $levelMaps[$level]; |
|
| 289 | + } |
|
| 290 | + return $value; |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - } |
|
| 293 | + } |
|