|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Abstract Route Controller |
|
7
|
|
|
* |
|
8
|
|
|
* @package PinkCrab\Route\Route |
|
9
|
|
|
* @author Glynn Quelch [email protected] |
|
10
|
|
|
* @since 0.0.1 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace PinkCrab\Route; |
|
14
|
|
|
|
|
15
|
|
|
use PinkCrab\Route\Route\Route; |
|
16
|
|
|
use PinkCrab\Route\Route_Factory; |
|
17
|
|
|
use PinkCrab\Route\Route_Exception; |
|
18
|
|
|
use PinkCrab\Route\Route_Collection; |
|
19
|
|
|
use PinkCrab\Route\Route\Route_Group; |
|
20
|
|
|
|
|
21
|
|
|
abstract class Route_Controller { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The namespace for this controllers routes |
|
25
|
|
|
* |
|
26
|
|
|
* @required |
|
27
|
|
|
* @var string|null |
|
28
|
|
|
*/ |
|
29
|
|
|
protected ?string $namespace = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns the controllers namespace |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
* @throws Route_Exception (code 101) |
|
36
|
|
|
*/ |
|
37
|
|
|
private function get_namespace(): string { |
|
38
|
|
|
if ( ! is_string( $this->namespace ) || mb_strlen( $this->namespace ) === 0 ) { |
|
39
|
|
|
throw Route_Exception::namespace_not_defined( \esc_html( get_class( $this ) ) ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->namespace; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns a factory for this controller. |
|
47
|
|
|
* |
|
48
|
|
|
* @return Route_Factory |
|
49
|
|
|
* @throws Route_Exception (code 101) |
|
50
|
|
|
*/ |
|
51
|
|
|
private function get_factory(): Route_Factory { |
|
52
|
|
|
return Route_Factory::for( $this->get_namespace() ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Adds all routes defined to the passed route collection. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Route_Collection $collection |
|
59
|
|
|
* @return Route_Collection |
|
60
|
|
|
*/ |
|
61
|
|
|
final public function get_routes( Route_Collection $collection ): Route_Collection { |
|
62
|
|
|
$routes = $this->define_routes( $this->get_factory() ); |
|
63
|
|
|
foreach ( $routes as $route ) { |
|
64
|
|
|
$collection->add_route( $route ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $collection; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Method defined to register all routes. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Route_Factory $factory |
|
74
|
|
|
* @return array<Route|Route_Group> |
|
75
|
|
|
*/ |
|
76
|
|
|
abstract protected function define_routes( Route_Factory $factory ): array; |
|
77
|
|
|
} |
|
78
|
|
|
|