|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the doyo/mezzio-testing project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anthonius Munthi <https://itstoni.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Doyo\Mezzio\Testing\Modules; |
|
15
|
|
|
|
|
16
|
|
|
use Laminas\ConfigAggregator\ArrayProvider; |
|
17
|
|
|
use Laminas\ConfigAggregator\ConfigAggregator; |
|
18
|
|
|
use Laminas\ConfigAggregator\PhpFileProvider; |
|
19
|
|
|
use Laminas\Stratigility\Middleware\ErrorHandler; |
|
20
|
|
|
use Mezzio\Application; |
|
21
|
|
|
use Mezzio\Handler\NotFoundHandler; |
|
22
|
|
|
use Mezzio\Helper\ServerUrlMiddleware; |
|
23
|
|
|
use Mezzio\Helper\UrlHelperMiddleware; |
|
24
|
|
|
use Mezzio\MiddlewareFactory; |
|
25
|
|
|
use Mezzio\Router\Middleware\DispatchMiddleware; |
|
26
|
|
|
use Mezzio\Router\Middleware\ImplicitHeadMiddleware; |
|
27
|
|
|
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware; |
|
28
|
|
|
use Mezzio\Router\Middleware\MethodNotAllowedMiddleware; |
|
29
|
|
|
use Mezzio\Router\Middleware\RouteMiddleware; |
|
30
|
|
|
use Psr\Container\ContainerInterface; |
|
31
|
|
|
|
|
32
|
|
|
trait WithContainer |
|
33
|
|
|
{ |
|
34
|
|
|
protected static ?ContainerInterface $container = null; |
|
35
|
|
|
|
|
36
|
|
|
protected static ?Application $app = null; |
|
37
|
|
|
|
|
38
|
|
|
protected static array $config = []; |
|
39
|
|
|
|
|
40
|
|
|
protected static array $configProviders = [ |
|
41
|
|
|
\Laminas\HttpHandlerRunner\ConfigProvider::class, |
|
42
|
|
|
\Mezzio\ConfigProvider::class, |
|
43
|
|
|
\Mezzio\Router\ConfigProvider::class, |
|
44
|
|
|
\Laminas\Diactoros\ConfigProvider::class, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
protected static array $uses; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string|ArrayProvider|PhpFileProvider $provider |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function addConfigProvider($provider): void |
|
53
|
|
|
{ |
|
54
|
|
|
if ( ! \in_array($provider, static::$configProviders, true)) { |
|
55
|
|
|
static::$configProviders[] = $provider; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected static function configure(): void |
|
60
|
|
|
{ |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @psalm-suppress UndefinedMethod |
|
65
|
|
|
*/ |
|
66
|
|
|
protected static function initialize(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$uses = static::$uses = array_flip(static::getClassUses(static::class)); |
|
69
|
|
|
|
|
70
|
|
|
if (isset($uses[WithFastRoute::class])) { |
|
71
|
|
|
static::configureFastRoute(); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
static::configure(); |
|
74
|
|
|
|
|
75
|
|
|
static::initConfig(); |
|
76
|
|
|
static::initTraits(); |
|
77
|
|
|
static::initContainer(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @psalm-suppress UndefinedMethod |
|
82
|
|
|
*/ |
|
83
|
|
|
protected static function initTraits(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$uses = static::$uses; |
|
86
|
|
|
|
|
87
|
|
|
if (isset($uses[WithLaminasServiceManager::class])) { |
|
88
|
|
|
static::setupServiceManager(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected static function initConfig(): void |
|
93
|
|
|
{ |
|
94
|
|
|
$aggregator = new ConfigAggregator(static::$configProviders); |
|
95
|
|
|
static::$config = $aggregator->getMergedConfig(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected static function initContainer(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$container = static::$container; |
|
101
|
|
|
\assert(null !== $container); |
|
102
|
|
|
|
|
103
|
|
|
/** @var Application|null $app */ |
|
104
|
|
|
$app = $container->get(Application::class); |
|
105
|
|
|
/** @var MiddlewareFactory|null $middlewareFactory */ |
|
106
|
|
|
$middlewareFactory = $container->get(MiddlewareFactory::class); |
|
107
|
|
|
|
|
108
|
|
|
\assert(null !== $app); |
|
109
|
|
|
\assert(null !== $middlewareFactory); |
|
110
|
|
|
static::configurePipeline($app, $middlewareFactory, $container); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected static function configurePipeline( |
|
114
|
|
|
Application $app, |
|
115
|
|
|
MiddlewareFactory $factory, |
|
|
|
|
|
|
116
|
|
|
ContainerInterface $container |
|
|
|
|
|
|
117
|
|
|
): void { |
|
118
|
|
|
$app->pipe(ErrorHandler::class); |
|
119
|
|
|
$app->pipe(ServerUrlMiddleware::class); |
|
120
|
|
|
$app->pipe(RouteMiddleware::class); |
|
121
|
|
|
$app->pipe(ImplicitHeadMiddleware::class); |
|
122
|
|
|
$app->pipe(ImplicitOptionsMiddleware::class); |
|
123
|
|
|
$app->pipe(MethodNotAllowedMiddleware::class); |
|
124
|
|
|
$app->pipe(UrlHelperMiddleware::class); |
|
125
|
|
|
$app->pipe(DispatchMiddleware::class); |
|
126
|
|
|
$app->pipe(NotFoundHandler::class); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
protected static function configureRoutes( |
|
130
|
|
|
Application $app, |
|
|
|
|
|
|
131
|
|
|
MiddlewareFactory $factory, |
|
|
|
|
|
|
132
|
|
|
ContainerInterface $container |
|
|
|
|
|
|
133
|
|
|
): void { |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return mixed|Application |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getApplication() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->getService(Application::class); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $id |
|
146
|
|
|
* |
|
147
|
|
|
* @return mixed |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getService(string $id) |
|
150
|
|
|
{ |
|
151
|
|
|
\assert(null !== static::$container); |
|
152
|
|
|
|
|
153
|
|
|
return static::$container->get($id); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
private static function getClassUses(string $class): array |
|
157
|
|
|
{ |
|
158
|
|
|
$results = []; |
|
159
|
|
|
|
|
160
|
|
|
foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) { |
|
161
|
|
|
$results += static::traitUsesRecursive($class); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return array_unique($results); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
private static function traitUsesRecursive(string $trait): array |
|
168
|
|
|
{ |
|
169
|
|
|
$traits = class_uses($trait); |
|
170
|
|
|
|
|
171
|
|
|
foreach ($traits as $trait) { |
|
172
|
|
|
$traits = array_merge($traits, static::traitUsesRecursive($trait)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $traits; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.