|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\DependencyInjection; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Hydrator\DateTimeSerializer; |
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
16
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author John Kleijn <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class KleijnWebSwaggerExtension extends Extension |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
27
|
|
|
{ |
|
28
|
|
|
$config = $this->processConfiguration(new Configuration(), $configs); |
|
29
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
30
|
|
|
$loader->load('services.yml'); |
|
31
|
|
|
|
|
32
|
|
|
if ($config['handle_exceptions']) { |
|
33
|
|
|
$loader->load('listener_exception.yml'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$container->setParameter('swagger.document.base_path', $config['document']['base_path']); |
|
37
|
|
|
$container->setParameter('phpapi.router_name', 'swagger'); |
|
38
|
|
|
|
|
39
|
|
|
if (isset($config['document']['cache'])) { |
|
40
|
|
|
$resolverDefinition = $container->getDefinition('swagger.description.repository'); |
|
41
|
|
|
$resolverDefinition->addArgument(new Reference($config['document']['cache'])); |
|
42
|
|
|
} |
|
43
|
|
|
$responseFactory = $container->getDefinition('swagger.response.factory'); |
|
44
|
|
|
|
|
45
|
|
|
if (isset($config['hydrator'])) { |
|
46
|
|
|
$container |
|
47
|
|
|
->getDefinition('swagger.hydrator.class_name_resolver') |
|
48
|
|
|
->replaceArgument(0, $config['hydrator']['namespaces']); |
|
49
|
|
|
|
|
50
|
|
|
$dateTimeSerializerDefinition = $container->getDefinition('swagger.hydrator.class_name_resolver'); |
|
51
|
|
|
if (isset($config['hydrator']['date_formats'])) { |
|
52
|
|
|
foreach ($config['hydrator']['date_formats'] as $format) { |
|
53
|
|
|
$predefinedFormat = DateTimeSerializer::class . "::FORMAT_{$format}"; |
|
54
|
|
|
if (defined($predefinedFormat)) { |
|
55
|
|
|
$format = constant($predefinedFormat); |
|
56
|
|
|
} |
|
57
|
|
|
$dateTimeSerializerDefinition->addArgument(new Reference($format)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$builderDefinition = $container->getDefinition('swagger.hydrator.processor.builder'); |
|
62
|
|
|
|
|
63
|
|
|
if (isset($config['hydrator']['processors'])) { |
|
64
|
|
|
foreach ($config['hydrator']['processors'] as $processor) { |
|
65
|
|
|
$builderDefinition->addMethodCall('add', new Reference($processor)); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$hydrator = new Reference('swagger.hydrator'); |
|
70
|
|
|
$definition = $container->getDefinition('swagger.request.processor'); |
|
71
|
|
|
$definition->addArgument($hydrator); |
|
72
|
|
|
$responseFactory->replaceArgument(0, $hydrator); |
|
73
|
|
|
} |
|
74
|
|
|
if ($config['validate_responses']) { |
|
75
|
|
|
$responseFactory->addArgument(new Reference('swagger.request.validator')); |
|
76
|
|
|
} |
|
77
|
|
|
if ($config['ok_status_resolver']) { |
|
78
|
|
|
$responseFactory->addArgument(new Reference($config['ok_status_resolver'])); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$container->setParameter('swagger.match_unsecured', $config['security']['match_unsecured']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getAlias(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return "swagger"; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: