Skip to content

Commit

Permalink
allow foreing imports to be grouped correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
iwyg committed Mar 1, 2016
1 parent 72d826d commit ab75ebb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@

namespace Lucid\Mux\Loader;

use Lucid\Resource\Loader\LoaderInterface as BaseLoaderInterface;

/**
* @interface LoaderInterface
*
* @package Lucid\Mux\Loader
* @version $Id$
* @author iwyg <mail@thomas-appel.com>
*/
interface LoaderInterface
interface LoaderInterface extends BaseLoaderInterface
{
/**
* Load routes
*
* @param mixed $routes
*
* @return Lucid\Mux\RouteContextInterface
*/
public function loadRoutes($routes);
}
27 changes: 22 additions & 5 deletions src/Loader/PhpLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Lucid\Mux\Loader;

use Lucid\Mux\Routes;
use Lucid\Resource\Locator;
use Lucid\Resource\LocatorInterface;
use Lucid\Mux\RouteCollectionBuilder;
use Lucid\Mux\RouteCollectionInterface;
Expand All @@ -34,10 +35,10 @@ class PhpLoader extends AbstractFileLoader implements LoaderInterface
* @param LocatorInterface $locator
* @param RouteCollectionInterface $routes
*/
public function __construct(LocatorInterface $locator, RouteCollectionInterface $routes = null)
public function __construct(RouteCollectionBuilder $builder = null, LocatorInterface $locator = null)
{
parent::__construct($locator);
$this->builder = new RouteCollectionBuilder($routes ?: new Routes);
parent::__construct($locator ?: new Locator);
$this->builder = $builder ?: new RouteCollectionBuilder(new Routes);
}

/**
Expand All @@ -64,7 +65,7 @@ protected function getExtensions()
protected function doLoad($file)
{
if (!is_array($routes = include $file)) {
throw new LoaderException('Return value must be array.');
throw new LoaderException('Return value must be an array.');
}

$this->addRoutes($routes);
Expand All @@ -82,11 +83,20 @@ protected function doLoad($file)
private function addRoutes(array $routes)
{
foreach ($routes as $name => $route) {
if (isset($route['resources'])) {
$this->loadImports(is_array($route['resources']) ? $route['resources'] :
(is_string($route['resources']) ? [$route['resources']] : []));
continue;
}

if ((bool)$gkeys = $this->getGroupKeys($route)) {

$req = isset($route['requirements']) ? $route['requirements'] : [];

if (null === $prefix = isset($route['pattern']) ? $route['pattern'] : $name) {
continue;
}

foreach ($gkeys as $i) {
$this->loadGroup($prefix, $req, $route[$i]);
}
Expand Down Expand Up @@ -151,8 +161,15 @@ private function addRoute($name, array $route)
*
* @return void
*/
private function loadImports(array $routes)
private function loadImports(array $resources)
{
foreach ($resources as $resource) {
if (!is_string($resource)) {
continue;
}

$this->import($resource);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Loader/Fixures/imports.0.routes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

<?php

return [
'foo' => [
'pattern' => '/foo',
'method' => 'GET',
'handler' => 'fooAction'
],
'bar' => [
'pattern' => '/bar',
'method' => 'GET',
'handler' => 'barAction'
],
];
16 changes: 16 additions & 0 deletions tests/Loader/Fixures/route_imports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Lucid\Mux\RouteCollectionBuilder;

return [
'index' => [
'pattern' => '/',
'method' => 'GET',
'handler' => 'indexAction'
],
'admin' => [
[
['resources' => ['imports.0.routes']]
]
]
];
42 changes: 34 additions & 8 deletions tests/Loader/PhpLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,55 @@
use Lucid\Mux\Routes;
use Lucid\Resource\Collection;
use Lucid\Mux\Loader\PhpLoader;
use Lucid\Resource\Loader\Resolver;
use Lucid\Mux\RouteCollectionBuilder;

class PhpLoaderTest extends \PHPUnit_Framework_TestCase
{
/** @test */
public function itShouldReturnRoutes()
{
$loader = new PhpLoader($this->mockLocator());
$loader = new PhpLoader(null, $this->mockLocator());

$this->assertInstanceOf('Lucid\Mux\RouteCollectionInterface', $routes = $loader->loadRoutes('routes.php'));
}

/** @test */
public function itShouldReturnInputCollection()
{
$loader = new PhpLoader($this->mockLocator(), $routes = new Routes);
$loader = new PhpLoader(new RouteCollectionBuilder($routes = new Routes), $this->mockLocator());

$this->assertSame($routes, $loader->loadRoutes('routes.php'));
}

/** @test */
public function itShouldImportRoutes()
{
$loader = new PhpLoader($builder = new RouteCollectionBuilder, $loc = $this->mockLocator());

$mockedLoader = $this->getMockbuilder('Lucid\Mux\Loader\PhpLoader')
->setMethods(['getExtensions'])
->setConstructorArgs([$builder, $loc])->getMock();
$mockedLoader->method('getExtensions')->willReturn(['routes']);

$resolver = new Resolver([$loader, $mockedLoader]);

$routes = $loader->loadRoutes('route_imports.php');


$all = $routes->all();

$this->assertArrayHasKey('foo', $all);
$this->assertArrayHasKey('bar', $all);

$this->assertSame('/admin/foo', $routes->get('foo')->getPattern());
$this->assertSame('/admin/bar', $routes->get('bar')->getPattern());
}

/** @test */
public function itShouldLoadRoutes()
{
$loader = new PhpLoader($this->mockLocator());
$loader = new PhpLoader(null, $this->mockLocator());
$routes = $loader->loadRoutes('routes.0.php');

$this->assertTrue($routes->has('index'));
Expand All @@ -36,7 +62,7 @@ public function itShouldLoadRoutes()
/** @test */
public function itShouldLoadGroups()
{
$loader = new PhpLoader($this->mockLocator());
$loader = new PhpLoader(null, $this->mockLocator());
$routes = $loader->loadRoutes('route_groups.0.php');

$this->assertTrue($routes->has('users'));
Expand All @@ -48,13 +74,13 @@ public function itShouldLoadGroups()
$this->assertSame('example.com', $routes->get('affiliates')->getHost());
$this->assertSame(['https'], $routes->get('users')->getSchemes());

$loader = new PhpLoader($this->mockLocator());
$loader = new PhpLoader(null, $this->mockLocator());
$routes = $loader->loadRoutes('route_groups.1.php');

$this->assertNull($routes->get('users')->getHost());
$this->assertSame(['http', 'https'], $routes->get('users')->getSchemes());

$loader = new PhpLoader($this->mockLocator());
$loader = new PhpLoader(null, $this->mockLocator());
$routes = $loader->loadRoutes('route_groups.2.php');

$this->assertNull($routes->get('users')->getHost());
Expand All @@ -64,12 +90,12 @@ public function itShouldLoadGroups()
/** @test */
public function itShouldThrowOnInvalidConfig()
{
$loader = new PhpLoader($this->mockLocator());
$loader = new PhpLoader(null, $this->mockLocator());

try {
$loader->loadRoutes('faulty.php');
} catch (\Lucid\Resource\Exception\LoaderException $e) {
$this->assertEquals('Return value must be array.', $e->getMessage());
$this->assertEquals('Return value must be an array.', $e->getMessage());
return;
}

Expand Down

0 comments on commit ab75ebb

Please sign in to comment.