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