apply() public static method

public static apply ( $object, array $conditions, $handler )
$conditions array
Beispiel #1
0
<?php

/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2011, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */
use lithium\core\ErrorHandler;
use lithium\action\Response;
use lithium\net\http\Media;
use lithium\analysis\Debugger;
ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) {
    $stack = Debugger::trace(array('format' => 'array', 'trace' => $info['exception']->getTrace()));
    $exception_class = get_class($info['exception']);
    array_unshift($stack, array('functionRef' => '[exception]', 'file' => $info['exception']->getFile(), 'line' => $info['exception']->getLine()));
    $response = new Response(array('request' => $params['request'], 'status' => $info['exception']->getCode()));
    Media::render($response, compact('info', 'params', 'stack', 'exception_class'), array('controller' => 'errors', 'template' => 'development', 'layout' => 'error', 'request' => $params['request']));
    return $response;
});
Beispiel #2
0
<?php

/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2011, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */
use lithium\core\ErrorHandler;
use lithium\action\Response;
use lithium\net\http\Media;
ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) {
    $response = new Response(array('request' => $params['request'], 'status' => $info['exception']->getCode()));
    Media::render($response, compact('info', 'params'), array('library' => true, 'controller' => '_errors', 'template' => 'development', 'layout' => 'error', 'request' => $params['request']));
    return $response;
});
 public function testRenderedOutput()
 {
     ob_start();
     echo 'Some Output';
     $subject = new ErrorHandlerTest();
     ErrorHandler::apply(array($subject, 'throwException'), array(), function ($details) {
     });
     $subject->throwException();
     $this->assertFalse(ob_get_length());
 }
Beispiel #4
0
<?php

/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2013, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */
use lithium\core\Libraries;
use lithium\core\ErrorHandler;
use lithium\action\Response;
use lithium\net\http\Media;
use lithium\analysis\Logger;
ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) {
    if (preg_match('/not found/i', $info['exception']->getMessage())) {
        $code = 404;
    } else {
        $code = $info['exception']->getCode() == 404 ? 404 : 500;
    }
    $response = new Response(array('request' => $params['request'], 'status' => $code));
    Media::render($response, compact('info', 'params'), array('library' => true, 'controller' => '_errors', 'template' => $code == 404 ? 'fourohfour' : 'fiveohoh', 'layout' => 'default', 'request' => $params['request']));
    return $response;
});
Logger::config(['default' => ['adapter' => 'File', 'path' => dirname(Libraries::get(true, 'path')) . '/log', 'timestamp' => '[Y-m-d H:i:s]', 'file' => function ($data, $config) {
    return 'app.log';
}, 'priority' => ['debug', 'error', 'notice', 'warning']]]);
Beispiel #5
0
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */
use lithium\core\ErrorHandler;
use lithium\action\Response;
use lithium\net\http\Media;
use lithium\core\Environment;
use lithium\core\Libraries;
ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) {
    $response = new Response(array('request' => $params['request'], 'status' => $info['exception']->getCode()));
    // Production error templates should follow the design of the site.
    $error_layout = 'default';
    $error_template = 'production';
    $appCfg = Libraries::get();
    $defaultApp = false;
    $defaultLibrary = null;
    foreach ($appCfg as $k => $library) {
        if ($library['default'] === true) {
            $defaultApp = $library;
            $defaultLibrary = $k;
        }
    }
    // Development error templates can look different.
    if (Environment::is('development')) {
        $error_layout = file_exists($defaultApp['path'] . '/views/layouts/error.html.php') ? 'error' : $error_layout;
        $error_template = 'development';
    }
    // If the error templates don't exist use li3b_core's.
    $error_library = file_exists($defaultApp['path'] . '/views/layouts/' . $error_layout . '.html.php') && file_exists($defaultApp['path'] . '/views/_errors/' . $error_template . '.html.php') ? $defaultLibrary : 'li3b_core';
    Media::render($response, compact('info', 'params'), array('library' => $error_library, 'controller' => '_errors', 'template' => $error_template, 'layout' => $error_layout, 'request' => $params['request']));
    return $response;
});
<?php
/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2011, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */

use lithium\core\ErrorHandler;
use lithium\action\Response;
use lithium\net\http\Media;

ErrorHandler::apply('lithium\action\Dispatcher::run', array(), function($info, $params) {
	$response = new Response(array('request' => $params['request']));

	Media::render($response, compact('info', 'params'), array(
		'controller' => '_errors',
		'template' => 'development',
		'layout' => 'error',
		'request' => $params['request']
	));
	return $response;
});

?>
Beispiel #7
0
 */
