Skip to content

Commit

Permalink
feat: Add ablity to register custom route with action class for reusa…
Browse files Browse the repository at this point in the history
…bility

- Create a class in your Actions folder within your module
- Implement \LaraStrict\ContractsCreateCustomRouteActionContract
```php
public function execute(CustomRouteEntity $customRoute, RouteRegistrar $routeRegistrar): bool
    {
        $routeRegistrar
            ->prefix('custom-route/' . $customRoute->urlPrefix)
            ->group($customRoute->path);

        return true;
    }
```
- In your service provider within `getCustomRoutes` method add your class:
```php
'custom' => CreateCustomRouteAction::class,
```
  • Loading branch information
pionl committed Sep 27, 2022
1 parent 9ec88b4 commit 428e83c
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Use the discussion functionality and propose your idea:

## Commit messages

- We are using (conventionalcommits)[https://www.conventionalcommits.org/en/v1.0.0/]
- We are using [conventionalcommits](https://www.conventionalcommits.org/en/v1.0.0/)
- CHANGELOG.md is generated from given commits using this [action](https://github.com/requarks/changelog-action).
- These keywords will ignore changelog change `build,docs,other,style`

Expand Down
16 changes: 16 additions & 0 deletions src/Contracts/CreateCustomRouteActionContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Contracts;

use Illuminate\Routing\RouteRegistrar;
use LaraStrict\Entities\CustomRouteEntity;

interface CreateCustomRouteActionContract
{
/**
* Registers custom route. Returns false if no registration has been made.
*/
public function execute(CustomRouteEntity $customRoute, RouteRegistrar $routeRegistrar): bool;
}
2 changes: 1 addition & 1 deletion src/Contracts/HasCustomRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
interface HasCustomRoutes
{
/**
* @return array<string|int, Closure(CustomRouteEntity,RouteRegistrar):bool|string>
* @return array<string|int, class-string<CreateCustomRouteActionContract>|string|Closure(CustomRouteEntity,RouteRegistrar):bool>
*/
public function getCustomRoutes(): array;
}
24 changes: 17 additions & 7 deletions src/Providers/Pipes/LoadProviderRoutesPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Routing\RouteRegistrar;
use Illuminate\Support\Str;
use LaraStrict\Contracts\AppServiceProviderPipeContract;
use LaraStrict\Contracts\CreateCustomRouteActionContract;
use LaraStrict\Contracts\HasCustomPrefixRoutes;
use LaraStrict\Contracts\HasCustomRoutes;
use LaraStrict\Contracts\HasRoutes;
Expand Down Expand Up @@ -126,19 +127,28 @@ protected function tryToLoadCustomRoutes(
$didLoadRoutes = true;
}
} else {
if (is_callable($value) === false) {
throw new LogicException(
'Custom route with file suffix name as key expects closure to build the route'
);
}

$routeEntity = new CustomRouteEntity(
path: $this->getRouteFilePath($dir, $serviceName, $key),
serviceName: $serviceName,
urlPrefix: $urlPrefix
);

$result = $value($routeEntity, $this->makeRoute());
if (is_callable($value)) {
$result = $value($routeEntity, $this->makeRoute());
} elseif (is_string($value) && class_exists($value)) {
$class = $this->container->make($value);
if ($class instanceof CreateCustomRouteActionContract === false) {
throw new LogicException(
'To build custom route with class you need to implement ' . CreateCustomRouteActionContract::class
);
}

$result = $class->execute($routeEntity, $this->makeRoute());
} else {
throw new LogicException(
'To build the custom route with file suffix name as key expects closure or class that implements ' . CreateCustomRouteActionContract::class
);
}

if ($result) {
$didLoadRoutes = true;
Expand Down
12 changes: 8 additions & 4 deletions src/Testing/Providers/Concerns/AssertProviderRegistersRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait AssertProviderRegistersRoutes
/**
* Asserts correct that routes are correctly registered in correct prefix (pluralized)
*
* @param class-string<AbstractServiceProvider> $registerServiceProvider
* @param class-string<AbstractServiceProvider> $registerServiceProvider
* @param array<string, array<string>|array<int, Closure(Route):void>> $expectUrlsByMethod
* ['GET'=>['web/tests/my-api']]
*/
Expand All @@ -39,12 +39,16 @@ public function assertRoutes(
foreach ($expectUrlsByMethod as $method => $urls) {
$registeredUrls = $routes->get($method);

$errorMessage = 'Not found in: ' . implode(', ', array_keys($registeredUrls));

$currentUrls = [];
foreach ($urls as $index => $value) {
$url = is_callable($value) ? $index : $value;
$currentUrls[] = $url;
}

Assert::assertEquals(array_keys($registeredUrls), $currentUrls);

Assert::assertArrayHasKey($url, $registeredUrls, $errorMessage);
foreach ($urls as $index => $value) {
$url = is_callable($value) ? $index : $value;

if (is_callable($value)) {
$value($registeredUrls[$url]);
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/Providers/AbstractServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public function testWithCustomOnly(): void
'dev/with-customs/1-dev' => function (Route $route) {
$this->assertEquals([], $route->gatherMiddleware());
},
'custom-route/with-customs/1-custom' => function (Route $route) {
$this->assertEquals([], $route->gatherMiddleware());
},
],
]);
}
Expand All @@ -112,6 +115,9 @@ public function testWithAll(): void
'dev/with-alls/1-dev' => function (Route $route) {
$this->assertEquals([], $route->gatherMiddleware());
},
'custom-route/with-alls/2-custom' => function (Route $route) {
$this->assertEquals([], $route->gatherMiddleware());
},
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;

Route::get('2-custom', static function () {
});
21 changes: 21 additions & 0 deletions tests/Feature/Providers/WithCustom/CreateCustomRouteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Feature\Providers\WithCustom;

use Illuminate\Routing\RouteRegistrar;
use LaraStrict\Contracts\CreateCustomRouteActionContract;
use LaraStrict\Entities\CustomRouteEntity;

class CreateCustomRouteAction implements CreateCustomRouteActionContract
{
public function execute(CustomRouteEntity $customRoute, RouteRegistrar $routeRegistrar): bool
{
$routeRegistrar
->prefix('custom-route/' . $customRoute->urlPrefix)
->group($customRoute->path);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;

Route::get('1-custom', static function () {
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getCustomRoutes(): array
return true;
},
'testing' => static fn (): bool => false,
'custom' => CreateCustomRouteAction::class,
];
}
}

0 comments on commit 428e83c

Please sign in to comment.