Skip to content

Commit

Permalink
Merge pull request #52 from sunrise-php/release/v2.2.0
Browse files Browse the repository at this point in the history
Load routes from a folder for the CollectableFileLoader
  • Loading branch information
fenric committed Feb 9, 2020
2 parents cb82e87 + aad7aec commit 65a909e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
## Installation

```bash
composer require 'sunrise/http-router:^2.1'
composer require 'sunrise/http-router:^2.2'
```

## QuickStart
Expand Down Expand Up @@ -66,6 +66,10 @@ $loader->attach('routes/api.php');
$loader->attach('routes/admin.php');
$loader->attach('routes/public.php');

// or attach a directory...
// [!] available from version 2.2
$loader->attach('routes');

$router = new Router();
$router->load($loader);

Expand Down
12 changes: 12 additions & 0 deletions src/Loader/CollectableFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
/**
* Import functions
*/
use function glob;
use function is_dir;
use function is_file;
use function sprintf;

Expand Down Expand Up @@ -68,6 +70,16 @@ public function __construct(
*/
public function attach($resource) : void
{
if (is_dir($resource)) {
$resources = glob($resource . '/*.php');

foreach ($resources as $resource) {
$this->resources[] = $resource;
}

return;
}

if (!is_file($resource)) {
throw new InvalidLoaderResourceException(
sprintf('The resource "%s" is not found.', $resource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

use Sunrise\Http\Router\Tests\Fixture\BlankRequestHandler;

$this->get('home', '/', new BlankRequestHandler());
$this->get('bar', '/bar', new BlankRequestHandler());
5 changes: 5 additions & 0 deletions tests/Fixture/routes/foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php declare(strict_types=1);

use Sunrise\Http\Router\Tests\Fixture\BlankRequestHandler;

$this->get('foo', '/foo', new BlankRequestHandler());
16 changes: 14 additions & 2 deletions tests/Loader/CollectableFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@ public function testAttachInvalidResource() : void
/**
* @return void
*/
public function testLoad() : void
public function testLoadFile() : void
{
$loader = new CollectableFileLoader();

$loader->attach(__DIR__ . '/../Fixture/collectable-routes.php');
$loader->attach(__DIR__ . '/../Fixture/routes/foo.php');

$this->assertCount(1, $loader->load()->all());
}

/**
* @return void
*/
public function testLoadDirectory() : void
{
$loader = new CollectableFileLoader();

$loader->attach(__DIR__ . '/../Fixture/routes');

$this->assertCount(2, $loader->load()->all());
}
}

0 comments on commit 65a909e

Please sign in to comment.