Passed
Push — master ( 448c6e...d87c6c )
by Glynn
09:54 queued 08:08
created

Route_Middleware::tear_down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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
Bug introduced by
This use statement conflicts with another class in this namespace, PinkCrab\Route\Module\Route. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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