|
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\ServiceProviderInterface; |
|
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 ServiceProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
const PACKAGE_NAME = 'thecodingmachine/splash'; |
|
24
|
|
|
|
|
25
|
|
|
public function getFactories() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
SplashDefaultRouter::class => [self::class, 'createDefaultRouter'], |
|
29
|
|
|
'thecodingmachine.splash.route-providers' => [self::class, 'createRouteProviders'], |
|
30
|
|
|
ControllerRegistry::class => [self::class, 'createControllerRegistry'], |
|
31
|
|
|
ControllerAnalyzer::class => [self::class, 'createControllerAnalyzer'], |
|
32
|
|
|
ParameterFetcherRegistry::class => [self::class, 'createParameterFetcherRegistry'], |
|
33
|
|
|
'thecodingmachine.splash.parameter-fetchers' => [self::class, 'createParameterFetchers'], |
|
34
|
|
|
SplashRequestFetcher::class => [self::class, 'createSplashRequestFetcher'], |
|
35
|
|
|
SplashRequestParameterFetcher::class => [self::class, 'createSplashRequestParameterFetcher'], |
|
36
|
|
|
'thecodingmachine.splash.mode' => new Parameter(SplashUtils::MODE_STRICT), |
|
37
|
|
|
'thecodingmachine.splash.controllers' => new Parameter([]) |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getExtensions() |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
|
|
|
|
|
44
|
|
|
MiddlewareListServiceProvider::MIDDLEWARES_QUEUE => [self::class, 'updatePriorityQueue'], |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function createDefaultRouter(ContainerInterface $container) : SplashDefaultRouter |
|
49
|
|
|
{ |
|
50
|
|
|
if ($container->has(CacheItemPoolInterface::class)) { |
|
51
|
|
|
$cache = $container->get(CacheItemPoolInterface::class); |
|
52
|
|
|
} else { |
|
53
|
|
|
$cache = null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($container->has(LoggerInterface::class)) { |
|
57
|
|
|
$logger = $container->get(LoggerInterface::class); |
|
58
|
|
|
} else { |
|
59
|
|
|
$logger = null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$routeProviders = $container->get('thecodingmachine.splash.route-providers'); |
|
63
|
|
|
|
|
64
|
|
|
$router = new SplashDefaultRouter($container, $routeProviders, $container->get(ParameterFetcherRegistry::class), $cache, $logger, SplashUtils::MODE_STRICT, true, self::getRootUrl($container)); |
|
65
|
|
|
|
|
66
|
|
|
return $router; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private static function getRootUrl(ContainerInterface $container) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($container->has('thecodingmachine.splash.root_url')) { |
|
72
|
|
|
return $container->get('thecodingmachine.splash.root_url'); |
|
73
|
|
|
} elseif ($container->has('root_url')) { |
|
74
|
|
|
return $container->get('root_url'); |
|
75
|
|
|
} else { |
|
76
|
|
|
return '/'; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public static function createRouteProviders(ContainerInterface $container) : array |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
|
|
$container->get(ControllerRegistry::class), |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public static function createControllerRegistry(ContainerInterface $container) : ControllerRegistry |
|
88
|
|
|
{ |
|
89
|
|
|
return new ControllerRegistry($container->get(ControllerAnalyzer::class), |
|
90
|
|
|
$container->get('thecodingmachine.splash.controllers')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public static function createControllerAnalyzer(ContainerInterface $container) : ControllerAnalyzer |
|
94
|
|
|
{ |
|
95
|
|
|
return new ControllerAnalyzer($container, $container->get(ParameterFetcherRegistry::class), |
|
96
|
|
|
$container->get(Reader::class)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function createParameterFetcherRegistry(ContainerInterface $container) : ParameterFetcherRegistry |
|
100
|
|
|
{ |
|
101
|
|
|
return new ParameterFetcherRegistry($container->get('thecodingmachine.splash.parameter-fetchers')); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function createParameterFetchers(ContainerInterface $container) : array |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
|
|
$container->get(SplashRequestFetcher::class), |
|
108
|
|
|
$container->get(SplashRequestParameterFetcher::class), |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public static function createSplashRequestFetcher() : SplashRequestFetcher |
|
113
|
|
|
{ |
|
114
|
|
|
return new SplashRequestFetcher(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public static function createSplashRequestParameterFetcher() : SplashRequestParameterFetcher |
|
118
|
|
|
{ |
|
119
|
|
|
return new SplashRequestParameterFetcher(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public static function updatePriorityQueue(ContainerInterface $container, \SplPriorityQueue $priorityQueue) : \SplPriorityQueue |
|
123
|
|
|
{ |
|
124
|
|
|
$priorityQueue->insert($container->get(SplashDefaultRouter::class), MiddlewareOrder::ROUTER); |
|
125
|
|
|
return $priorityQueue; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.