Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Aug 8, 2022
2 parents de39e1b + 3cfcc03 commit 9337d39
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 79 deletions.
6 changes: 6 additions & 0 deletions .changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'path' => 'CHANGELOG.md',
'tagPrefix' => 'v',
];
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!--- BEGIN HEADER -->
# Changelog

All notable changes to this project will be documented in this file.
<!--- END HEADER -->

## [1.0.0](https://github.com/olliecodes/laravel-route-registrars/compare/de39e1b20e6aae11e6cbf492b07b405c2f13c59f...v1.0.0) (2022-08-08)

---
80 changes: 79 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/olliecodes/laravel-route-registrars)
![GitHub](https://img.shields.io/github/license/olliecodes/laravel-route-registrars)
[![codecov](https://codecov.io/gh/olliecodes/laravel-route-registrars/branch/main/graph/badge.svg?token=FHJ41NQMTA)](https://codecov.io/gh/olliecodes/laravel-route-registrars)
[![Build Status](https://travis-ci.com/olliecodes/laravel-route-registrars.svg?branch=main)](https://travis-ci.com/olliecodes/laravel-route-registrars)

## Laravel Route Registrars
This package introduces a clean object based way to define your routes in a Laravel application. A tutorial on the basic premise exists on [Laravel news](https://laravel-news.com/route-registrars), written by [@juststeveking](https://twitter.com/JustSteveKing).
Expand All @@ -17,6 +16,20 @@ Install via composer.
$ composer require olliecodes/laravel-route-registrars
```

### Fresh Laravel Installation

If you're installing this package on a fresh laravel installation, you'll want to run the following command before you do anything with your routes.

```bash
php artisan init:routing
```

This will overwrite the `app/Providers/RouteServiceProvider.php` file with one compatible with the route registrars, and create default route registrars to replace both `routes/web.php` and `routes/api.php`.

### Existing Laravel Installation

If you have an existing application, it is recommended that you read through the Laravel News article that covers this approach, as you're going to have to manually refactor your route service provider and route files.

### Requirements

This package requires the following;
Expand All @@ -25,3 +38,68 @@ This package requires the following;
- `laravel/framework` >= 9.0

## Usage

To register new routes, you can either add their definitions to the `map` method of a `RouteRegistrar`, or create a new one.

### Creating a new registrar

The following command will create a new registrar inside `app/Http/Routes`.

```bash
php artisan make:registrar {name}
```

The `{name}` can be any valid class name, or sub-namespace. For example, using `Auth\\GuestRoutes` will create `app/Http/Routes/Auth/GuestRoutes.php`.

There are several options available when creating a registrar.

#### `--C|hasChildren`

Create a route registrar that has the `MapRouteRegistrars` trait so that it can map child registrars.

#### `--W|web`

Create the route registrar in `app/Http/Routes/Web`, an option left in for those of you that are splitting the Web and API routes.

#### `--A|API`

Exactly the same as the above option, except it creates it in `app/Http/Routes/Api`,

### Registering Registrars

Any class that wants to register registrars can use the following trait.

```
OllieCodes\Registrars\Concerns\MapsRouteRegistrars
```

Once doing so, there are three ways to register. All of them require an instance of the following class.

```
Illuminate\Contracts\Routing\Registrar
```

This interface is implemented by the Laravel `Router` class, so you can use that.

#### Register multiple
Call the following method with an instance of the laravel router, and an array of fully qualified class names for the registrars.

```
mapRouteRegistrars(Registrar $router, array $registrars)
```

#### Register one
Call the following method with an instance of the laravel router, and the fully qualified class name of the registrars.

```
mapRouteRegistrar(Registrar $router, string $registrar)
```

#### Register from paths
Call the following method with an instance of the laravel router, and an array of paths where the registrars are held.

This method will use the order that the files are returned, so please keep route order precedence in mind.

```
loadRouteRegistrars(Registrar $router, array $paths)
```
75 changes: 36 additions & 39 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
{
"name": "olliecodes/laravel-route-registrars",
"description": "Adds route registrars as an alternative object based approach to registering routes.",
"type": "library",
"license": "MIT",
"minimum-stability": "stable",
"authors": [
{
"name": "Ollie Read",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0",
"laravel/framework": "^9.0"
},
"require-dev": {
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"OllieCodes\\Registrars\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"OllieCodes\\Registrars\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"OllieCodes\\Registrars\\ToolkitServiceProvider"
],
"aliases": [
]
}
}
}
"name": "olliecodes/laravel-route-registrars",
"description": "Adds route registrars as an alternative object based approach to registering routes.",
"type": "library",
"license": "MIT",
"minimum-stability": "stable",
"authors": [
{
"name": "Ollie Read",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0",
"laravel/framework": "^9.0"
},
"require-dev": {
"marcocesarato/php-conventional-changelog": "^1.10"
},
"autoload": {
"psr-4": {
"OllieCodes\\Registrars\\": "src/"
}
},
"scripts": {
"changelog": "conventional-changelog --config=.changelog"
},
"extra": {
"laravel": {
"providers": [
"OllieCodes\\Registrars\\RouteRegistrarServiceProvider"
],
"aliases": []
}
},
"version": "1.0.0"
}
18 changes: 0 additions & 18 deletions phpunit.xml.dist

This file was deleted.

4 changes: 2 additions & 2 deletions resources/stubs/api-default-route-registrar.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace {{ rootNamespace }}Http\Routes\Api;

use Illuminate\Http\Request;
use Illuminate\Contracts\Routing\Registrar;
use OllieCodes\Toolkit\Routing\Contracts\RouteRegistrar;
use OllieCodes\Registrars\Contracts\RouteRegistrar;

class DefaultRoutes implements RouteRegistrar
{
Expand All @@ -19,6 +19,6 @@ class DefaultRoutes implements RouteRegistrar
{
$registrar->get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum')';
})->middleware('auth:sanctum');
}
}
4 changes: 2 additions & 2 deletions resources/stubs/api-route-registrar.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use OllieCodes\Registrars\Contracts\RouteRegistrar;
use OllieCodes\Registrars\Concerns\MapsRouteRegistrars;
use {{ rootNamespace }}Http\Routes\Api;

class WebRoutes implements RouteRegistrar
class ApiRoutes implements RouteRegistrar
{
use MapsRouteRegistrars;

Expand All @@ -31,7 +31,7 @@ class WebRoutes implements RouteRegistrar
'middleware' => 'api',
'prefix' => 'api'
], function (Registrar $registrar) {
$this->mapRoutes($registrar, $this->registrars);
$this->mapRouteRegistrars($registrar, $this->registrars);
});
}
}
5 changes: 4 additions & 1 deletion resources/stubs/route-service-provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace {{ rootNamespace }}Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use OllieCodes\Registrars\Concerns\MapsRouteRegistrars;
use {{ rootNamespace }}Http\Routes;
use RateLimiter;

class RouteServiceProvider extends ServiceProvider
{
Expand All @@ -29,7 +32,7 @@ class RouteServiceProvider extends ServiceProvider
$this->configureRateLimiting();

$this->routes(function (Registrar $router) {
$this->mapRoutes($router, $this->registrars);
$this->mapRouteRegistrars($router, $this->registrars);
});
}

Expand Down
2 changes: 1 addition & 1 deletion resources/stubs/web-default-route-registrar.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace {{ rootNamespace }}Http\Routes\Web;

use Illuminate\Contracts\Routing\Registrar;
use OllieCodes\Toolkit\Routing\Contracts\RouteRegistrar;
use OllieCodes\Registrars\Contracts\RouteRegistrar;

class DefaultRoutes implements RouteRegistrar
{
Expand Down
2 changes: 1 addition & 1 deletion resources/stubs/web-route-registrar.stub
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class WebRoutes implements RouteRegistrar
$registrar->group([
'middleware' => 'web',
], function (Registrar $registrar) {
$this->mapRoutes($registrar, $this->registrars);
$this->mapRouteRegistrars($registrar, $this->registrars);
});
}
}
24 changes: 21 additions & 3 deletions src/Commands/InitRoutingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function handle(): int
}

$this->overwriteRouteServiceProvider();
$this->createDirectories();
$this->addDefaultRoutes();

return self::SUCCESS;
Expand All @@ -50,20 +51,37 @@ private function addDefaultRoutes(): void
{
$defaultRoutes = [
'WebRoutes' => 'web-route-registrar.stub',
'Web\\DefaultRoutes' => 'web-default-route-registrar',
'Web\\DefaultRoutes' => 'web-default-route-registrar.stub',
'ApiRoutes' => 'api-route-registrar.stub',
'Api\\DefaultRoutes' => 'api-default-route-registrar.stub',
];

foreach ($defaultRoutes as $class => $stub) {
if ($this->createClass($class, $stub)) {
if ($this->createClass('Http\\Routes\\' . $class, $stub)) {
$this->info('Route registrar \'' . $class . '\' written');
} else {
$this->error('Unable to write \'' . $class . '\' class');
}
}
}

private function createDirectories(): void
{
$directories = [
app_path('Http/Routes'),
app_path('Http/Routes/Web'),
app_path('Http/Routes/Api')

];

foreach ($directories as $directory) {
if (! $this->files->isDirectory($directory)) {
$this->files->makeDirectory($directory);
$this->info('Created directory: ' . $directory);
}
}
}

private function overwriteRouteServiceProvider(): void
{
if ($this->createClass('Providers\\RouteServiceProvider', 'route-service-provider.stub')) {
Expand All @@ -74,7 +92,7 @@ private function overwriteRouteServiceProvider(): void
private function createClass(string $class, string $stub): bool
{
return $this->files->put(
app_path(str_replace('\\', __DIR__, $class) . '.php'),
app_path(str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'),
$this->getReplacement(
__DIR__ . '/../../resources/stubs/' . $stub,
$class
Expand Down
14 changes: 12 additions & 2 deletions src/Commands/RouteRegistrarMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class RouteRegistrarMakeCommand extends GeneratorCommand
use ConfirmableTrait;

protected $signature = 'make:registrar {name : The name of the route registrar}
{--C|hasChildren : Create the route registrar as a parent}';
{--C|hasChildren : Create the route registrar as a parent}
{--W|web : Create as a web route registrar}
{--A|api : Create as an API route registrar}';

protected $description = 'Create a route registrar';

Expand Down Expand Up @@ -41,6 +43,14 @@ protected function getStub(): string
*/
protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace . '\\Http\\Routes';
$namespace = $rootNamespace . '\\Http\\Routes';

if ($this->option('web')) {
$namespace .= '\\Web';
} else if ($this->option('api')) {
$namespace .= '\\Api';
}

return $namespace;
}
}
Loading

0 comments on commit 9337d39

Please sign in to comment.