Skip to content

Instantly share code, notes, and snippets.

@aek
Created October 18, 2010 16:42
Show Gist options
  • Save aek/632545 to your computer and use it in GitHub Desktop.
Save aek/632545 to your computer and use it in GitHub Desktop.
halo_MultiActionController
<?php
class halo_AntPathMatcher implements halo_IPathMatcher {
public function match($pattern, $path){
/**
** PurSelector is an Utility class from http://www.php-pop.org
**/
return PurSelector::match($pattern, $path, true);
}
}
?>
<?php
interface halo_IMethodNameResolver {
public function getHandlerMethodName(halo_HttpRequest $request);
}
?>
<?php
class halo_InternalPathMethodNameResolver implements halo_IMethodNameResolver {
private $prefix = "";
private $suffix = "";
public function setPrefix($prefix) {
$this->prefix = ($prefix !== null ? $prefix : "");
}
protected function getPrefix() {
return $this->prefix;
}
public function setSuffix($suffix) {
$this->suffix = ($suffix !== null ? $suffix : "");
}
protected function getSuffix() {
return $this->suffix;
}
public function getHandlerMethodName(halo_HttpRequest $request){
$urlPath = $request->getRequestedUrl();
$methodName = basename($urlPath, ".php");
$methodName = $this->prefix. $methodName. $this->suffix;
return $methodName;
}
}
?>
<?php
interface halo_IPathMatcher {
public function match($pattern, $path);
}
?>
<?php
class halo_MultiActionController implements halo_IController {
private $methodNameResolver;
private $delegate;
public function __construct() {
$this->methodNameResolver = new halo_InternalPathMethodNameResolver();
$this->delegate = $this;
}
public function handleRequest(halo_HttpRequest $request, halo_HttpResponse $response){
$methodName = $this->methodNameResolver->getHandlerMethodName($request);
return $this->invokeNamedMethod($methodName, $request, $response);
}
public function setDelegate($delegate) {
$this->delegate = $delegate;
}
public function setMethodNameResolver(halo_IMethodNameResolver $methodNameResolver) {
$this->methodNameResolver = $methodNameResolver;
}
public function getMethodNameResolver() {
return $this->methodNameResolver;
}
public function bind(halo_HttpRequest $request, $object){
$reflectionClass = new ReflectionClass(get_class($object));
foreach ($request->getParameterNames() as $param) {
$props = explode(".",$param);
$value = $object;
for ($index = 0; $index <= count($props)-2; $index++) {
$prop = 'get'.ucfirst($props[$index]);
if(method_exists($value , $prop)){
$value = $value->$prop();
} else {
break;
}
}
$prop = 'set'.ucfirst($props[count($props)-1]);
if(method_exists($value , $prop)){
$value->$prop($request->getParameter($param));
$prop = 'get'.ucfirst($props[count($props)-1]);
}
}
}
protected function invokeNamedMethod($methodName, halo_HttpRequest $request, halo_HttpResponse $response){
$method = new ReflectionMethod(get_class($this->delegate), $methodName);
$parameters = $method->getParameters();
$params = Array();
$params[] = $request;
$params[] = $response;
if ($method->getNumberOfParameters() >= 3) {
$commandReflectionClass = new ReflectionClass($parameters[2]->getClass());
$class = $parameters[2]->getClass()->getName();
$command = new $class;
$this->bind($request, $command);
$params[] = $command;
}
$returnValue = $method->invokeArgs($this->delegate, $params);
return $returnValue;
}
}
?>
<?php
class halo_ParameterMethodNameResolver implements halo_IMethodNameResolver {
private $paramName = 'action';
private $methodParamNames;
public function setParamName($paramName) {
$this->paramName = $paramName;
}
public function setMethodParamNames(Array $methodParamNames) {
$this->methodParamNames = $methodParamNames;
}
public function getHandlerMethodName(halo_HttpRequest $request){
$methodName = '';
if ($this->methodParamNames !== null) {
foreach ($this->methodParamNames as $variable) {
if($request->getParameter($variable) !== null){
$methodName = $request->getParameter($variable);
break;
}
}
}
if ($methodName === null && $this->paramName !== null) {
$methodName = $request->getParameter($this->paramName);
}
if ($methodName !== null && $methodName === "") {
$methodName = null;
}
if ($methodName === null) {
if ($this->defaultMethodName !== null) {
$methodName = $this->defaultMethodName;
}
else {
throw new Exception('Handler method, not found');
}
}
return $methodName;
}
}
?>
<?php
class halo_PropertiesMethodNameResolver implements halo_IMethodNameResolver {
private $mappings;
private $pathMatcher;
public function __construct() {
$this->pathMatcher = new halo_AntPathMatcher();
}
public function setMappings(Array $mappings) {
$this->mappings = $mappings;
}
/**
* Set the PathMatcher implementation to use for matching URL paths
* against registered URL patterns. Default is halo_AntPathMatcher.
* @see halo_AntPathMatcher
*/
public function setPathMatcher(halo_IPathMatcher $pathMatcher) {
$this->pathMatcher = $pathMatcher;
}
public function getHandlerMethodName(halo_HttpRequest $request){
$urlPath = $request->getRequestedUrl();
if(array_key_exists($urlPath, $this->mappings)){
return $this->mappings[$urlPath];
}
foreach ($this->mappings as $key => $value) {
if ($this->pathMatcher->match($key, $urlPath)) {
return $value;
}
}
return null;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment