3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Service
;
12 use Zend\Console\Console
;
13 use Zend\Mvc\Router\Console\SimpleRouteStack
as ConsoleRouter
;
14 use Zend\Mvc\Router\Http\TreeRouteStack
as HttpRouter
;
15 use Zend\ServiceManager\FactoryInterface
;
16 use Zend\ServiceManager\ServiceLocatorInterface
;
18 class RouterFactory
implements FactoryInterface
21 * Create and return the router
23 * Retrieves the "router" key of the Config service, and uses it
24 * to instantiate the router. Uses the TreeRouteStack implementation by
27 * @param ServiceLocatorInterface $serviceLocator
28 * @param string|null $cName
29 * @param string|null $rName
30 * @return \Zend\Mvc\Router\RouteStackInterface
32 public function createService(ServiceLocatorInterface
$serviceLocator, $cName = null, $rName = null)
34 $config = $serviceLocator->has('Config') ?
$serviceLocator->get('Config') : array();
37 $routerClass = 'Zend\Mvc\Router\Http\TreeRouteStack';
38 $routerConfig = isset($config['router']) ?
$config['router'] : array();
40 // Console environment?
41 if ($rName === 'ConsoleRouter' // force console router
42 ||
($cName === 'router' && Console
::isConsole()) // auto detect console
44 // We are in a console, use console router defaults.
45 $routerClass = 'Zend\Mvc\Router\Console\SimpleRouteStack';
46 $routerConfig = isset($config['console']['router']) ?
$config['console']['router'] : array();
49 // Obtain the configured router class, if any
50 if (isset($routerConfig['router_class']) && class_exists($routerConfig['router_class'])) {
51 $routerClass = $routerConfig['router_class'];
54 // Inject the route plugins
55 if (!isset($routerConfig['route_plugins'])) {
56 $routePluginManager = $serviceLocator->get('RoutePluginManager');
57 $routerConfig['route_plugins'] = $routePluginManager;
61 $factory = sprintf('%s::factory', $routerClass);
62 return call_user_func($factory, $routerConfig);