<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.5 |
+ | CiviCRM version 4.6 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
-*/
+ */
/**
- * To ensure that PHP errors or unhandled exceptions are reported in JSON format,
- * wrap this around your code. For example:
+ * To ensure that PHP errors or unhandled exceptions are reported in JSON
+ * format, wrap this around your code. For example:
*
* @code
* $errorContainer = new CRM_Queue_ErrorPolicy();
- * $errorContainer->call(function(){
+ * $errorContainer->call(function() {
* ...include some files, do some work, etc...
* });
* @endcode
* will be necessary to get reuse from the other parts of this class.
*/
class CRM_Queue_ErrorPolicy {
- var $active;
+ public $active;
/**
- * @param null $level
+ * @param null|int $level
+ * PHP error level to capture (e.g. E_PARSE|E_USER_ERROR).
*/
- function __construct($level = NULL) {
+ public function __construct($level = NULL) {
register_shutdown_function(array($this, 'onShutdown'));
if ($level === NULL) {
$level = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
$this->level = $level;
}
- function activate() {
+ /**
+ * Enable the error policy.
+ */
+ public function activate() {
$this->active = TRUE;
$this->backup = array();
foreach (array(
- 'display_errors', 'html_errors', 'xmlrpc_errors') as $key) {
+ 'display_errors',
+ 'html_errors',
+ 'xmlrpc_errors',
+ ) as $key) {
$this->backup[$key] = ini_get($key);
ini_set($key, 0);
}
$this->errorScope = CRM_Core_TemporaryErrorScope::useException();
}
- function deactivate() {
+ /**
+ * Disable the error policy.
+ */
+ public function deactivate() {
$this->errorScope = NULL;
restore_error_handler();
foreach (array(
- 'display_errors', 'html_errors', 'xmlrpc_errors') as $key) {
+ 'display_errors',
+ 'html_errors',
+ 'xmlrpc_errors',
+ ) as $key) {
ini_set($key, $this->backup[$key]);
}
$this->active = FALSE;
}
/**
- * @param $callable
+ * Execute the callable. Activate and deactivate the error policy
+ * automatically.
+ *
+ * @param callable|array|string $callable
+ * A callback function.
*
* @return mixed
*/
- function call($callable) {
+ public function call($callable) {
$this->activate();
try {
$result = $callable();
}
- catch(Exception$e) {
+ catch (Exception$e) {
$this->reportException($e);
}
$this->deactivate();
*
* @see set_error_handler
*/
- function onError($errno, $errstr, $errfile, $errline) {
+ public function onError($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno)) {
return TRUE;
}
* @see register_shutdown_function
* @see error_get_last
*/
- function onShutdown() {
+ public function onShutdown() {
if (!$this->active) {
return;
}
/**
* Print a fatal error
*
- * @param $error
+ * @param array $error
+ * The PHP error (with "type", "message", etc).
*/
- function reportError($error) {
+ public function reportError($error) {
$response = array(
'is_error' => 1,
'is_continue' => 0,
/**
* Print an unhandled exception
*
- * @param $e
+ * @param Exception $e
+ * The unhandled exception.
*/
- function reportException(Exception $e) {
+ public function reportException(Exception $e) {
CRM_Core_Error::debug_var('CRM_Queue_ErrorPolicy_reportException', CRM_Core_Error::formatTextException($e));
$response = array(
if (is_object($activeQueueRunner)) {
$response['last_task_title'] = $activeQueueRunner->lastTaskTitle;
}
- echo json_encode($response);
- CRM_Utils_System::civiExit();
+ CRM_Utils_JSON::output($response);
}
-}
+}