|
1
|
|
|
<?php declare (strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Flute\Package; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
|
6
|
|
|
use Doctrine\DBAL\Types\Type; |
|
7
|
|
|
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A; |
|
8
|
|
|
use Limoncello\Contracts\Application\CacheSettingsProviderInterface; |
|
9
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
|
10
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
|
11
|
|
|
use Limoncello\Contracts\Data\ModelSchemaInfoInterface; |
|
12
|
|
|
use Limoncello\Contracts\Exceptions\ThrowableHandlerInterface; |
|
13
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
|
14
|
|
|
use Limoncello\Flute\Api\BasicRelationshipPaginationStrategy; |
|
15
|
|
|
use Limoncello\Flute\Contracts\Api\RelationshipPaginationStrategyInterface; |
|
16
|
|
|
use Limoncello\Flute\Contracts\Encoder\EncoderInterface; |
|
17
|
|
|
use Limoncello\Flute\Contracts\FactoryInterface; |
|
18
|
|
|
use Limoncello\Flute\Contracts\Http\Query\ParametersMapperInterface; |
|
19
|
|
|
use Limoncello\Flute\Contracts\Schema\JsonSchemasInterface; |
|
20
|
|
|
use Limoncello\Flute\Contracts\Validation\FormValidatorFactoryInterface; |
|
21
|
|
|
use Limoncello\Flute\Contracts\Validation\JsonApiParserFactoryInterface; |
|
22
|
|
|
use Limoncello\Flute\Factory; |
|
23
|
|
|
use Limoncello\Flute\Http\Query\ParametersMapper; |
|
24
|
|
|
use Limoncello\Flute\Http\ThrowableHandlers\FluteThrowableHandler; |
|
25
|
|
|
use Limoncello\Flute\Types\DateTimeType; |
|
26
|
|
|
use Limoncello\Flute\Types\DateType; |
|
27
|
|
|
use Limoncello\Flute\Validation\Form\Execution\FormValidatorFactory; |
|
28
|
|
|
use Limoncello\Flute\Validation\JsonApi\Execution\JsonApiParserFactory; |
|
29
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
|
30
|
|
|
use Psr\Log\LoggerInterface; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @package Limoncello\Flute |
|
34
|
|
|
* |
|
35
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
36
|
|
|
*/ |
|
37
|
|
|
class FluteContainerConfigurator implements ContainerConfiguratorInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var callable */ |
|
40
|
|
|
const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME]; |
|
41
|
|
|
|
|
42
|
|
|
/** @var callable */ |
|
43
|
|
|
const CONFIGURE_EXCEPTION_HANDLER = [self::class, 'configureExceptionHandler']; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
* |
|
48
|
|
|
* @throws DBALException |
|
49
|
|
|
* |
|
50
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
|
53
|
|
|
{ |
|
54
|
1 |
|
$factory = new Factory($container); |
|
55
|
|
|
|
|
56
|
1 |
|
$container[FactoryInterface::class] = $factory; |
|
57
|
|
|
|
|
58
|
|
|
$container[JsonSchemasInterface::class] = function (PsrContainerInterface $container) use ($factory) { |
|
59
|
1 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class); |
|
60
|
1 |
|
$modelSchemas = $container->get(ModelSchemaInfoInterface::class); |
|
61
|
|
|
|
|
62
|
1 |
|
return $factory->createJsonSchemas( |
|
63
|
1 |
|
$settings[FluteSettings::KEY_MODEL_TO_SCHEMA_MAP], |
|
64
|
1 |
|
$settings[FluteSettings::KEY_TYPE_TO_SCHEMA_MAP], |
|
65
|
1 |
|
$modelSchemas |
|
66
|
|
|
); |
|
67
|
|
|
}; |
|
68
|
|
|
|
|
69
|
|
|
$container[ParametersMapperInterface::class] = function (PsrContainerInterface $container) { |
|
70
|
1 |
|
return new ParametersMapper($container->get(JsonSchemasInterface::class)); |
|
71
|
|
|
}; |
|
72
|
|
|
|
|
73
|
|
|
$container[EncoderInterface::class] = function (PsrContainerInterface $container) use ($factory) { |
|
74
|
|
|
/** @var JsonSchemasInterface $jsonSchemas */ |
|
75
|
1 |
|
$jsonSchemas = $container->get(JsonSchemasInterface::class); |
|
76
|
1 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class); |
|
77
|
|
|
$encoder = $factory |
|
78
|
1 |
|
->createEncoder($jsonSchemas) |
|
79
|
1 |
|
->withEncodeOptions($settings[FluteSettings::KEY_JSON_ENCODE_OPTIONS]) |
|
80
|
1 |
|
->withEncodeDepth($settings[FluteSettings::KEY_JSON_ENCODE_DEPTH]) |
|
81
|
1 |
|
->withUrlPrefix($settings[FluteSettings::KEY_URI_PREFIX]); |
|
82
|
1 |
|
isset($settings[FluteSettings::KEY_META]) ? $encoder->withMeta($settings[FluteSettings::KEY_META]) : null; |
|
83
|
1 |
|
($settings[FluteSettings::KEY_IS_SHOW_VERSION] ?? false) ? |
|
84
|
1 |
|
$encoder->withJsonApiVersion(FluteSettings::DEFAULT_JSON_API_VERSION) : null; |
|
85
|
|
|
|
|
86
|
1 |
|
return $encoder; |
|
87
|
|
|
}; |
|
88
|
|
|
|
|
89
|
|
|
$container[RelationshipPaginationStrategyInterface::class] = function (PsrContainerInterface $container) { |
|
90
|
1 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class); |
|
91
|
|
|
|
|
92
|
1 |
|
return new BasicRelationshipPaginationStrategy($settings[FluteSettings::KEY_DEFAULT_PAGING_SIZE]); |
|
93
|
|
|
}; |
|
94
|
|
|
|
|
95
|
|
|
$container[JsonApiParserFactoryInterface::class] = function (PsrContainerInterface $container) { |
|
96
|
1 |
|
$factory = new JsonApiParserFactory($container); |
|
97
|
|
|
|
|
98
|
1 |
|
return $factory; |
|
99
|
|
|
}; |
|
100
|
|
|
|
|
101
|
|
|
$container[FormValidatorFactoryInterface::class] = function (PsrContainerInterface $container) { |
|
102
|
1 |
|
$factory = new FormValidatorFactory($container); |
|
103
|
|
|
|
|
104
|
1 |
|
return $factory; |
|
105
|
|
|
}; |
|
106
|
|
|
|
|
107
|
|
|
// register date/date time types |
|
108
|
1 |
|
Type::hasType(DateTimeType::NAME) === true ?: Type::addType(DateTimeType::NAME, DateTimeType::class); |
|
109
|
1 |
|
Type::hasType(DateType::NAME) === true ?: Type::addType(DateType::NAME, DateType::class); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param LimoncelloContainerInterface $container |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public static function configureExceptionHandler(LimoncelloContainerInterface $container) |
|
118
|
|
|
{ |
|
119
|
|
|
$container[ThrowableHandlerInterface::class] = function (PsrContainerInterface $container) { |
|
120
|
|
|
/** @var CacheSettingsProviderInterface $provider */ |
|
121
|
1 |
|
$provider = $container->get(CacheSettingsProviderInterface::class); |
|
122
|
1 |
|
$appConfig = $provider->getApplicationConfiguration(); |
|
123
|
1 |
|
$fluteSettings = $provider->get(FluteSettings::class); |
|
124
|
|
|
|
|
125
|
1 |
|
$isLogEnabled = $appConfig[A::KEY_IS_LOG_ENABLED]; |
|
126
|
1 |
|
$isDebug = $appConfig[A::KEY_IS_DEBUG]; |
|
127
|
|
|
|
|
128
|
1 |
|
$ignoredErrorClasses = $fluteSettings[FluteSettings::KEY_DO_NOT_LOG_EXCEPTIONS_LIST__AS_KEYS]; |
|
129
|
1 |
|
$codeForUnexpected = $fluteSettings[FluteSettings::KEY_HTTP_CODE_FOR_UNEXPECTED_THROWABLE]; |
|
130
|
|
|
$throwableConverter = |
|
131
|
1 |
|
$fluteSettings[FluteSettings::KEY_THROWABLE_TO_JSON_API_EXCEPTION_CONVERTER] ?? null; |
|
132
|
|
|
|
|
133
|
|
|
/** @var EncoderInterface $encoder */ |
|
134
|
1 |
|
$encoder = $container->get(EncoderInterface::class); |
|
135
|
|
|
|
|
136
|
1 |
|
$handler = new FluteThrowableHandler( |
|
137
|
1 |
|
$encoder, |
|
138
|
1 |
|
$ignoredErrorClasses, |
|
139
|
1 |
|
$codeForUnexpected, |
|
140
|
1 |
|
$isDebug, |
|
141
|
1 |
|
$throwableConverter |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
1 |
|
if ($isLogEnabled === true && $container->has(LoggerInterface::class) === true) { |
|
145
|
|
|
/** @var LoggerInterface $logger */ |
|
146
|
1 |
|
$logger = $container->get(LoggerInterface::class); |
|
147
|
1 |
|
$handler->setLogger($logger); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return $handler; |
|
151
|
|
|
}; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|