Skip to content

Commit

Permalink
Merge branch 'hotfix/4630' into develop
Browse files Browse the repository at this point in the history
Forward port zendframework#4630
  • Loading branch information
Mike Willbanks committed Jun 12, 2013
2 parents f3a10da + b873ba7 commit e911436
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 12 deletions.
27 changes: 15 additions & 12 deletions library/Zend/Mvc/Service/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ class RouterFactory implements FactoryInterface
*/
public function createService(ServiceLocatorInterface $serviceLocator, $cName = null, $rName = null)
{
$config = $serviceLocator->get('Config');
$routerConfig = array();
$routePluginManager = $serviceLocator->get('RoutePluginManager');
$config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : array();

// Defaults
$routerClass = 'Zend\Mvc\Router\Http\TreeRouteStack';
$routerConfig = isset($config['router']) ? $config['router'] : array();

// Console environment?
if ($rName === 'ConsoleRouter' // force console router
|| ($cName === 'router' && Console::isConsole()) // auto detect console
) {
Expand All @@ -43,22 +46,22 @@ public function createService(ServiceLocatorInterface $serviceLocator, $cName =
$routerConfig = $config['console']['router'];
}

if (!isset($routerConfig['route_plugins'])) {
$routerConfig['route_plugins'] = $routePluginManager;
}

return ConsoleRouter::factory($routerConfig);
$routerClass = 'Zend\Mvc\Router\Console\SimpleRouteStack';
}

// This is an HTTP request, so use HTTP router
if (isset($config['router'])) {
$routerConfig = $config['router'];
// Obtain the configured router class, if any
if (isset($routerConfig['router_class']) && class_exists($routerConfig['router_class'])) {
$routerClass = $routerConfig['router_class'];
}

// Inject the route plugins
if (!isset($routerConfig['route_plugins'])) {
$routePluginManager = $serviceLocator->get('RoutePluginManager');
$routerConfig['route_plugins'] = $routePluginManager;
}

return HttpRouter::factory($routerConfig);
// Obtain an instance
$factory = sprintf('%s::factory', $routerClass);
return call_user_func($factory, $routerConfig);
}
}
44 changes: 44 additions & 0 deletions tests/ZendTest/Mvc/Service/RouterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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
* @package Zend_Mvc
*/

namespace ZendTest\Mvc\Service;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\EventManager;
use Zend\Mvc\Router\RoutePluginManager;
use Zend\Mvc\Service\RouterFactory;
use Zend\ServiceManager\ServiceManager;

class RouterFactoryTest extends TestCase
{
public function setUp()
{
$this->services = new ServiceManager();
$this->services->setService('RoutePluginManager', new RoutePluginManager());
$this->factory = new RouterFactory();
}

public function testFactoryCanCreateRouterBasedOnConfiguredName()
{
$this->services->setService('Config', array(
'router' => array(
'router_class' => 'ZendTest\Mvc\Service\TestAsset\Router',
),
'console' => array(
'router' => array(
'router_class' => 'ZendTest\Mvc\Service\TestAsset\Router',
),
),
));

$router = $this->factory->createService($this->services, 'router', 'Router');
$this->assertInstanceOf('ZendTest\Mvc\Service\TestAsset\Router', $router);
}
}
90 changes: 90 additions & 0 deletions tests/ZendTest/Mvc/Service/TestAsset/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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 ZendTest\Mvc\Service\TestAsset;

use Zend\Mvc\Router\RouteStackInterface;
use Zend\Stdlib\RequestInterface as Request;

class Router implements RouteStackInterface
{
/**
* Create a new route with given options.
*
* @param array|\Traversable $options
* @return void
*/
public static function factory($options = array())
{
return new static();
}

/**
* Match a given request.
*
* @param Request $request
* @return RouteMatch|null
*/
public function match(Request $request)
{
}

/**
* Assemble the route.
*
* @param array $params
* @param array $options
* @return mixed
*/
public function assemble(array $params = array(), array $options = array())
{
}

/**
* Add a route to the stack.
*
* @param string $name
* @param mixed $route
* @param int $priority
* @return RouteStackInterface
*/
public function addRoute($name, $route, $priority = null)
{
}

/**
* Add multiple routes to the stack.
*
* @param array|\Traversable $routes
* @return RouteStackInterface
*/
public function addRoutes($routes)
{
}

/**
* Remove a route from the stack.
*
* @param string $name
* @return RouteStackInterface
*/
public function removeRoute($name)
{
}

/**
* Remove all routes from the stack and set new ones.
*
* @param array|\Traversable $routes
* @return RouteStackInterface
*/
public function setRoutes($routes)
{
}
}

0 comments on commit e911436

Please sign in to comment.