From: Michael J. Rubinsky Date: Wed, 10 Dec 2008 17:37:33 +0000 (-0500) Subject: * Add Horde_Controller_StatusCodes class X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=aed9dae48f1817560cd08d3b01ec513e08295a0d;p=horde.git * Add Horde_Controller_StatusCodes class * Missed adding the urlFor() method directly to Horde_Controller_Base --- diff --git a/framework/Controller/lib/Horde/Controller/Base.php b/framework/Controller/lib/Horde/Controller/Base.php index 438c63bf7..7b67c661c 100644 --- a/framework/Controller/lib/Horde/Controller/Base.php +++ b/framework/Controller/lib/Horde/Controller/Base.php @@ -167,6 +167,26 @@ abstract class Horde_Controller_Base } /** + */ + protected function interpretStatus($status) + { + return Horde_Controller_StatusCodes::interpret($status); + } + + /** + * Generate a URL + * @see Horde_Controller_UrlWriter + * + * @param string|array $first named route, string, or options array + * @param array $second options array (if not in $first) + * @return string generated url + */ + protected function urlFor($first = array(), $second = array()) + { + return $this->getUrlWriter()->urlFor($first, $second); + } + + /** * Get an instance of UrlWriter for this controller. * * @return Horde_Controller_UrlWriter diff --git a/framework/Controller/lib/Horde/Controller/StatusCodes.php b/framework/Controller/lib/Horde/Controller/StatusCodes.php new file mode 100644 index 000000000..9c56149d0 --- /dev/null +++ b/framework/Controller/lib/Horde/Controller/StatusCodes.php @@ -0,0 +1,139 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Controller + */ +class Horde_Controller_StatusCodes +{ + /** + * All known status codes and their messages + * @var array + */ + public static $statusCodes = array( + 100 => "Continue", + 101 => "Switching Protocols", + 102 => "Processing", + + 200 => "OK", + 201 => "Created", + 202 => "Accepted", + 203 => "Non-Authoritative Information", + 204 => "No Content", + 205 => "Reset Content", + 206 => "Partial Content", + 207 => "Multi-Status", + 226 => "IM Used", + + 300 => "Multiple Choices", + 301 => "Moved Permanently", + 302 => "Found", + 303 => "See Other", + 304 => "Not Modified", + 305 => "Use Proxy", + 307 => "Temporary Redirect", + + 400 => "Bad Request", + 401 => "Unauthorized", + 402 => "Payment Required", + 403 => "Forbidden", + 404 => "Not Found", + 405 => "Method Not Allowed", + 406 => "Not Acceptable", + 407 => "Proxy Authentication Required", + 408 => "Request Timeout", + 409 => "Conflict", + 410 => "Gone", + 411 => "Length Required", + 412 => "Precondition Failed", + 413 => "Request Entity Too Large", + 414 => "Request-URI Too Long", + 415 => "Unsupported Media Type", + 416 => "Requested Range Not Satisfiable", + 417 => "Expectation Failed", + 422 => "Unprocessable Entity", + 423 => "Locked", + 424 => "Failed Dependency", + 426 => "Upgrade Required", + + 500 => "Internal Server Error", + 501 => "Not Implemented", + 502 => "Bad Gateway", + 503 => "Service Unavailable", + 504 => "Gateway Timeout", + 505 => "HTTP Version Not Supported", + 507 => "Insufficient Storage", + 510 => "Not Extended" + ); + + /** + * Given a status parameter, determine whether it needs to be converted + * to a string. If it is an integer, use the $statusCodes hash to lookup + * the default message. If it is a string, build $symbolToStatusCode + * and convert it. + * + * interpret(404) => "404 Not Found" + * interpret("notFound") => "404 Not Found" + * + * Differences from Rails: + * - $status is camelized, not underscored. + * - an unknown status raises an exception + * + * @param string|integer Status code or "symbol" + * @return string Header + */ + public static function interpret($status) + { + // Status from integer or numeric string + if (is_numeric($status)) { + if (isset(self::$statusCodes[$status])) { + return $status . ' ' . self::$statusCodes[$status]; + } else { + $msg = 'Unknown status code: ' . $status; + throw new InvalidArgumentException($msg); + } + + // Status from string + } elseif (is_string($status)) { + // Build a string-to-integer lookup for converting a symbol (like + // 'created' or 'notImplemented') into its corresponding HTTP status + // code (like 200 or 501). + static $symbolToStatusCode = array(); + $inflector = new Horde_Support_Inflector(); + if (empty($symbolToStatusCode)) { + foreach (self::$statusCodes as $code => $message) { + $symbol = $inflector->camelize($message, $first='lower'); + $symbolToStatusCode[$symbol] = $code; + } + } + + // Convert status symbol to integer code, return header + if (isset($symbolToStatusCode[$status])) { + return self::interpret($symbolToStatusCode[$status]); + } + + // Error: Status symbol could not be converted to an integer code + // Try to help if the developer mixed up underscore/camel + $msg = 'Unknown status: \'' . $status . '\''; + if (strpos($status, '_')) { + $status = $inflector->camelize($status, $first='lower'); + if (isset($symbolToStatusCode[$status])) { + $msg .= ' (underscore), did you mean \'' . $status . '\' (camel)?'; + } + } + throw new InvalidArgumentException($msg); + + // Status is an unknown type + } else { + $msg = '$status must be numeric or string, got ' + . gettype($status); + throw new InvalidArgumentException($msg); + } + + } + +} diff --git a/framework/Controller/package.xml b/framework/Controller/package.xml index 3f2781eeb..f66efa046 100644 --- a/framework/Controller/package.xml +++ b/framework/Controller/package.xml @@ -54,6 +54,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -75,6 +76,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> +