From a7159502bd4543247d782da8af81eef1af25e548 Mon Sep 17 00:00:00 2001 From: stadchenko Date: Wed, 28 Mar 2018 12:37:42 +0300 Subject: [PATCH] Logger behaviour change --- Components/Memcached.php | 6 +-- Harmonious.php | 100 +++++++++++++++++++++------------------ Log.php | 83 +++++++++++++------------------- Logger.php | 4 +- 4 files changed, 92 insertions(+), 101 deletions(-) diff --git a/Components/Memcached.php b/Components/Memcached.php index 85c8a25..de43488 100644 --- a/Components/Memcached.php +++ b/Components/Memcached.php @@ -37,12 +37,12 @@ class Harmonious_Components_Memcached implements IHarmonious_Component{ function get($key, $usecas = false) { $m = $this->get_server($key); if (!$usecas) $ret = $m->get($key); - else $ret = $m->get($key, null, $cas); + else $ret = $m->get($key, null, Memcached::GET_EXTENDED); if ($ret === false) $retcode = $m->getResultCode(); unset($m); if ($ret === false) return array ('result'=>false, 'result_code'=>$retcode, 'result_string'=>$this->retcode2string($retcode)); - $ret = array ('result'=>true, 'value'=>$ret); - if ($usecas) $ret['cas'] = $cas; + if (!$usecas) $ret = array ('result'=>true, 'value'=>$ret); + else $ret = array ('result'=>true, 'value'=>$ret, 'cas'=>$ret['cas']); return $ret; } diff --git a/Harmonious.php b/Harmonious.php index 6d24b02..77cd251 100644 --- a/Harmonious.php +++ b/Harmonious.php @@ -24,11 +24,11 @@ } spl_autoload_register(array('Harmonious', 'autoloader')); - + class Harmonious { protected $template_params = array(); - protected $components; + protected $components; protected $request; protected $response; protected $view; @@ -46,13 +46,13 @@ 'error.500' => array(array()), 'error.halt' => array(array()) ); - + /** * Constructor * @param array $userSettings * @return void */ - public function __construct( $userSettings = array() ) { + public function __construct( $userSettings = array() ) { //Merge application settings $this->settings = array_merge(array( //Mode @@ -91,7 +91,7 @@ $this->getMode(); $this->components = new Harmonious_Components_Factory($this); - + //Setup HTTP request and response handling $this->request = $this->components['request']; $this->response = $this->components['response'];; @@ -109,7 +109,7 @@ if ( $sessionHandler instanceof Harmonious_Session_Handler ) { $sessionHandler->register($this); } - session_cache_limiter(false); + session_cache_limiter(false); session_start(); } @@ -120,25 +120,28 @@ set_error_handler(array('Harmonious', 'handleErrors')); //set_exception_handler(array('Harmonious', 'handleExceptions')); - + register_shutdown_function(array($this, 'FatalErrorCatcher')); } - + /** * Run the Harmonious application * @return void */ public function run() { try { - try { + try { $this->applyHook('app.before', $this); ob_start(); - $this->applyHook('app.before.router', $this); $httpMethod = $this->request->getMethod(); $uri = rtrim($this->request->getResourceUri(), "/"); if ($uri == '') $uri = '/index'; $controller_name = $this->config('controller_path') . $uri . "_controller.php"; $controller_class_name = substr($uri, strrpos($uri, '/') + 1) . "Controller"; + $controller_class_name = str_replace('.', '_', $controller_class_name); + $hook_responce = $this->applyHook('app.before.router', array($this, $controller_name, $controller_class_name)); + $controller_name = $hook_responce[1]; + $controller_class_name = $hook_responce[2]; if (file_exists($controller_name)) { include ($controller_name); //создать экземпляр класса и проверить поддерживается ли http метод, если нет - ошибка 405 @@ -169,13 +172,13 @@ } catch ( Slim_Exception_Stop $e ) { //Exit application context } - } - + } + /** * "Магические" методы для работы с компонентами Harmonious. * http://www.php.net/manual/ru/language.oop5.magic.php - * - * Возвращает компонент Harmonious из фабрики. + * + * Возвращает компонент Harmonious из фабрики. * @param type $name - Имя компонента * @return type - объект(компонент Harmonious) */ @@ -183,7 +186,7 @@ if ($name == 'components') return $this->components; return $this->components[$name]; } - + /** * Добавляет компонент в фабрику * @param type $name - имя компонента @@ -193,22 +196,22 @@ { $this->components[$name] = $value; } - + /****** Параметры шаблона ******/ - + /** * Глобальный массив параметров шаблона используется для наполнения шаблона параметрами из различных * участков кода. Например, часть параметров может быть передана посредством хука, а остальные посредством * контроллена. См. также метод render, в котором этот глобальные массив объединяется с переданным перед * передачей в шаблон. - * + * * Объединяет два массива: переданный в параметре и глобальный массив параметров шаблона - * @param type $array + * @param type $array */ public function addTemplateParam($array) { $this->template_params = array_merge($this->template_params, $array); } - + /** * Чистит глобальный массив параметров шаблона */ @@ -216,15 +219,15 @@ unset($this->template_params); $this->template_params = array(); } - + /** * Возвращает глобальный массив параметров шаблона - * @return type + * @return type */ public function getTemplateParams() { return $this->template_params; } - + /** * Render a template * @param string $template The name of the template passed into the View::render method @@ -237,8 +240,8 @@ $data = array_merge($this->template_params, $data); $this->view->appendData($data); $this->view->display($template); - } - + } + /** * Render a template * @param string $template The name of the template passed into the View::render method @@ -251,8 +254,8 @@ $data = array_merge($this->template_params, $data); $this->view->appendData($data); return $this->view->fetch($template); - } - + } + /** * Harmonious auto-loader * Этот метод производит загрузку файлов с классами при первом обращении к ним. Грузит свои классы и классы Slim-а @@ -260,19 +263,19 @@ */ public static function autoloader( $className ) { $className = str_replace('Slim', 'Harmonious', $className); - + if ( strpos($className, 'Harmonious') !== 0 ) { return; } $file = dirname(__FILE__) . '/' . str_replace('_', DIRECTORY_SEPARATOR, substr($className, 11)) . '.php'; - + if ( file_exists($file) ) { require $file; } } - - - /***** CONFIGURATION *****/ + + + /***** CONFIGURATION *****/ /** * Get application mode * @return string @@ -285,7 +288,7 @@ } return $this->mode; } - + /** * Configure Slim for a given mode * @@ -330,27 +333,27 @@ } else { $this->settings[$name] = $value; } - } - + } + /***** LOGGING *****/ /** * Get application Log (lazy-loaded) - * @return Slim_Log + * @return Harmonious_Log */ public function getLog() { if ( !isset($this->log) ) { - $this->log = new Slim_Log(); + $this->log = new Harmonious_Log(); $this->log->setEnabled($this->config('log.enable')); $logger = $this->config('log.logger'); if ( $logger ) { $this->log->setLogger($logger); } else { - $this->log->setLogger(new Slim_Logger($this->config('log.path'), $this->config('log.level'))); + $this->log->setLogger(new Harmonious_Logger($this->config('log.path'), $this->config('log.level'))); } } return $this->log; - } + } /** * Not Found Handler @@ -374,7 +377,7 @@ $this->halt(500, ob_get_clean()); } - /***** ACCESSORS *****/ + /***** ACCESSORS *****/ /** * Get and/or set the View * @@ -668,8 +671,8 @@ } else { throw new InvalidArgumentException('Slim::redirect only accepts HTTP 300-307 status codes.'); } - } - + } + /***** HOOKS *****/ /** @@ -706,7 +709,8 @@ foreach( $this->hooks[$name] as $priority ) { if( !empty($priority) ) { foreach($priority as $callable) { - $hookArg = call_user_func($callable, $hookArg); + if (is_array($hookArg)) $hookArg = call_user_func_array($callable, $hookArg); + else $hookArg = call_user_func($callable, $hookArg); } } } @@ -770,6 +774,8 @@ * @throws ErrorException */ public static function handleErrors( $errno, $errstr = '', $errfile = '', $errline = '' ) { + if (isset($this)) $this->getLog()->error('handleErrors catched: '.$errstr); + if ( error_reporting() & $errno ) { if (isset($this)) $this->getLog()->error('Unhandled Error: '.$errstr); throw new ErrorException($errstr, $errno, 0, $errfile, $errline); @@ -781,7 +787,7 @@ $this->getLog()->error('Unhandled Exception: '.$exception->getMessage()); throw $exception; } - + public function FatalErrorCatcher() { $error = error_get_last(); @@ -809,7 +815,7 @@ } } } - + /** * Generate markup for error message * @@ -866,6 +872,6 @@ protected function defaultError() { echo self::generateTemplateMarkup('Error', '

A website error has occured. The website administrator has been notified of the issue. Sorry for the temporary inconvenience.

'); } - - } + + } ?> diff --git a/Log.php b/Log.php index b958890..25bfe50 100644 --- a/Log.php +++ b/Log.php @@ -50,12 +50,12 @@ * @author Josh Lockhart * @since Version 1.0 */ -class Slim_Log { +class Harmonious_Log { /** * @var mixed An object that implements expected Logger interface */ - protected $logger; + protected $loggers = array(); /** * @var bool Enable logging? @@ -90,66 +90,51 @@ class Slim_Log { return $this->enabled; } - /** - * Log debug message - * @param mixed $object - * @return mixed|false What the Logger returns, or false if Logger not set or not enabled - */ - public function debug( $object ) { - return isset($this->logger) && $this->isEnabled() ? $this->logger->debug($object) : false; - } - - /** - * Log info message - * @param mixed $object - * @return mixed|false What the Logger returns, or false if Logger not set or not enabled - */ - public function info( $object ) { - return isset($this->logger) && $this->isEnabled() ? $this->logger->info($object) : false; - } - - /** - * Log warn message - * @param mixed $object - * @return mixed|false What the Logger returns, or false if Logger not set or not enabled - */ - public function warn( $object ) { - return isset($this->logger) && $this->isEnabled() ? $this->logger->warn($object) : false; + public function __call($name, $args) { + if ($this->isEnabled()) { + $name = strtolower($name); + $result = true; + foreach ($this->loggers as $logger) { + switch ($name) { + case 'debug': + if (!$logger->debug($args)) $result = false; + break; + case 'info': + if (!$logger->info($args)) $result = false; + break; + case 'warn': + if (!$logger->warn($args)) $result = false; + break; + case 'error': + if (!$logger->error($args)) $result = false; + break; + case 'fatal': + if (!$logger->fatal($args)) $result = false; + break; + default: + return false; + break; + } + } + return $result; + } else return false; } - - /** - * Log error message - * @param mixed $object - * @return mixed|false What the Logger returns, or false if Logger not set or not enabled - */ - public function error( $object ) { - return isset($this->logger) && $this->isEnabled() ? $this->logger->error($object) : false; - } - - /** - * Log fatal message - * @param mixed $object - * @return mixed|false What the Logger returns, or false if Logger not set or not enabled - */ - public function fatal( $object ) { - return isset($this->logger) && $this->isEnabled() ? $this->logger->fatal($object) : false; - } - + /** * Set Logger * @param mixed $logger * @return void */ public function setLogger( $logger ) { - $this->logger = $logger; + $this->loggers[] = $logger; } /** * Get Logger * @return mixed */ - public function getLogger() { - return $this->logger; + public function getLoggers() { + return $this->loggers; } } \ No newline at end of file diff --git a/Logger.php b/Logger.php index b55eaa2..3c685e7 100644 --- a/Logger.php +++ b/Logger.php @@ -42,7 +42,7 @@ * @author Josh Lockhart * @since Version 1.0 */ -class Slim_Logger { +class Harmonious_Logger { /** * @var array Log levels @@ -184,7 +184,7 @@ class Slim_Logger { throw new RuntimeException("Log directory '$dir' not writable."); } if ( $level <= $this->getLevel() ) { - $this->write(sprintf("[%s] %s - %s\r\n", $this->levels[$level], date('c'), (string)$data)); + $this->write(sprintf("[%s] %s - %s\r\n", $this->levels[$level], date('c'), (string)$data[0])); } }