1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicofuma\SwaggerBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcher; |
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
12
|
|
|
|
13
|
|
|
class NicofumaSwaggerExtension extends Extension |
14
|
|
|
{ |
15
|
|
|
private $requestMatchers = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
public function load(array $configs, ContainerBuilder $container) |
21
|
|
|
{ |
22
|
|
|
$config = $this->processConfiguration(new Configuration(), $configs); |
23
|
|
|
|
24
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
25
|
|
|
$loader->load('services.yml'); |
26
|
|
|
|
27
|
|
|
$mapDefinition = $container->getDefinition('swagger.validator_map'); |
28
|
|
|
$map = []; |
29
|
|
|
foreach ($config['definitions'] as $key => $definition) { |
30
|
|
|
$matcher = $this->createRequestMatcher( |
31
|
|
|
$container, |
32
|
|
|
$definition['pattern']['path'], |
33
|
|
|
$definition['pattern']['host'], |
34
|
|
|
$definition['pattern']['methods'], |
35
|
|
|
$definition['pattern']['ips'] |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$schemaManagerId = 'swagger.schema_manager.'.$key; |
39
|
|
|
$schemaManager = $container->setDefinition($schemaManagerId, new DefinitionDecorator('swagger.schema_manager')); |
40
|
|
|
$schemaManager->replaceArgument(0, $definition['swagger_file']); |
41
|
|
|
|
42
|
|
|
$validatorId = 'swagger.validator.'.$key; |
43
|
|
|
$validator = $container->setDefinition($validatorId, new DefinitionDecorator('swagger.validator')); |
44
|
|
|
$validator->replaceArgument(1, new Reference($schemaManagerId)); |
45
|
|
|
$validator->replaceArgument(2, $definition['strict']); |
46
|
|
|
|
47
|
|
|
$map[$validatorId] = $matcher; |
48
|
|
|
} |
49
|
|
|
$mapDefinition->replaceArgument(1, $map); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = [], $ip = null, array $attributes = []) |
53
|
|
|
{ |
54
|
|
|
if ($methods) { |
|
|
|
|
55
|
|
|
$methods = array_map('strtoupper', (array) $methods); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$serialized = serialize([$path, $host, $methods, $ip, $attributes]); |
59
|
|
|
$id = 'swagger.request_matcher.'.md5($serialized).sha1($serialized); |
60
|
|
|
|
61
|
|
|
if (isset($this->requestMatchers[$id])) { |
62
|
|
|
return $this->requestMatchers[$id]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// only add arguments that are necessary |
66
|
|
|
$arguments = [$path, $host, $methods, $ip, $attributes]; |
67
|
|
|
while (count($arguments) > 0 && !end($arguments)) { |
68
|
|
|
array_pop($arguments); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$container |
72
|
|
|
->register($id, RequestMatcher::class) |
73
|
|
|
->setPublic(false) |
74
|
|
|
->setArguments($arguments) |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
return $this->requestMatchers[$id] = new Reference($id); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.