From 0d064dc6060e278baf475be85d2cec5d296c194d Mon Sep 17 00:00:00 2001 From: Mike Naberezny Date: Mon, 9 Feb 2009 19:03:12 -0800 Subject: [PATCH] Added Horde_Routes_Printer. --- framework/Routes/lib/Horde/Routes/Printer.php | 120 ++++++++++++++++++++++++++ framework/Routes/lib/Horde/Routes/Utils.php | 13 +++ 2 files changed, 133 insertions(+) create mode 100644 framework/Routes/lib/Horde/Routes/Printer.php diff --git a/framework/Routes/lib/Horde/Routes/Printer.php b/framework/Routes/lib/Horde/Routes/Printer.php new file mode 100644 index 000000000..57e13f42a --- /dev/null +++ b/framework/Routes/lib/Horde/Routes/Printer.php @@ -0,0 +1,120 @@ +_mapper = $mapper; + } + + /** + * Pretty-print a listing of the routes connected to the mapper. + * + * @param stream|null $stream Output stream for printing (optional) + * @param string|null $eol Line ending (optional) + * @return void + */ + public function printRoutes($stream = null, $eol = PHP_EOL) + { + $routes = $this->getRoutes(); + if (empty($routes)) { return; } + + if ($stream === null) { + $stream = fopen('php://output', 'a'); + } + + // find the max $widths to size the output columns {'name'=>40, 'method'=>6, ...} + $widths = array(); + foreach (array_keys($routes[0]) as $key) { + $width = 0; + foreach($routes as $r) { + $l = strlen($r[$key]); + if ($l > $width) { $width = $l; } + } + $widths[$key] = $width; + } + + // print the output + foreach ($routes as $r) { + fwrite($stream, str_pad($r['name'], $widths['name'], ' ', STR_PAD_LEFT) . ' '); + fwrite($stream, str_pad($r['method'], $widths['method'], ' ', STR_PAD_RIGHT) . ' '); + fwrite($stream, str_pad($r['path'], $widths['path'], ' ', STR_PAD_RIGHT) . ' '); + fwrite($stream, $r['hardcodes'] . $eol); + } + } + + /** + * Analyze the mapper and return an array of data about the + * routes connected to the mapper. + * + * @return array + */ + public function getRoutes() + { + /** + * Traverse all routes connected to the mapper in match order, + * and assemble an array of $routes used to build the output + */ + $routes = array(); + foreach ($this->_mapper->matchList as $route) { + // name of this route, or empty string if anonymous + $routeName = ''; + foreach ($this->_mapper->routeNames as $name => $namedRoute) { + if ($route === $namedRoute) { $routeName = $name; break; } + } + + // request_method types recognized by this route, or empty string for any + $methods = array(''); + if (isset($route->conditions['method']) && is_array($route->conditions['method']) ) { + $methods = $route->conditions['method']; + } + + // hardcoded defaults that can't be overriden by the request path as {:key=>"value"} + $hardcodes = array(); + foreach ($route->hardCoded as $key) { + $value = isset($route->defaults[$key]) ? $route->defaults[$key] : 'NULL'; + $dump = ":{$key}=>\"{$value}\""; + ($key == 'controller') ? array_unshift($hardcodes, $dump) : $hardcodes[] = $dump; + } + $hardcodes = empty($hardcodes) ? '' : '{'. implode(', ', $hardcodes) .'}'; + + // route data for output + foreach ($methods as $method) { + $routes[] = array('name' => $routeName, + 'method' => $method, + 'path' => '/' . $route->routePath, + 'hardcodes' => $hardcodes); + } + } + + return $routes; + } + +} diff --git a/framework/Routes/lib/Horde/Routes/Utils.php b/framework/Routes/lib/Horde/Routes/Utils.php index adbec274c..dbc2e4e1f 100644 --- a/framework/Routes/lib/Horde/Routes/Utils.php +++ b/framework/Routes/lib/Horde/Routes/Utils.php @@ -243,6 +243,19 @@ class Horde_Routes_Utils } /** + * Pretty-print a listing of the routes connected to the mapper. + * + * @param stream|null $stream Output stream for printing (optional) + * @param string|null $eol Line ending (optional) + * @return void + */ + public function printRoutes($stream = null, $eol = PHP_EOL) + { + $printer = new Horde_Routes_Printer($this->mapper); + $printer->printRoutes($stream, $eol); + } + + /** * Scan a directory for PHP files and use them as controllers. Used * as the default scanner callback for Horde_Routes_Mapper. See the * constructor of that class for more information. -- 2.11.0