1 | <?php |
||
2 | /** |
||
3 | * @package WPEmerge |
||
4 | * @author Atanas Angelov <[email protected]> |
||
5 | * @copyright 2018 Atanas Angelov |
||
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
||
7 | * @link https://wpemerge.com/ |
||
8 | */ |
||
9 | |||
10 | namespace WPEmerge\Routing; |
||
11 | |||
12 | use WPEmerge\Facades\Application; |
||
13 | use WPEmerge\Facades\Router as RouterFacade; |
||
14 | use WPEmerge\Routing\Conditions\ConditionFactory; |
||
15 | use WPEmerge\ServiceProviders\ExtendsConfigTrait; |
||
16 | use WPEmerge\ServiceProviders\ServiceProviderInterface; |
||
17 | |||
18 | /** |
||
19 | * Provide routing dependencies |
||
20 | * |
||
21 | * @codeCoverageIgnore |
||
22 | */ |
||
23 | class RoutingServiceProvider implements ServiceProviderInterface { |
||
24 | use ExtendsConfigTrait; |
||
25 | |||
26 | /** |
||
27 | * Key=>Class dictionary of condition types |
||
28 | * |
||
29 | * @var array<string, string> |
||
30 | */ |
||
31 | protected static $condition_types = [ |
||
32 | 'url' => \WPEmerge\Routing\Conditions\UrlCondition::class, |
||
33 | 'custom' => \WPEmerge\Routing\Conditions\CustomCondition::class, |
||
34 | 'multiple' => \WPEmerge\Routing\Conditions\MultipleCondition::class, |
||
35 | 'negate' => \WPEmerge\Routing\Conditions\NegateCondition::class, |
||
36 | 'post_id' => \WPEmerge\Routing\Conditions\PostIdCondition::class, |
||
37 | 'post_slug' => \WPEmerge\Routing\Conditions\PostSlugCondition::class, |
||
38 | 'post_status' => \WPEmerge\Routing\Conditions\PostStatusCondition::class, |
||
39 | 'post_template' => \WPEmerge\Routing\Conditions\PostTemplateCondition::class, |
||
40 | 'post_type' => \WPEmerge\Routing\Conditions\PostTypeCondition::class, |
||
41 | 'query_var' => \WPEmerge\Routing\Conditions\QueryVarCondition::class, |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * {@inheritDoc} |
||
46 | */ |
||
47 | public function register( $container ) { |
||
48 | $this->registerConfiguration( $container ); |
||
49 | $this->registerDependencies( $container ); |
||
50 | $this->registerFacades(); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Register configuration options. |
||
55 | * |
||
56 | * @param \Pimple\Container $container |
||
57 | * @return void |
||
58 | */ |
||
59 | protected function registerConfiguration( $container ) { |
||
60 | // TODO remove this config; see how Flash and OldInput will hook in to add their global middleware. |
||
61 | $this->extendConfig( $container, 'middleware_default_priority', 100 ); |
||
62 | $this->extendConfig( $container, 'middleware_priority', [] ); |
||
63 | $this->extendConfig( $container, 'global_middleware', [] ); |
||
64 | |||
65 | $container[ WPEMERGE_ROUTING_MIDDLEWARE_DEFAULT_PRIORITY_KEY ] = |
||
66 | $container[ WPEMERGE_CONFIG_KEY ]['middleware_default_priority']; |
||
67 | |||
68 | $container[ WPEMERGE_ROUTING_MIDDLEWARE_PRIORITY_KEY ] = |
||
69 | $container[ WPEMERGE_CONFIG_KEY ]['middleware_priority']; |
||
70 | |||
71 | $container[ WPEMERGE_ROUTING_GLOBAL_MIDDLEWARE_KEY ] = |
||
72 | $container[ WPEMERGE_CONFIG_KEY ]['global_middleware']; |
||
73 | |||
74 | $container[ WPEMERGE_ROUTING_CONDITION_TYPES_KEY ] = |
||
75 | static::$condition_types; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Register dependencies. |
||
80 | * |
||
81 | * @param \Pimple\Container $container |
||
82 | * @return void |
||
83 | */ |
||
84 | protected function registerDependencies( $container ) { |
||
85 | $container[ WPEMERGE_ROUTING_ROUTER_KEY ] = function ( $c ) { |
||
86 | return new Router( |
||
87 | $c[ WPEMERGE_ROUTING_CONDITIONS_CONDITION_FACTORY_KEY ], |
||
88 | $c[ WPEMERGE_EXCEPTIONS_ERROR_HANDLER_KEY ] |
||
89 | ); |
||
90 | }; |
||
91 | |||
92 | $container[ WPEMERGE_ROUTING_CONDITIONS_CONDITION_FACTORY_KEY ] = function ( $c ) { |
||
93 | return new ConditionFactory( $c[ WPEMERGE_ROUTING_CONDITION_TYPES_KEY ] ); |
||
94 | }; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Register facades. |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | protected function registerFacades() { |
||
103 | Application::facade( 'Router', RouterFacade::class ); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | public function bootstrap( $container ) { |
||
110 | // Nothing to bootstrap. |
||
111 | } |
||
112 | } |
||
113 |