use lithium\core\ErrorHandler;
use lithium\action\Response;
use lithium\net\http\Media;
use app\extensions\util\HttpUserAgentUtil;
ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) {
    $response = new Response(array('request' => $params['request'], 'status' => $info['exception']->getCode()));
    // 根据抛出来的错误类型而进行处理, 这里分开发和生产环境, 方便开发环境进行异常处理
    $exception = $info['exception'];
    $template = 'production';
    $layout = 'default';
    $mobile = false;
    if (!empty($_SERVER['HTTP_USER_AGENT']) && HttpUserAgentUtil::is_mobile($_SERVER['HTTP_USER_AGENT'])) {
        $mobile = true;
    }
    $environment = environment_config();
    if ($environment['environment'] == 'production') {
        if ($mobile) {
            $layout = 'mobile';
            $template = 'mobile';
        }
    } else {
        $layout = 'error';
        $template = 'development';
    }
    if ($exception instanceof \app\extensions\core\exception\WeixinLoginException) {
        $template = $environment['environment'] == 'production' ? 'business/weixin_login_failed' : 'development';
    }
    Media::render($response, compact('info', 'params'), array('library' => true, 'controller' => '_errors', 'template' => $template, 'layout' => $layout, 'request' => $params['request']));
    return $response;
});
Beispiel #8
0
ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) {
    if ($params['request']->type() != "html") {
        return;
    }
    $response = new Response(array('request' => $params['request']));
    ini_set('highlight.string', '#4DDB4A');
    ini_set('highlight.comment', '#D42AAE');
    ini_set('highlight.keyword', '#D42AAE');
    ini_set('highlight.default', '#3C96FF');
    ini_set('highlight.htm', '#FFFFFF');
    $exception = $info['exception'];
    $replace = array('&lt;?php', '?&gt;', '<code>', '</code>', "\n");
    $context = 5;
    $stack = Debugger::trace(array('format' => 'array', 'trace' => $exception->getTrace()));
    array_unshift($stack, array('functionRef' => '[exception]', 'file' => $exception->getFile(), 'line' => $exception->getLine()));
    foreach ($stack as &$frame) {
        $location = "{$frame['file']}: {$frame['line']}";
        $lines = range($frame['line'] - $context, $frame['line'] + $context);
        $code = Inspector::lines($frame['file'], $lines);
        if ($code) {
            foreach ($code as $num => &$content) {
                $numPad = str_pad($num, 3, ' ');
                $content = str_ireplace(array('<?php', '?>'), '', $content);
                $content = highlight_string("<?php {$numPad}{$content} ?>", true);
                $content = str_replace($replace, '', $content);
            }
        }
        $frame += compact('location', 'lines', 'code');
    }
    $code = $exception->getCode();
    $class = get_class($exception);
    $message = $exception->getMessage();
    $data = compact('info', 'params', 'class', 'stack', 'code', 'exception');
    Media::render($response, $data, array('controller' => '_errors', 'type' => 'html', 'template' => 'development', 'layout' => 'error', 'request' => $params['request']));
    return $response;
});
Beispiel #9
0
 public function testApply()
 {
     $subject = new ErrorHandlerTest();
     ErrorHandler::apply(array($subject, 'throwException'), array(), function ($details) {
         return $details['exception']->getMessage();
     });
     $this->assertEqual('foo', $subject->throwException());
 }
Beispiel #10
0
<?php

include dirname(__DIR__) . '/vendor/autoload.php';
use lithium\core\ErrorHandler;
use Whoops\Run;
use Whoops\Handler\PrettyPageHandler;
ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) {
    $run = new Run();
    $handler = new PrettyPageHandler();
    $jsonHandler = new \Whoops\Handler\JsonResponseHandler();
    $jsonHandler->onlyForAjaxRequests(true);
    $run->pushHandler($handler);
    $run->pushHandler($jsonHandler);
    $request = $params['request'];
    $exception = $info['exception'];
    $handler->addDataTable('Request', array('URL' => $request->url, 'Query String' => isset($request->params['query']) ? $request->params['query'] : '<none>', 'HTTP Host' => $request->get('http:host'), 'HTTP Method' => $request->get('http:method'), 'Base Path' => $request->path, 'Scheme' => $request->scheme, 'Port' => $request->port, 'Host' => $request->host, 'Auth' => $request->auth, 'Username' => $request->username, 'Password' => $request->password, 'Protocol' => $request->protocol, 'Version' => $request->version));
    $handler->addDataTable('Params', $request->params);
    $handler->addDataTable('Data', $request->data);
    $handler->addDataTable('Headers', $request->headers);
    $handler->addDataTable('Cookies', $request->cookies);
    $handler->addDataTable('Body', $request->body);
    $run->writeToOutput(false);
    return $run->handleException($exception);
});