Pink-Crab /
Perique-Route
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Route Registration Middleware |
||
| 7 | * |
||
| 8 | * @since 0.1.0 |
||
| 9 | * @author Glynn Quelch <[email protected]> |
||
| 10 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
| 11 | * @package PinkCrab\Route |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace PinkCrab\Route\Module; |
||
| 15 | |||
| 16 | use PinkCrab\Route\Route\Route; |
||
|
0 ignored issues
–
show
|
|||
| 17 | use PinkCrab\Route\Route_Collection; |
||
| 18 | use PinkCrab\Route\Route_Controller; |
||
| 19 | use PinkCrab\Route\Route\Route_Group; |
||
| 20 | use PinkCrab\Route\Route\Abstract_Route; |
||
| 21 | use PinkCrab\Route\Registration\Route_Manager; |
||
| 22 | use PinkCrab\Perique\Interfaces\Registration_Middleware; |
||
| 23 | |||
| 24 | class Route_Middleware implements Registration_Middleware { |
||
| 25 | |||
| 26 | protected Route_Manager $route_manager; |
||
| 27 | |||
| 28 | public function __construct( Route_Manager $route_manager ) { |
||
| 29 | $this->route_manager = $route_manager; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Add all valid route calls to the dispatcher. |
||
| 34 | * |
||
| 35 | * @param object $class |
||
| 36 | * @return object |
||
| 37 | */ |
||
| 38 | public function process( object $class ): object { |
||
| 39 | |||
| 40 | if ( is_a( $class, Route_Controller::class ) ) { |
||
| 41 | $routes = $class->get_routes( new Route_Collection() ); |
||
| 42 | $routes->each( |
||
| 43 | function( Abstract_Route $route ) { |
||
| 44 | if ( is_a( $route, Route::class ) ) { |
||
| 45 | $this->route_manager->from_route( $route ); |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | if ( is_a( $route, Route_Group::class ) ) { |
||
| 50 | $this->route_manager->from_group( $route ); |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $class; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function setup(): void { |
||
| 61 | /*noOp*/ |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Register all routes with WordPress calls. |
||
| 66 | * |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | public function tear_down(): void { |
||
| 70 | $this->route_manager->execute(); |
||
| 71 | } |
||
| 72 | } |
||
| 73 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: