Skip to content

Commit

Permalink
Merge pull request #402 from phalcon/2.0.x
Browse files Browse the repository at this point in the history
2.0.7
  • Loading branch information
sergeyklay committed Aug 18, 2015
2 parents 43bf209 + 71f0743 commit 3ca8169
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 29 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 2 additions & 4 deletions Library/Phalcon/Error/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -66,9 +66,7 @@ public function main()
private function registerAutoloaders()
{
$loader = new Loader();
$loader->registerNamespaces(array(
'Phalcon\Error' => '.',
));
$loader->registerNamespaces(['Phalcon\Error' => '.']);
$loader->register();
}

Expand Down
9 changes: 4 additions & 5 deletions Library/Phalcon/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -21,7 +21,6 @@

class Error
{

/**
* @var array
*/
Expand All @@ -32,17 +31,17 @@ 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' => '',
'line' => '',
'exception' => null,
'isException' => false,
'isError' => false,
);
];

$options = array_merge($defaults, $options);

Expand Down
33 changes: 19 additions & 14 deletions Library/Phalcon/Error/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -15,15 +15,16 @@
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
| Nikita Vershinin <endeveit@gmail.com> |
| Serghei Iakovlev <sadhooklay@gmail.com> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Error;

use Phalcon\DI;
use Phalcon\Di;
use Phalcon\Logger\Formatter;

class Handler
{

/**
* Registers itself as error and exception handler.
*
Expand All @@ -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));
});
Expand All @@ -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:
Expand All @@ -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();
Expand Down
9 changes: 4 additions & 5 deletions Library/Phalcon/Error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<?php
return [
'error' => [
'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',
]
Expand All @@ -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
Expand All @@ -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

<?php
class Application extends \Phalcon\Mvc\Application
{
Expand All @@ -55,7 +55,6 @@ class Application extends \Phalcon\Mvc\Application
In the error controller \Phalcon\Error\Error can be retrieved through the dispatcher:

```php

public function indexAction()
{
$error = $this->dispatcher->getParam('error');
Expand Down Expand Up @@ -84,4 +83,4 @@ Error message could be displayed to the user this way:
<pre><?php echo $error->exception()->getTraceAsString(); ?></pre>
<?php } ?>
<?php endif; ?>
```
```
29 changes: 29 additions & 0 deletions Library/Phalcon/Mvc/View/Engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Library/Phalcon/Mvc/View/Engine/Smarty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions Library/Phalcon/Mvc/View/SmartyView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Phalcon\Mvc\View;

use \Phalcon\Mvc\View;

class SmartyView extends View
{
public function __construct()
{
parent::__construct();
}

public function setVar($key, $value, $nocache = false)
{
$this->_viewParams[$key] = $value;
$this->_viewParams["_" . $key] = $nocache;
}
}

0 comments on commit 3ca8169

Please sign in to comment.