1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\DI; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use Interop\Container\ContainerInterface; |
7
|
|
|
use Interop\Container\Factories\Parameter; |
8
|
|
|
use Interop\Container\ServiceProvider; |
9
|
|
|
use Mouf\Mvc\Splash\Routers\SplashDefaultRouter; |
10
|
|
|
use Mouf\Mvc\Splash\Services\ControllerAnalyzer; |
11
|
|
|
use Mouf\Mvc\Splash\Services\ControllerRegistry; |
12
|
|
|
use Mouf\Mvc\Splash\Services\ParameterFetcherRegistry; |
13
|
|
|
use Mouf\Mvc\Splash\Services\SplashRequestFetcher; |
14
|
|
|
use Mouf\Mvc\Splash\Services\SplashRequestParameterFetcher; |
15
|
|
|
use Mouf\Mvc\Splash\Services\SplashUtils; |
16
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use TheCodingMachine\MiddlewareListServiceProvider; |
19
|
|
|
use TheCodingMachine\MiddlewareOrder; |
20
|
|
|
|
21
|
|
|
class SplashServiceProvider implements ServiceProvider |
22
|
|
|
{ |
23
|
|
|
const PACKAGE_NAME = 'thecodingmachine/splash'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns a list of all container entries registered by this service provider. |
27
|
|
|
* |
28
|
|
|
* - the key is the entry name |
29
|
|
|
* - the value is a callable that will return the entry, aka the **factory** |
30
|
|
|
* |
31
|
|
|
* Factories have the following signature: |
32
|
|
|
* function(ContainerInterface $container, callable $getPrevious = null) |
33
|
|
|
* |
34
|
|
|
* About factories parameters: |
35
|
|
|
* |
36
|
|
|
* - the container (instance of `Interop\Container\ContainerInterface`) |
37
|
|
|
* - a callable that returns the previous entry if overriding a previous entry, or `null` if not |
38
|
|
|
* |
39
|
|
|
* @return callable[] |
40
|
|
|
*/ |
41
|
|
|
public function getServices() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
SplashDefaultRouter::class => [self::class, 'createDefaultRouter'], |
45
|
|
|
'thecodingmachine.splash.route-providers' => [self::class, 'createRouteProviders'], |
46
|
|
|
ControllerRegistry::class => [self::class, 'createControllerRegistry'], |
47
|
|
|
ControllerAnalyzer::class => [self::class, 'createControllerAnalyzer'], |
48
|
|
|
ParameterFetcherRegistry::class => [self::class, 'createParameterFetcherRegistry'], |
49
|
|
|
'thecodingmachine.splash.parameter-fetchers' => [self::class, 'createParameterFetchers'], |
50
|
|
|
SplashRequestFetcher::class => [self::class, 'createSplashRequestFetcher'], |
51
|
|
|
SplashRequestParameterFetcher::class => [self::class, 'createSplashRequestParameterFetcher'], |
52
|
|
|
'thecodingmachine.splash.mode' => new Parameter(SplashUtils::MODE_STRICT), |
53
|
|
|
MiddlewareListServiceProvider::MIDDLEWARES_QUEUE => [self::class, 'updatePriorityQueue'] |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function createDefaultRouter(ContainerInterface $container) : SplashDefaultRouter |
58
|
|
|
{ |
59
|
|
|
if ($container->has(CacheItemPoolInterface::class)) { |
60
|
|
|
$cache = $container->get(CacheItemPoolInterface::class); |
61
|
|
|
} else { |
62
|
|
|
$cache = null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($container->has(LoggerInterface::class)) { |
66
|
|
|
$logger = $container->get(LoggerInterface::class); |
67
|
|
|
} else { |
68
|
|
|
$logger = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$routeProviders = $container->get('thecodingmachine.splash.route-providers'); |
72
|
|
|
|
73
|
|
|
$router = new SplashDefaultRouter($container, $routeProviders, $container->get(ParameterFetcherRegistry::class), $cache, $logger, SplashUtils::MODE_STRICT, true, self::getRootUrl($container)); |
74
|
|
|
|
75
|
|
|
return $router; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private static function getRootUrl(ContainerInterface $container) |
79
|
|
|
{ |
80
|
|
|
if ($container->has('thecodingmachine.splash.root_url')) { |
81
|
|
|
return $container->get('thecodingmachine.splash.root_url'); |
82
|
|
|
} elseif ($container->has('root_url')) { |
83
|
|
|
return $container->get('root_url'); |
84
|
|
|
} else { |
85
|
|
|
return '/'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function createRouteProviders(ContainerInterface $container) : array |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
$container->get(ControllerRegistry::class), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public static function createControllerRegistry(ContainerInterface $container) : ControllerRegistry |
97
|
|
|
{ |
98
|
|
|
return new ControllerRegistry($container->get(ControllerAnalyzer::class), |
99
|
|
|
$container->get('thecodingmachine.splash.controllers')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public static function createControllerAnalyzer(ContainerInterface $container) : ControllerAnalyzer |
103
|
|
|
{ |
104
|
|
|
return new ControllerAnalyzer($container, $container->get(ParameterFetcherRegistry::class), |
105
|
|
|
$container->get(Reader::class)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public static function createParameterFetcherRegistry(ContainerInterface $container) : ParameterFetcherRegistry |
109
|
|
|
{ |
110
|
|
|
return new ParameterFetcherRegistry($container->get('thecodingmachine.splash.parameter-fetchers')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function createParameterFetchers(ContainerInterface $container) : array |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
$container->get(SplashRequestFetcher::class), |
117
|
|
|
$container->get(SplashRequestParameterFetcher::class), |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function createSplashRequestFetcher() : SplashRequestFetcher |
122
|
|
|
{ |
123
|
|
|
return new SplashRequestFetcher(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function createSplashRequestParameterFetcher() : SplashRequestParameterFetcher |
127
|
|
|
{ |
128
|
|
|
return new SplashRequestParameterFetcher(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function updatePriorityQueue(ContainerInterface $container, callable $previous = null) : \SplPriorityQueue |
132
|
|
|
{ |
133
|
|
|
if ($previous) { |
134
|
|
|
$priorityQueue = $previous(); |
135
|
|
|
$priorityQueue->insert($container->get(SplashDefaultRouter::class), MiddlewareOrder::ROUTER); |
136
|
|
|
return $priorityQueue; |
137
|
|
|
} else { |
138
|
|
|
throw new InvalidArgumentException("Could not find declaration for service '".MiddlewareListServiceProvider::MIDDLEWARES_QUEUE."'."); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|