This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added metadata to routes, returned in matches. Changed how redirects …

…and aliaseswork, simpler now.
  • Loading branch information...
lox committed Nov 25, 2010
1 parent a5097e3 commit f28fd17d8a0f2a75b6498f1f3195a0eff96b6b05
View
@@ -0,0 +1,83 @@
+<?php
+
+namespace Ergo;
+
+/**
+ * A generic decorator for forwarding method calls and property access to a delegate
+ */
+class Decorator
+{
+ private $__instance;
+
+ /**
+ * Constructor.
+ * @param Object $object
+ */
+ public function __construct($object)
+ {
+ $this->__setObject($object);
+ }
+
+ /**
+ * Returns the decorated object.
+ */
+ public function __getObject()
+ {
+ return $this->__instance;
+ }
+
+ /**
+ * Replace the decorated delegate with the specified object.
+ */
+ public function __setObject($object)
+ {
+ $this->__instance = $object;
+ }
+
+ /**
+ * Forward method calls.
+ *
+ * @param String $method method name
+ * @param Array $args method arguments
+ * @return Unknown method return value
+ */
+ public function __call($method, $args)
+ {
+ $callback = array($this->__instance, $method);
+
+ if (is_callable($callback))
+ {
+ return call_user_func_array($callback, $args);
+ }
+ else
+ {
+ throw new \BadMethodCallException(sprintf(
+ '%s::%s() is not callable',
+ get_class($this->__instance),
+ $method
+ ));
+ }
+ }
+
+ /**
+ * Forward property set.
+ *
+ * @param String $name property name
+ * @param Unknown $value property value
+ */
+ public function __set($name, $value)
+ {
+ $this->__instance->$name = $value;
+ }
+
+ /**
+ * Forward property get.
+ *
+ * @param String $name, property name
+ * @return Unknown
+ */
+ public function __get($name)
+ {
+ return $this->__instance->$name;
+ }
+}
@@ -56,5 +56,4 @@ public function getBody()
{
return $this->_body;
}
-
}
@@ -11,11 +11,14 @@ class ControllerDirectory implements ControllerResolver
private $_callback;
/**
- * @param DirectoryIterator
- * @param callback takes a file and class and returns a controller
+ * @param mixed either a directory path, or an iterator
+ * @param callback returns an instance of a controller, given file and controller name
*/
public function __construct($directoryIterator, $callback=null)
{
+ if(is_string($directoryIterator))
+ $directoryIterator = new \DirectoryIterator($directoryIterator);
+
$this->_iterator = $directoryIterator;
$this->_callback = $callback ?: function($file, $className) {
require_once($file);
@@ -45,7 +45,7 @@ public function addResponseFilter($filter)
* Apply a chain of filters to an object
* @return object
*/
- private function _filter($chain, $object)
+ private function _applyFilter($chain, $object)
{
foreach($chain as $filter)
{
@@ -56,14 +56,23 @@ private function _filter($chain, $object)
return $object;
}
+ /**
+ * Filter a request and response and return the response
+ * @return response
+ */
+ protected function filter($request)
+ {
+ return $this->_applyFilter($this->_responses,
+ $this->_controller->execute(
+ $this->_applyFilter($this->_requests, $request)
+ ));
+ }
+
/* (non-phpdoc)
* @see Controller::execute()
*/
public function execute($request)
{
- return $this->_filter($this->_responses,
- $this->_controller->execute(
- $this->_filter($this->_requests, $request)
- ));
+ return $this->filter($request);
}
}
@@ -42,7 +42,7 @@ public function __construct($name, $template)
/**
* @return RouteMatch or null if no match.
*/
- public function getMatch($path)
+ public function getMatch($path, $metadata=null)
{
if (preg_match($this->_pattern, $path, $matches))
{
@@ -53,11 +53,11 @@ public function getMatch($path)
? array()
: array_combine($this->_parameters, $matches);
- return new RouteMatch($this->_name, $parameters);
+ return new RouteMatch($this->_name, $parameters, $metadata);
}
else if(strlen($path) > 1 && substr($path,-1) == '/')
{
- return $this->getMatch(rtrim($path,'/'));
+ return $this->getMatch(rtrim($path,'/'), $metadata);
}
return null;
@@ -5,21 +5,22 @@
/**
* A match result from a lookup against a {@link Router}
*/
-class RouteMatch extends \ArrayIterator
+class RouteMatch implements \IteratorAggregate
{
private $_name;
private $_parameters;
+ private $_metadata;
/**
* @param string $name
* @param array $parameters
* @params array $tags
*/
- public function __construct($name, $parameters)
+ public function __construct($name, $parameters, $metadata=array())
{
$this->_name = $name;
$this->_parameters = $parameters;
- parent::__construct($parameters);
+ $this->_metadata = $metadata;
}
/**
@@ -38,11 +39,35 @@ public function getParameters()
return $this->_parameters;
}
+ /**
+ * @return array
+ */
+ public function getMetadata()
+ {
+ return $this->_metadata;
+ }
+
/**
* @return string
*/
public function parameter($key,$default=false)
{
return isset($this[$key]) ? $this[$key] : $default;
}
+
+ /*
+ * @see IteratorAggregate
+ */
+ public function getIterator()
+ {
+ return new \ArrayIterator($this->_parameters);
+ }
+
+ /**
+ * Magic getter method
+ */
+ public function __get($key)
+ {
+ return $this->_parameters[$key];
+ }
}
Oops, something went wrong.

0 comments on commit f28fd17

Please sign in to comment.