Skip to content

Commit

Permalink
Add the ability to customise the names of *action* methods via an
Browse files Browse the repository at this point in the history
*action map*.
  • Loading branch information
trq committed Nov 12, 2012
1 parent b574d0a commit 7a37ab5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
5 changes: 1 addition & 4 deletions lib/Proem/Controller/Standard.php
Expand Up @@ -71,16 +71,13 @@ public function init()
*/ */
public function dispatch($action) public function dispatch($action)
{ {
$action = strtolower($action);

if ($this->assets->provides('events', '\Proem\Signal\Manager\Template')) { if ($this->assets->provides('events', '\Proem\Signal\Manager\Template')) {
$this->assets->get('events')->trigger( $this->assets->get('events')->trigger(
(new Bootstrap('proem.pre.action.' . $action))->setServiceManager($this->assets) (new Bootstrap('proem.pre.action.' . $action))->setServiceManager($this->assets)
); );
} }


$method = $action . 'Action'; $result = $this->{$action}();
$result = $this->{$method}();


if ($this->assets->provides('events', '\Proem\Signal\Manager\Template')) { if ($this->assets->provides('events', '\Proem\Signal\Manager\Template')) {
$this->assets->get('events')->trigger( $this->assets->get('events')->trigger(
Expand Down
6 changes: 5 additions & 1 deletion lib/Proem/Dispatch/Stage.php
Expand Up @@ -171,9 +171,13 @@ public function dispatch($e)
public function testRoute($event) public function testRoute($event)
{ {
if ($this->assets->has('dispatch')) { if ($this->assets->has('dispatch')) {
$this->assets->get('dispatch')->setPayload($event->getPayload());
$this->assets->get('dispatch')->setModule($event->getPayload()->get('module', ''));
$this->assets->get('dispatch')->setController($event->getPayload()->get('controller', ''));
$this->assets->get('dispatch')->setAction($event->getPayload()->get('action', ''));
return $event->setParam( return $event->setParam(
'isDispatchable', 'isDispatchable',
$this->assets->get('dispatch')->setPayload($event->getPayload())->isDispatchable() $this->assets->get('dispatch')->isDispatchable()
); );
} }
} }
Expand Down
95 changes: 88 additions & 7 deletions lib/Proem/Dispatch/Standard.php
Expand Up @@ -40,10 +40,15 @@
class Standard implements Template class Standard implements Template
{ {
/** /**
* Constants used for priorities * Priorities
*/ */
const DEFAULT_CONTROLLERMAP_PRIORITY = 0; const DEFAULT_CONTROLLERMAP_PRIORITY = 0;


/**
* Action placeholder
*/
const ACTION_PLACEHOLDER = '{:action}';

/** /**
* Store the Assets manager * Store the Assets manager
* *
Expand All @@ -64,6 +69,11 @@ class Standard implements Template
*/ */
protected $controllerMaps; protected $controllerMaps;


/**
* Store the *action map* pattern.
*/
protected $actionMap;

/** /**
* Store the absolute namespace to the current class * Store the absolute namespace to the current class
* *
Expand Down Expand Up @@ -112,6 +122,7 @@ public function __construct(Manager $assets)
'Module\:module\Controller\:controller', 'Module\:module\Controller\:controller',
self::DEFAULT_CONTROLLERMAP_PRIORITY self::DEFAULT_CONTROLLERMAP_PRIORITY
); );
$this->actionMap = self::ACTION_PLACEHOLDER . 'Action';
} }


/** /**
Expand All @@ -134,6 +145,63 @@ public function setPayload(Payload $payload)
return $this; return $this;
} }


/**
* Set the current module
*
* @param string $module
*/
public function setModule($module)
{
$this->module = $module;
return $this;
}

/**
* Get the current module
*/
public function getModule()
{
return $this->module;
}

/**
* Set the current controller
*
* @param string $controller
*/
public function setController($controller)
{
$this->controller = $controller;
return $this;
}

/**
* Get the current controller
*/
public function getController()
{
return $this->controller;
}

/**
* Set the current action
*
* @param string $action
*/
public function setAction($action)
{
$this->action = $action;
return $this;
}

/**
* Get the current action
*/
public function getAction()
{
return str_replace(self::ACTION_PLACEHOLDER, strtolower($this->action), $this->actionMap);
}

/** /**
* Add a new controller map onto the stack of controller * Add a new controller map onto the stack of controller
* maps. * maps.
Expand All @@ -151,6 +219,7 @@ public function setPayload(Payload $payload)
* with the module and controller that are made available via the payload. * with the module and controller that are made available via the payload.
* *
* @param string $map * @param string $map
* @param int $priority
* @return Proem\Dispatch\Template * @return Proem\Dispatch\Template
*/ */
public function attachControllerMap($map, $priority = self::DEFAULT_CONTROLLERMAP_PRIORITY) public function attachControllerMap($map, $priority = self::DEFAULT_CONTROLLERMAP_PRIORITY)
Expand All @@ -159,6 +228,22 @@ public function attachControllerMap($map, $priority = self::DEFAULT_CONTROLLERMA
return $this; return $this;
} }


/**
* Allows the customisation of the *action map*.
*
* The default implementation looks like {:action}Action
*
* {:action} gets replaced by the action returning from the
* router.
*
* @param string $mapping
*/
public function setActionMap($mapping)
{
$this->actionMap = $mapping;
return $this;
}

/** /**
* Test to see if the current payload is dispatchable. * Test to see if the current payload is dispatchable.
* *
Expand All @@ -173,10 +258,6 @@ public function attachControllerMap($map, $priority = self::DEFAULT_CONTROLLERMA
*/ */
public function isDispatchable() public function isDispatchable()
{ {
$this->module = $this->payload->has('module') ? $this->prepare($this->payload->get('module')) : '';
$this->controller = $this->payload->has('controller') ? $this->prepare($this->payload->get('controller')) : '';
$this->action = $this->payload->has('action') ? $this->payload->get('action') : '';

foreach (array_reverse($this->controllerMaps) as $map) { foreach (array_reverse($this->controllerMaps) as $map) {
$this->class = str_replace( $this->class = str_replace(
[':module', ':controller'], [':module', ':controller'],
Expand All @@ -187,7 +268,7 @@ public function isDispatchable()
try { try {
$class = new \ReflectionClass($this->class); $class = new \ReflectionClass($this->class);
if ($class->implementsInterface('\Proem\Controller\Template')) { if ($class->implementsInterface('\Proem\Controller\Template')) {
$method = $class->getMethod($this->action . 'Action'); $method = $class->getMethod($this->getAction());
if ($method->isPublic()) { if ($method->isPublic()) {
return true; return true;
} }
Expand Down Expand Up @@ -220,7 +301,7 @@ public function dispatch()
} }


$this->class = new $this->class($this->assets); $this->class = new $this->class($this->assets);
$this->class->dispatch($this->action); $this->class->dispatch($this->getAction());
return $this; return $this;
} }
} }

0 comments on commit 7a37ab5

Please sign in to comment.