forked from zendframework/zendframework
/
RouterFactory.php
69 lines (60 loc) · 2.46 KB
/
RouterFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc\Service;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use Zend\Mvc\Router\Console\SimpleRouteStack as ConsoleRouter;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Console\Console;
class RouterFactory implements FactoryInterface
{
/**
* Create and return the router
*
* Retrieves the "router" key of the Config service, and uses it
* to instantiate the router. Uses the TreeRouteStack implementation by
* default.
*
* @param ServiceLocatorInterface $serviceLocator
* @param string|null $cName
* @param string|null $rName
* @return \Zend\Mvc\Router\RouteStackInterface
*/
public function createService(ServiceLocatorInterface $serviceLocator, $cName = null, $rName = null)
{
$config = $serviceLocator->get('Config');
$routePluginManager = $serviceLocator->get('RoutePluginManager');
if (
$rName === 'ConsoleRouter' || // force console router
($cName === 'router' && Console::isConsole()) // auto detect console
) {
// We are in a console, use console router.
if (isset($config['console']) && isset($config['console']['router'])) {
$routerConfig = $config['console']['router'];
} else {
$routerConfig = array();
}
$router = new ConsoleRouter($routePluginManager);
} else {
// This is an HTTP request, so use HTTP router
$router = new HttpRouter($routePluginManager);
$routerConfig = isset($config['router']) ? $config['router'] : array();
}
if (isset($routerConfig['route_plugins'])) {
$router->setRoutePluginManager($routerConfig['route_plugins']);
}
if (isset($routerConfig['routes'])) {
$router->addRoutes($routerConfig['routes']);
}
if (isset($routerConfig['default_params'])) {
$router->setDefaultParams($routerConfig['default_params']);
}
return $router;
}
}