Skip to content

Commit 6ca5ab0

Browse files
committed
Another refactor, this one seems to work the best
1 parent 7756b11 commit 6ca5ab0

File tree

7 files changed

+92
-151
lines changed

7 files changed

+92
-151
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
],
2121
"require": {
2222
"php": "^8.2",
23-
"spatie/laravel-package-tools": "^1.16",
24-
"illuminate/contracts": "^10.0||^11.0||^12.0",
25-
"illuminate/routing": "^10.0||^11.0||^12.0"
23+
"illuminate/contracts": ">=10.0",
24+
"illuminate/routing": ">=10.0",
25+
"spatie/invade": "^2.1",
26+
"spatie/laravel-package-tools": "^1.16"
2627
},
2728
"require-dev": {
2829
"laravel/pint": "^1.14",

src/Providers/RoutingServiceProvider.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Zacksmash\SoftDeleteRoutes\Providers;
44

5-
use Illuminate\Foundation\Application;
6-
use Illuminate\Routing\Router;
5+
use Illuminate\Routing\PendingResourceRegistration;
6+
use Illuminate\Routing\ResourceRegistrar as BaseResourceRegistrar;
77
use Illuminate\Support\ServiceProvider;
8+
use Zacksmash\SoftDeleteRoutes\RegisterRoute;
9+
use Zacksmash\SoftDeleteRoutes\ResourceRegistrar;
810

911
class RoutingServiceProvider extends ServiceProvider
1012
{
@@ -13,16 +15,31 @@ class RoutingServiceProvider extends ServiceProvider
1315
*/
1416
public function register(): void
1517
{
16-
$this->app->extend(Router::class, function (Router $router, Application $app) {
17-
return new \Zacksmash\SoftDeleteRoutes\Routing\Router($router, $app);
18-
});
18+
$this->app->bind(BaseResourceRegistrar::class, ResourceRegistrar::class);
1919
}
2020

2121
/**
2222
* Bootstrap any application services.
2323
*/
2424
public function boot(): void
2525
{
26-
//
26+
PendingResourceRegistration::macro('withRestore', function () {
27+
RegisterRoute::with('restore', $this->registrar);
28+
29+
return $this;
30+
});
31+
32+
PendingResourceRegistration::macro('withErase', function () {
33+
RegisterRoute::with('erase', $this->registrar);
34+
35+
return $this;
36+
});
37+
38+
PendingResourceRegistration::macro('softDeletes', function () {
39+
$this->withRestore();
40+
$this->withErase();
41+
42+
return $this;
43+
});
2744
}
2845
}

src/RegisterRoute.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Zacksmash\SoftDeleteRoutes;
4+
5+
class RegisterRoute
6+
{
7+
public static function with($action, $registrar)
8+
{
9+
$registrar = invade($registrar);
10+
11+
$registrar->verbs([$action => $action]);
12+
13+
$registrar->resourceDefaults = array_merge(
14+
$registrar->resourceDefaults, [$action => $action]
15+
);
16+
}
17+
}

src/ResourceRegistrar.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Zacksmash\SoftDeleteRoutes;
4+
5+
use Illuminate\Routing\ResourceRegistrar as BaseResourceRegistrar;
6+
7+
class ResourceRegistrar extends BaseResourceRegistrar
8+
{
9+
/**
10+
* Add the restore method for a resourceful route.
11+
*
12+
* @param string $name
13+
* @param string $base
14+
* @param string $controller
15+
* @param array $options
16+
* @return \Illuminate\Routing\Route
17+
*/
18+
protected function addResourceRestore($name, $base, $controller, $options)
19+
{
20+
$name = $this->getShallowName($name, $options);
21+
22+
$uri = $this->getResourceUri($name).'/{'.$base.'}/'.static::$verbs['restore'];
23+
24+
$action = $this->getResourceAction($name, $controller, 'restore', $options);
25+
26+
return $this->router->patch($uri, $action)->withTrashed();
27+
}
28+
29+
/**
30+
* Add the erase method for a resourceful route.
31+
*
32+
* @param string $name
33+
* @param string $base
34+
* @param string $controller
35+
* @param array $options
36+
* @return \Illuminate\Routing\Route
37+
*/
38+
protected function addResourceErase($name, $base, $controller, $options)
39+
{
40+
$name = $this->getShallowName($name, $options);
41+
42+
$uri = $this->getResourceUri($name).'/{'.$base.'}/'.static::$verbs['erase'];
43+
44+
$action = $this->getResourceAction($name, $controller, 'erase', $options);
45+
46+
return $this->router->delete($uri, $action)->withTrashed();
47+
}
48+
}

src/Routing/PendingResourceRegistration.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Routing/ResourceRegistrar.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Routing/Router.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)