use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / ModuleRouteListener.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Mvc;
11
12 use Zend\EventManager\AbstractListenerAggregate;
13 use Zend\EventManager\EventManagerInterface;
14
15 class ModuleRouteListener extends AbstractListenerAggregate
16 {
17 const MODULE_NAMESPACE = '__NAMESPACE__';
18 const ORIGINAL_CONTROLLER = '__CONTROLLER__';
19
20 /**
21 * Attach to an event manager
22 *
23 * @param EventManagerInterface $events
24 * @param int $priority
25 */
26 public function attach(EventManagerInterface $events, $priority = 1)
27 {
28 $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'onRoute'], $priority);
29 }
30
31 /**
32 * Listen to the "route" event and determine if the module namespace should
33 * be prepended to the controller name.
34 *
35 * If the route match contains a parameter key matching the MODULE_NAMESPACE
36 * constant, that value will be prepended, with a namespace separator, to
37 * the matched controller parameter.
38 *
39 * @param MvcEvent $e
40 * @return null
41 */
42 public function onRoute(MvcEvent $e)
43 {
44 $matches = $e->getRouteMatch();
45 if (!$matches instanceof Router\RouteMatch) {
46 // Can't do anything without a route match
47 return;
48 }
49
50 $module = $matches->getParam(self::MODULE_NAMESPACE, false);
51 if (!$module) {
52 // No module namespace found; nothing to do
53 return;
54 }
55
56 $controller = $matches->getParam('controller', false);
57 if (!$controller) {
58 // no controller matched, nothing to do
59 return;
60 }
61
62 // Ensure the module namespace has not already been applied
63 if (0 === strpos($controller, $module)) {
64 return;
65 }
66
67 // Keep the originally matched controller name around
68 $matches->setParam(self::ORIGINAL_CONTROLLER, $controller);
69
70 // Prepend the controllername with the module, and replace it in the
71 // matches
72 $controller = $module . '\\' . str_replace(' ', '', ucwords(str_replace('-', ' ', $controller)));
73 $matches->setParam('controller', $controller);
74 }
75 }