Skip to content

Commit 171bd1a

Browse files
authored
Merge pull request #3 from kirschbaum-development/dev
0.1 release
2 parents 57fa5c4 + e759dfe commit 171bd1a

File tree

6 files changed

+109
-35
lines changed

6 files changed

+109
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
All notable changes to `laravel-route-file-macro` will be documented in this file.
44

5-
## 1.0.0 - 2019-04-23
5+
## 1.0.0 - 2019-07-25
66

77
- Initial release

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"keywords": [
66
"kirschbaum-development",
77
"laravel",
8+
"laravel-route",
89
"laravel-route-file-macro"
910
],
1011
"homepage": "https://github.com/kirschbaum-development/laravel-route-file-macro",

readme.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,29 @@ composer require kirschbaum-development/laravel-route-file-macro
1919

2020
## Usage
2121

22+
### Single Item
23+
24+
`Route::file` accepts a single file path or `SplFileInfo` object and cannot be used with multiple paths or file objects.
25+
2226
```php
2327
Route::file(base_path('routes/admin/users.php'));
28+
29+
$files = File::files(__DIR__.'/routes');
30+
Route::files($files[0]);
2431
```
2532

26-
### Array Usage
33+
### Multiple Items
34+
35+
`Route::files` accepts an array of file paths or an array of `SplFileInfo` objects and cannot be used with a single path or file object.
2736

2837
```php
29-
Route::file([
38+
Route::files([
3039
base_path('routes/admin/posts.php'),
3140
base_path('routes/admin/users.php')
3241
]);
33-
```
3442

35-
### `SplFileInfo` Usage
36-
37-
```php
3843
$files = File::files(__DIR__.'/routes');
39-
Route::file($files);
44+
Route::files($files);
4045
```
4146

4247
## Changelog

src/RouteFileMacroServiceProvider.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@
22

33
namespace KirschbaumDevelopment\RouteFileMacro;
44

5-
use SplFileInfo;
6-
75
use Illuminate\Support\Facades\Route;
86
use Illuminate\Support\ServiceProvider;
97

108
class RouteFileMacroServiceProvider extends ServiceProvider
119
{
10+
/**
11+
* Register the service provider.
12+
*
13+
* @return void
14+
*/
1215
public function register()
1316
{
14-
$this->registerRouteFileMacro();
15-
}
16-
17-
protected function registerRouteFileMacro()
18-
{
19-
Route::macro('file', function ($file) {
20-
collect(Arr::wrap($file))->each(function ($file) {
21-
$path = ($file instanceof SplFileInfo) ? $file->getRealPath() : $file;
22-
Route::group([], $path);
23-
});
24-
});
17+
Route::mixin(new RouteMacros);
2518
}
2619
}

src/RouteMacros.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\RouteFileMacro;
4+
5+
use SplFileInfo;
6+
use Illuminate\Support\Facades\Route;
7+
8+
class RouteMacros
9+
{
10+
/**
11+
* Load routes from a file.
12+
*
13+
* @return \Closure
14+
*/
15+
public function file()
16+
{
17+
return function ($file) {
18+
$this->generateRouteGroupForFile($file);
19+
};
20+
}
21+
22+
/**
23+
* Load routes from multiple files.
24+
*
25+
* @return \Closure
26+
*/
27+
public function files()
28+
{
29+
return function ($files) {
30+
collect($files)->each(function ($file) {
31+
$this->generateRouteGroupForFile($file);
32+
});
33+
};
34+
}
35+
36+
/**
37+
* Generate the route group for a the passed file.
38+
*
39+
* @return \Closure
40+
*/
41+
protected function generateRouteGroupForFile()
42+
{
43+
return function ($file) {
44+
$path = ($file instanceof SplFileInfo) ? $file->getRealPath() : $file;
45+
46+
Route::group([], $path);
47+
};
48+
}
49+
}

tests/RouteFileMacroTest.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,45 @@
55
use Orchestra\Testbench\TestCase;
66
use Illuminate\Support\Facades\File;
77
use Illuminate\Support\Facades\Route;
8+
use KirschbaumDevelopment\RouteFileMacro\RouteFileMacroServiceProvider;
89

910
class RouteFileMacroTest extends TestCase
1011
{
11-
protected function getPackageProviders($app)
12+
/** @test */
13+
public function hasTheRouteFileMacros()
1214
{
13-
return ['KirschbaumDevelopment\RouteFileMacro\RouteFileMacroServiceProvider'];
15+
$this->assertTrue(Route::hasMacro('file'));
16+
$this->assertTrue(Route::hasMacro('files'));
1417
}
1518

16-
/** @test */
17-
public function it_provides_route_file_macro()
19+
/** @test **/
20+
public function loadsRoutesFromSingleFilePath()
1821
{
19-
$this->assertTrue(Route::hasMacro('file'));
22+
Route::file(__DIR__ . '/routes/test-route-1.php');
23+
24+
$this->refreshNamedRoutes();
25+
26+
$this->assertTrue(Route::has('test-route-1'));
2027
}
2128

2229
/** @test **/
23-
public function it_loads_a_route_from_a_file_path()
30+
public function loadsRoutesFromSingleFileInfoObject()
2431
{
25-
Route::file(__DIR__.'/routes/test-route-1.php');
32+
$file = File::files(__DIR__ . '/routes');
33+
34+
Route::file($file[0]);
2635

2736
$this->refreshNamedRoutes();
2837

2938
$this->assertTrue(Route::has('test-route-1'));
3039
}
3140

3241
/** @test **/
33-
public function it_loads_multiple_routes_from_an_array_of_file_paths()
42+
public function loadsRoutesFromMultipleFilePaths()
3443
{
35-
Route::file([
36-
__DIR__.'/routes/test-route-1.php',
37-
__DIR__.'/routes/test-route-2.php',
44+
Route::files([
45+
__DIR__ . '/routes/test-route-1.php',
46+
__DIR__ . '/routes/test-route-2.php',
3847
]);
3948

4049
$this->refreshNamedRoutes();
@@ -44,18 +53,35 @@ public function it_loads_multiple_routes_from_an_array_of_file_paths()
4453
}
4554

4655
/** @test **/
47-
public function it_loads_route_files_from_a_file_object()
56+
public function loadsRoutesFromMultipleFileInfoObjects()
4857
{
49-
$files = File::files(__DIR__.'/routes');
58+
$files = File::files(__DIR__ . '/routes');
5059

51-
Route::file($files);
60+
Route::files($files);
5261

5362
$this->refreshNamedRoutes();
5463

5564
$this->assertTrue(Route::has('test-route-1'));
5665
$this->assertTrue(Route::has('test-route-2'));
5766
}
5867

68+
/**
69+
* Get package providers.
70+
*
71+
* @param \Illuminate\Foundation\Application $app
72+
*
73+
* @return array
74+
*/
75+
protected function getPackageProviders($app)
76+
{
77+
return [RouteFileMacroServiceProvider::class];
78+
}
79+
80+
/**
81+
* Refresh the named routes.
82+
*
83+
* @return void
84+
*/
5985
protected function refreshNamedRoutes()
6086
{
6187
app('router')->getRoutes()->refreshNameLookups();

0 commit comments

Comments
 (0)