/** * Use the new resolving mechanism to call for local services. */ static function call($service, $method, $parameters = array(), $options = array()) { $hostname = System::getHostname('service'); if (!$hostname) { throw new ServiceException('Service hostname undefined.'); } if (is_string($options)) { $options = array('method' => $options); } if (isset($options['type']) && empty($options['method'])) { $options['method'] = $options['type']; } $options = (array) $options + self::$defaultOptions; $prefix = conf::get('web::resolvers.service.prefix', '/service'); $options['uri'] = array('scheme' => (bool) @$options['secure'] ? 'https' : 'http', 'host' => $hostname, 'path' => "{$prefix}/{$service}/{$method}/" . implode('/', array_map('urlencode', (array) $parameters))); unset($prefix); // Customizable Resolver if (@$options['resolver'] instanceof Resolver) { $serviceResolver = $options['resolver']; } else { $serviceResolver = Resolver::getActiveInstance(); } unset($options['resolver']); if (@$options['response'] instanceof Response) { $serviceResponse = $options['response']; } else { $serviceResponse = new Response(); } unset($options['response']); $serviceRequest = new Request($options); // Explicitly force this request to be local. $serviceRequest->__local = true; return $serviceRequest->send($serviceResolver, $serviceResponse); }
public function __construct($message, $code = 0, \Exception $previous = null) { $res = Resolver::getActiveInstance(); // Resolve message from database, with the exception code. if ($res && $code) { $res = $res->response(); if ($res) { $res = $res->__("exception.{$code}", 'Exception'); if ($res) { if (is_array($message)) { $message = call_user_func_array('sprintf', $message); } } } } if (is_array($message)) { $message = implode(' ', $message); } if (!$message) { $message = sprintf('Exception #%d', $code); } parent::__construct($message, $code, $previous); }
<?php error_reporting(E_ALL); ini_set('display_errors', 1); use FlickrTestApp\Config\AppConfig; use Framework\Resolver; use Framework\Router; use Framework\Request; // Composer autoload $loader = (require __DIR__ . '/../vendor/autoload.php'); // Load application configuration define('APP_CONFIG', realpath(__DIR__ . '/../src/FlickrTestApp/Config/AppConfig.php')); // Check if the app configuration is readable if (!is_readable(APP_CONFIG)) { die('No application configuration found. Please create an application configuration in ' . APP_CONFIG); } // Setup routes from AppConfig $router = new Router(AppConfig::$routes); // URL resolver $resolver = Resolver::build($router); // Build the current request to be served $request = Request::fromCurrent(); // Built response object $response = $resolver->handle($request); // Dispatch the response $response->dispatch();
public static function handleException($e) { if (error_reporting() == 0) { return; } while (ob_get_level() > 0) { ob_end_clean(); } $eS = $e->getMessage(); $eN = $e->getCode(); $eC = $e->getTrace(); // Put current context into stack trace array_unshift($eC, array('file' => $e->getFile(), 'line' => $e->getLine())); if ($e instanceof ErrorException) { switch ($e->getSeverity()) { case E_ERROR: case E_PARSE: case E_CORE_ERROR: case E_USER_ERROR: default: $logType = LogLevel::CRITICAL; break; case E_WARNING: case E_CORE_WARNING: case E_USER_WARNING: $logType = LogLevel::WARNING; break; case E_DEPRECATED: case E_NOTICE: case E_USER_DEPRECATED: case E_USER_NOTICE: $logType = LogLevel::NOTICE; break; case E_STRICT: $logType = LogLevel::INFO; break; } $exceptionType = 'error'; } else { $exceptionType = get_class($e); if (strpos($exceptionType, '\\') !== false) { $exceptionType = substr(strrchr($exceptionType, '\\'), 1); } $logType = LogLevel::ERROR; } $logString = sprintf('[Gateway] Uncaught %s#%d with message: "%s".', $exceptionType, $eN, $eS); unset($exceptionType); // Current request context $resolver = Resolver::getActiveInstance(); if ($resolver) { if ($resolver->request()) { $client = $resolver->request()->client(); } $response = $resolver->response(); } unset($resolver); // Prevent recursive errors on logging when database fails to connect. if (Database::isConnected()) { // Release table locks of current session. @Database::unlockTables(false); if (Database::inTransaction()) { @Database::rollback(); } } $logContext = array_filter(array('errorContext' => $eC, 'client' => @$client)); // Log the error try { @Log::log($logType, $logString, $logContext); } catch (\Exception $e) { } unset($logContext); // Send the error to output $output = array('error' => $eS, 'code' => $eN); if (System::environment(false) != System::ENV_PRODUCTION) { $output['trace'] = $eC; } // Display error message if (isset($response) && @$client['type'] != 'cli') { // Do i18n when repsonse context is available if ($e instanceof GeneralException) { $errorMessage = $response->__($eS, $logType); if ($errorMessage) { $output['error'] = $errorMessage; } } if ($e instanceof ErrorException) { $statusCode = 500; } else { $statusCode = 400; } if ($e instanceof ValidationException) { $output['errors'] = $e->getErrors(); } $response->clearHeaders(); $response->header('Content-Type', 'application/json; charset=utf-8'); $response->send($output, $statusCode); } else { header('Content-Type: text/plain', true, 500); echo "{$logString}\n"; // Debug stack trace if (System::environment(false) != System::ENV_PRODUCTION) { echo "Trace:\n"; array_walk($eC, function ($stack, $index) { $trace = $index + 1 . '.'; $function = implode('->', array_filter(array(@$stack['class'], @$stack['function']))); if ($function) { $trace .= " {$function}()"; } unset($function); if (@$stack['file']) { $trace .= " {$stack['file']}"; if (@$stack['line']) { $trace .= ":{$stack['line']}"; } } echo "{$trace}\n"; }); } } // CLI exit code on Exceptions and Errors if (in_array($logType, array(LogLevel::ERROR, LogLevel::CRITICAL, LogLevel::ALERT, LogLevel::EMERGENCY))) { $exitCode = $e->getCode(); if ($exitCode <= 0) { $exitCode = 1; } die($exitCode); } }
public function setup() { $this->router = new Router($this->routes); $this->resolver = Resolver::build($this->router); }
/** * Fire this request object as a request. * * @param {?Resolver} $resolver If provided, this request will be resolved by * it instead of creating a real HTTP request. * A real CURL request will be made upon omission. * @param {?Response} $response Response object for the request, a new response * object will be created if omitted. * * @return {Response} The response object after resolution. */ public function send(Resolver $resolver = null, Response $response = null) { if ($this->resolver) { trigger_error('Active request cannot be fired again.', E_USER_WARNING); return; } if ($resolver) { $this->resolver = $resolver; $resolver->run($this, $response); return $resolver->response(); } // TODO: Handle file uploads? // Creates a CURL request upon current request context. Net::httpRequest(array('url' => http_build_url($this->uri()), 'data' => array_replace_recursive((array) $this->param(), (array) $this->file()), 'type' => $this->method(), 'headers' => $this->header(), 'success' => function ($responseText, $options) use(&$response) { if ($response === null) { $response = new Response(); } foreach (array_filter(preg_split('/\\r?\\n/', @$options['response']['headers'])) as $value) { $response->header($value); } $response->send($responseText, (int) @$options['status']); }, 'failure' => function ($errNum, $errMsg, $options) { throw new FrameworkException($errMsg, $errNum); })); return $response; }
/** * @constructor * * Controllers have the context of current HTTP request. */ public function __construct(TaskInstance $instance) { $this->taskInstance = $instance; $this->resolver = Resolver::getActiveInstance(); }