/**
  * Respond function to echo json with status and data
  *
  * @param boolean $success Success status of the call
  * @param integer|boolean $status The StatusCode of the call
  * @param array|boolean $data Extra data
  *
  * @return array
  */
 protected static function respond($success, $status = false, $data = false)
 {
     Request::contentType('application/json');
     $reply = ['success' => $success];
     $reply['multiple'] = false;
     $reply['status'] = [];
     if ($status !== false) {
         if (is_array($status)) {
             $reply['multiple'] = true;
             $reply['status']['codes'] = [];
             $reply['status']['hex'] = [];
             $reply['status']['names'] = [];
             $reply['status']['messages'] = [];
             foreach ($status as $stat) {
                 $object = StatusCodeHandler::find($stat);
                 $reply['status']['codes'][] = $object->code();
                 $reply['status']['hex'][] = $object->hex();
                 $reply['status']['names'][] = $object->name();
                 $reply['status']['messages'][] = $object->readable();
             }
         } else {
             $object = StatusCodeHandler::find($status);
             $reply['status']['code'] = $object->code();
             $reply['status']['hex'] = $object->hex();
             $reply['status']['name'] = $object->name();
             $reply['status']['message'] = $object->readable();
         }
     }
     if ($data !== false) {
         $reply['data'] = $data;
     }
     return $reply;
 }
Exemple #2
0
<?php

/**
 * Register development routes which are available when
 * the development status is on
 */
namespace Application;

use Application\Core\App;
use Application\Core\Router;
use Application\Core\HTMLHandler;
use Application\Core\StatusCodeHandler;
Router::restrict(function () {
    return App::environment() == 'dev' || App::environment() == 'development';
}, function () {
    Router::get('xtend/codes', function () {
        $table = HTMLHandler::createDocument()->createElement('table');
        $codes = StatusCodeHandler::all();
        foreach ($codes as $code) {
            $row = $table->createElement('tr');
            $row->createElement('td')->addText($code->hex());
            $row->createElement('td')->addText($code->name());
            $row->createElement('td')->addText($code->readable());
        }
        $table->write(true);
    });
});
Exemple #3
0
 /**
  * Throws an application error and sets an HTTP code
  *
  * @param integer $code
  *
  * @return boolean
  */
 public static function throw($code)
 {
     header("HTTP/1.0 {$code}");
     $error = StatusCodeHandler::find($code);
     if ($error instanceof StatusCode) {
         LogHandler::write($error, Request::path() . "\t" . $_SERVER["REMOTE_ADDR"]);
         return Router::throw($code);
     }
     return false;
 }
Exemple #4
0
 /**
  * Registers error routes
  *
  * @param string|xTend\Objects\Route
  * @param mixed $route
  * @param string|boolean $alias
  *
  * @return xTend\Objects\Route
  */
 public static function error($handle, $route = false, $alias = false, $override = false)
 {
     //here the handler should be an errorcode
     //you can either pass an actual handle as the handle
     //or directly pass a route object as the handle
     //ignoring the route and alias parameters completely
     $h;
     if (is_numeric($handle) && $route !== false) {
         $h = new Route($handle, $route, $alias);
     } elseif ($handle instanceof Route) {
         $h = $handle;
     } elseif (StatusCodeHandler::find($handle) instanceof StatusCode) {
         $h = new Route(StatusCodeHandler::find($handle)->code(), $route, $alias);
     }
     if ($override === true || !isset($routes[$h->handle()])) {
         //add route to the error list
         self::$_error[$h->handle()] = $h;
         //add to aliases if there is any
         if ($h->alias() !== false) {
             self::$_aliases[$h->alias()] = $h;
         }
     } elseif (isset($routes[$h->handle()])) {
         $h = $routes[$h->handle()];
     }
     //return Route object
     return $h;
 }
Exemple #5
0
<?php

/**
 * Registers the application's
 * status codes
 */
namespace Application;

use Application\Core\StatusCodeHandler;
StatusCodeHandler::register(0x0, 'statuscodehandler:invalid-code', 'Error in StatusCodeHandler: Trying to register an invalid code');
StatusCodeHandler::register(0x1, 'statuscodehandler:invalid-name', 'Error in StatusCodeHandler: Trying to register an invalid name');
StatusCodeHandler::register(0x194, 'http:404', 'HTTP 404: Page not found');
StatusCodeHandler::register(0x2, 'controllerhandler:invalid-controller-definition', 'Error while trying to pass data to an initialized controller. Data methods not implemented.');
StatusCodeHandler::register(0x3, 'viewhandler:invalid-view-definition', 'Error while trying to pass data to an initialized view. Data methods not implemented.');
StatusCodeHandler::register(0x4, 'viewhandler:invalid-view-definition', 'Error while trying to execute a view object. Execute method not implemented.');