From 0d4fc82d5c2e76ea49dc519a5f8d7845b837f8c7 Mon Sep 17 00:00:00 2001 From: CurtisBaldwinson Date: Fri, 31 Jul 2015 00:08:31 -0700 Subject: [PATCH 1/4] Make Smarty adapter compatible with nocache optional argument --- Library/Phalcon/Mvc/View/Engine/README.md | 29 ++++++++++++++++++++++ Library/Phalcon/Mvc/View/Engine/Smarty.php | 6 ++++- Library/Phalcon/Mvc/View/SmartyView.php | 19 ++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Library/Phalcon/Mvc/View/SmartyView.php diff --git a/Library/Phalcon/Mvc/View/Engine/README.md b/Library/Phalcon/Mvc/View/Engine/README.md index d7fd90570..e064d188b 100644 --- a/Library/Phalcon/Mvc/View/Engine/README.md +++ b/Library/Phalcon/Mvc/View/Engine/README.md @@ -170,6 +170,35 @@ $di->set('view', function() { }); ``` +Smarty's equivalent to Phalcon's "setVar($key, $value)" function is "assign($key, $value, $nocache = false)" which has a third optional argument. This third argument, when set to true, marks the variable as exempt from caching. This is an essential Smarty feature that other template engines lack, being useful for pages that have portions that are often changing such as the current user who is logged in. If you want to utilize this additional argument, use the incubator SmartyView instead of View which extends View to include this functionality. + +```php +//Setting up the view component +use Phalcon\Mvc\View\SmartyView; +$di->set('view', function() { + + $view = new SmartyView(); + + $view->setViewsDir('../app/views/'); + + $view->registerEngines( + array(".tpl" => 'Phalcon\Mvc\View\Engine\Smarty') + ); + + return $view; +}); +``` + +You may now use the setVar function you are familiar with in Phalcon with the third, optional argument: + +```php +// This variable is exempt from caching +$this->view->setVar($key, $value, true); + +// This variable can be cached, as $nocache is false by default +$this->view->setVar($key, $value); +``` + Smarty can be configured to alter its default behavior, the following example explain how to do that: ```php diff --git a/Library/Phalcon/Mvc/View/Engine/Smarty.php b/Library/Phalcon/Mvc/View/Engine/Smarty.php index 0ac1f844c..cbe603c78 100644 --- a/Library/Phalcon/Mvc/View/Engine/Smarty.php +++ b/Library/Phalcon/Mvc/View/Engine/Smarty.php @@ -50,7 +50,11 @@ public function render($path, $params, $mustClean = false) $params['content'] = $this->_view->getContent(); } foreach ($params as $key => $value) { - $this->smarty->assign($key, $value); + if ($params['_' . $key] === true) { + $this->smarty->assign($key, $value, true); + } else { + $this->smarty->assign($key, $value); + } } $content = $this->smarty->fetch($path); diff --git a/Library/Phalcon/Mvc/View/SmartyView.php b/Library/Phalcon/Mvc/View/SmartyView.php new file mode 100644 index 000000000..ae1f1eba7 --- /dev/null +++ b/Library/Phalcon/Mvc/View/SmartyView.php @@ -0,0 +1,19 @@ +_viewParams[$key] = $value; + $this->_viewParams["_" . $key] = $nocache; + } +} \ No newline at end of file From 38b3c61a5f76545c3dc74fd55360c3de9685e0a9 Mon Sep 17 00:00:00 2001 From: CurtisBaldwinson Date: Fri, 31 Jul 2015 00:08:31 -0700 Subject: [PATCH 2/4] Make Smarty adapter compatible with nocache optional argument --- Library/Phalcon/Mvc/View/Engine/README.md | 29 ++++++++++++++++++++++ Library/Phalcon/Mvc/View/Engine/Smarty.php | 6 ++++- Library/Phalcon/Mvc/View/SmartyView.php | 19 ++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Library/Phalcon/Mvc/View/SmartyView.php diff --git a/Library/Phalcon/Mvc/View/Engine/README.md b/Library/Phalcon/Mvc/View/Engine/README.md index d7fd90570..e064d188b 100644 --- a/Library/Phalcon/Mvc/View/Engine/README.md +++ b/Library/Phalcon/Mvc/View/Engine/README.md @@ -170,6 +170,35 @@ $di->set('view', function() { }); ``` +Smarty's equivalent to Phalcon's "setVar($key, $value)" function is "assign($key, $value, $nocache = false)" which has a third optional argument. This third argument, when set to true, marks the variable as exempt from caching. This is an essential Smarty feature that other template engines lack, being useful for pages that have portions that are often changing such as the current user who is logged in. If you want to utilize this additional argument, use the incubator SmartyView instead of View which extends View to include this functionality. + +```php +//Setting up the view component +use Phalcon\Mvc\View\SmartyView; +$di->set('view', function() { + + $view = new SmartyView(); + + $view->setViewsDir('../app/views/'); + + $view->registerEngines( + array(".tpl" => 'Phalcon\Mvc\View\Engine\Smarty') + ); + + return $view; +}); +``` + +You may now use the setVar function you are familiar with in Phalcon with the third, optional argument: + +```php +// This variable is exempt from caching +$this->view->setVar($key, $value, true); + +// This variable can be cached, as $nocache is false by default +$this->view->setVar($key, $value); +``` + Smarty can be configured to alter its default behavior, the following example explain how to do that: ```php diff --git a/Library/Phalcon/Mvc/View/Engine/Smarty.php b/Library/Phalcon/Mvc/View/Engine/Smarty.php index 0ac1f844c..cbe603c78 100644 --- a/Library/Phalcon/Mvc/View/Engine/Smarty.php +++ b/Library/Phalcon/Mvc/View/Engine/Smarty.php @@ -50,7 +50,11 @@ public function render($path, $params, $mustClean = false) $params['content'] = $this->_view->getContent(); } foreach ($params as $key => $value) { - $this->smarty->assign($key, $value); + if ($params['_' . $key] === true) { + $this->smarty->assign($key, $value, true); + } else { + $this->smarty->assign($key, $value); + } } $content = $this->smarty->fetch($path); diff --git a/Library/Phalcon/Mvc/View/SmartyView.php b/Library/Phalcon/Mvc/View/SmartyView.php new file mode 100644 index 000000000..e19c9e517 --- /dev/null +++ b/Library/Phalcon/Mvc/View/SmartyView.php @@ -0,0 +1,19 @@ +_viewParams[$key] = $value; + $this->_viewParams["_" . $key] = $nocache; + } +} From 20465432898b4214c970fdcf86073cd3d98bfea8 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 16 Aug 2015 23:05:46 +0300 Subject: [PATCH 3/4] Added support for formatter to Phalcon\Error\Handler --- Library/Phalcon/Error/Application.php | 6 ++--- Library/Phalcon/Error/Error.php | 9 ++++---- Library/Phalcon/Error/Handler.php | 33 +++++++++++++++------------ Library/Phalcon/Error/README.md | 9 ++++---- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Library/Phalcon/Error/Application.php b/Library/Phalcon/Error/Application.php index 0a1b461b8..6c4b2e814 100644 --- a/Library/Phalcon/Error/Application.php +++ b/Library/Phalcon/Error/Application.php @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -66,9 +66,7 @@ public function main() private function registerAutoloaders() { $loader = new Loader(); - $loader->registerNamespaces(array( - 'Phalcon\Error' => '.', - )); + $loader->registerNamespaces(['Phalcon\Error' => '.']); $loader->register(); } diff --git a/Library/Phalcon/Error/Error.php b/Library/Phalcon/Error/Error.php index e62500075..0daee35de 100644 --- a/Library/Phalcon/Error/Error.php +++ b/Library/Phalcon/Error/Error.php @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -21,7 +21,6 @@ class Error { - /** * @var array */ @@ -32,9 +31,9 @@ class Error * * @param array $options */ - public function __construct(array $options = array()) + public function __construct(array $options = []) { - $defaults = array( + $defaults = [ 'type' => -1, 'message' => 'No error message', 'file' => '', @@ -42,7 +41,7 @@ public function __construct(array $options = array()) 'exception' => null, 'isException' => false, 'isError' => false, - ); + ]; $options = array_merge($defaults, $options); diff --git a/Library/Phalcon/Error/Handler.php b/Library/Phalcon/Error/Handler.php index 042247d43..eb6fade21 100644 --- a/Library/Phalcon/Error/Handler.php +++ b/Library/Phalcon/Error/Handler.php @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -15,15 +15,16 @@ | Authors: Andres Gutierrez | | Eduar Carvajal | | Nikita Vershinin | + | Serghei Iakovlev | +------------------------------------------------------------------------+ */ namespace Phalcon\Error; -use Phalcon\DI; +use Phalcon\Di; +use Phalcon\Logger\Formatter; class Handler { - /** * Registers itself as error and exception handler. * @@ -50,26 +51,26 @@ public static function register() return; } - $options = array( + $options = [ 'type' => $errno, 'message' => $errstr, 'file' => $errfile, 'line' => $errline, 'isError' => true, - ); + ]; static::handle(new Error($options)); }); set_exception_handler(function (\Exception $e) { - $options = array( + $options = [ 'type' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'isException' => true, 'exception' => $e, - ); + ]; static::handle(new Error($options)); }); @@ -89,12 +90,16 @@ public static function register() */ public static function handle(Error $error) { - $di = DI::getDefault(); - $config = $di->getShared('config'); + $di = Di::getDefault(); + $config = $di->getShared('config')->error; $type = static::getErrorType($error->type()); $message = "$type: {$error->message()} in {$error->file()} on line {$error->line()}"; - $config->error->logger->log($message); + if (isset($config->formatter) && $config->formatter instanceof Formatter) { + $config->logger->setFormatter($config->formatter); + } + + $config->logger->log($message); switch ($error->type()) { case E_WARNING: @@ -119,13 +124,13 @@ public static function handle(Error $error) $view = $di->getShared('view'); $response = $di->getShared('response'); - $dispatcher->setControllerName($config->error->controller); - $dispatcher->setActionName($config->error->action); - $dispatcher->setParams(array('error' => $error)); + $dispatcher->setControllerName($config->controller); + $dispatcher->setActionName($config->action); + $dispatcher->setParams(['error' => $error]); $view->start(); $dispatcher->dispatch(); - $view->render($config->error->controller, $config->error->action, $dispatcher->getParams()); + $view->render($config->controller, $config->action, $dispatcher->getParams()); $view->finish(); return $response->setContent($view->getContent())->send(); diff --git a/Library/Phalcon/Error/README.md b/Library/Phalcon/Error/README.md index 9e1ee98ef..7c9b304e5 100644 --- a/Library/Phalcon/Error/README.md +++ b/Library/Phalcon/Error/README.md @@ -8,14 +8,14 @@ Configuration ------------- For the error handler to work properly, following section has to be created -in the configuration file (in this case php array). All options are mandatory: +in the configuration file (in this case php array). The `logger`, `controller`, `action` options are mandatory: ```php - [ 'logger' => new \Phalcon\Logger\Adapter\File(ROOT_PATH . '/log/' . APPLICATION_ENV . '.log'), + 'formatter' => new \Phalcon\Logger\Formatter\Line('[%date%][%type%] %message%', 'Y-m-d H:i:s O'), 'controller' => 'error', 'action' => 'index', ] @@ -25,6 +25,7 @@ return [ * `logger` defines an object used for logging. It has to implement `log` method in order for error handler to work properly. +* `formatter` sets the message formatter. * `controller` is the name of error controller, which will be dispatched, when an exception or error occurs. * `action` is the name of action in the error controller, which will be called, when an exception or error @@ -34,7 +35,6 @@ In the Application file (please take a look at \Phalcon\Error\Application for re error handler has to be registered. Application must also define constants for application environments: ```php - dispatcher->getParam('error'); @@ -84,4 +83,4 @@ Error message could be displayed to the user this way:
exception()->getTraceAsString(); ?>
-``` \ No newline at end of file +``` From 1492523c973cc94f00ffa8c87935484d7aaf5697 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Tue, 18 Aug 2015 08:28:58 +0300 Subject: [PATCH 4/4] Added phalcon-v2.0.7 to Travis CI environment --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 53fa8e7ab..be830ed1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ cache: - $HOME/.composer/cache env: + - PHALCON_VERSION="2.0.7" - PHALCON_VERSION="2.0.6" - PHALCON_VERSION="2.0.5" - PHALCON_VERSION="2.0.4"