Permalink
Browse files

Merge branch 'feature/action-map-implementation' into develop

  • Loading branch information...
2 parents b574d0a + 7a37ab5 commit faaef76d0d49cf4c41768b10f575e9160feeb2e3 @trq trq committed Nov 12, 2012
Showing with 94 additions and 12 deletions.
  1. +1 −4 lib/Proem/Controller/Standard.php
  2. +5 −1 lib/Proem/Dispatch/Stage.php
  3. +88 −7 lib/Proem/Dispatch/Standard.php
@@ -71,16 +71,13 @@ public function init()
*/
public function dispatch($action)
{
- $action = strtolower($action);
-
if ($this->assets->provides('events', '\Proem\Signal\Manager\Template')) {
$this->assets->get('events')->trigger(
(new Bootstrap('proem.pre.action.' . $action))->setServiceManager($this->assets)
);
}
- $method = $action . 'Action';
- $result = $this->{$method}();
+ $result = $this->{$action}();
if ($this->assets->provides('events', '\Proem\Signal\Manager\Template')) {
$this->assets->get('events')->trigger(
@@ -171,9 +171,13 @@ public function dispatch($e)
public function testRoute($event)
{
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(
'isDispatchable',
- $this->assets->get('dispatch')->setPayload($event->getPayload())->isDispatchable()
+ $this->assets->get('dispatch')->isDispatchable()
);
}
}
@@ -40,10 +40,15 @@
class Standard implements Template
{
/**
- * Constants used for priorities
+ * Priorities
*/
const DEFAULT_CONTROLLERMAP_PRIORITY = 0;
+ /**
+ * Action placeholder
+ */
+ const ACTION_PLACEHOLDER = '{:action}';
+
/**
* Store the Assets manager
*
@@ -64,6 +69,11 @@ class Standard implements Template
*/
protected $controllerMaps;
+ /**
+ * Store the *action map* pattern.
+ */
+ protected $actionMap;
+
/**
* Store the absolute namespace to the current class
*
@@ -112,6 +122,7 @@ public function __construct(Manager $assets)
'Module\:module\Controller\:controller',
self::DEFAULT_CONTROLLERMAP_PRIORITY
);
+ $this->actionMap = self::ACTION_PLACEHOLDER . 'Action';
}
/**
@@ -134,6 +145,63 @@ public function setPayload(Payload $payload)
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
* maps.
@@ -151,6 +219,7 @@ public function setPayload(Payload $payload)
* with the module and controller that are made available via the payload.
*
* @param string $map
+ * @param int $priority
* @return Proem\Dispatch\Template
*/
public function attachControllerMap($map, $priority = self::DEFAULT_CONTROLLERMAP_PRIORITY)
@@ -159,6 +228,22 @@ public function attachControllerMap($map, $priority = self::DEFAULT_CONTROLLERMA
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.
*
@@ -173,10 +258,6 @@ public function attachControllerMap($map, $priority = self::DEFAULT_CONTROLLERMA
*/
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) {
$this->class = str_replace(
[':module', ':controller'],
@@ -187,7 +268,7 @@ public function isDispatchable()
try {
$class = new \ReflectionClass($this->class);
if ($class->implementsInterface('\Proem\Controller\Template')) {
- $method = $class->getMethod($this->action . 'Action');
+ $method = $class->getMethod($this->getAction());
if ($method->isPublic()) {
return true;
}
@@ -220,7 +301,7 @@ public function dispatch()
}
$this->class = new $this->class($this->assets);
- $this->class->dispatch($this->action);
+ $this->class->dispatch($this->getAction());
return $this;
}
}

0 comments on commit faaef76

Please sign in to comment.