|
1
|
|
|
<?php namespace Limoncello\Flute\Package; |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\DBAL\Connection; |
|
4
|
|
|
use Doctrine\DBAL\Types\Type; |
|
5
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
|
6
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
|
7
|
|
|
use Limoncello\Contracts\Data\ModelSchemeInfoInterface; |
|
8
|
|
|
use Limoncello\Contracts\Exceptions\ExceptionHandlerInterface; |
|
9
|
|
|
use Limoncello\Contracts\L10n\FormatterFactoryInterface; |
|
10
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
|
11
|
|
|
use Limoncello\Flute\Adapters\FilterOperations; |
|
12
|
|
|
use Limoncello\Flute\Adapters\PaginationStrategy; |
|
13
|
|
|
use Limoncello\Flute\Contracts\Adapters\FilterOperationsInterface; |
|
14
|
|
|
use Limoncello\Flute\Contracts\Adapters\PaginationStrategyInterface; |
|
15
|
|
|
use Limoncello\Flute\Contracts\Adapters\RepositoryInterface; |
|
16
|
|
|
use Limoncello\Flute\Contracts\Encoder\EncoderInterface; |
|
17
|
|
|
use Limoncello\Flute\Contracts\FactoryInterface; |
|
18
|
|
|
use Limoncello\Flute\Contracts\Schema\JsonSchemesInterface; |
|
19
|
|
|
use Limoncello\Flute\Contracts\Validation\JsonApiValidatorFactoryInterface; |
|
20
|
|
|
use Limoncello\Flute\Factory; |
|
21
|
|
|
use Limoncello\Flute\Http\Errors\FluteExceptionHandler; |
|
22
|
|
|
use Limoncello\Flute\L10n\Messages; |
|
23
|
|
|
use Limoncello\Flute\Types\DateJsonApiStringType; |
|
24
|
|
|
use Limoncello\Flute\Types\DateTimeJsonApiStringType; |
|
25
|
|
|
use Limoncello\Flute\Types\JsonApiDateTimeType; |
|
26
|
|
|
use Limoncello\Flute\Types\JsonApiDateType; |
|
27
|
|
|
use Limoncello\Flute\Validation\Execution\JsonApiValidatorFactory; |
|
28
|
|
|
use Neomerx\JsonApi\Contracts\Http\Query\QueryParametersParserInterface; |
|
29
|
|
|
use Neomerx\JsonApi\Encoder\EncoderOptions; |
|
30
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @package Limoncello\Flute |
|
34
|
|
|
* |
|
35
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
36
|
|
|
*/ |
|
37
|
|
|
class FluteContainerConfigurator implements ContainerConfiguratorInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var callable */ |
|
40
|
|
|
const CONFIGURE_EXCEPTION_HANDLER = [self::class, 'configureExceptionHandler']; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
* |
|
45
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
|
48
|
|
|
{ |
|
49
|
1 |
|
$factory = new Factory($container); |
|
50
|
|
|
|
|
51
|
1 |
|
$container[FactoryInterface::class] = $factory; |
|
52
|
|
|
|
|
53
|
1 |
|
$container[QueryParametersParserInterface::class] = function () use ($factory) { |
|
54
|
1 |
|
return $factory->getJsonApiFactory()->createQueryParametersParser(); |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
1 |
|
$container[JsonSchemesInterface::class] = function (PsrContainerInterface $container) use ($factory) { |
|
58
|
1 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class); |
|
59
|
1 |
|
$modelSchemes = $container->get(ModelSchemeInfoInterface::class); |
|
60
|
|
|
|
|
61
|
1 |
|
return $factory->createJsonSchemes($settings[FluteSettings::KEY_MODEL_TO_SCHEME_MAP], $modelSchemes); |
|
62
|
|
|
}; |
|
63
|
|
|
|
|
64
|
1 |
|
$container[EncoderInterface::class] = function (PsrContainerInterface $container) use ($factory) { |
|
65
|
|
|
/** @var JsonSchemesInterface $jsonSchemes */ |
|
66
|
1 |
|
$jsonSchemes = $container->get(JsonSchemesInterface::class); |
|
67
|
1 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class); |
|
68
|
1 |
|
$encoder = $factory->createEncoder($jsonSchemes, new EncoderOptions( |
|
69
|
1 |
|
$settings[FluteSettings::KEY_JSON_ENCODE_OPTIONS], |
|
70
|
1 |
|
$settings[FluteSettings::KEY_URI_PREFIX], |
|
71
|
1 |
|
$settings[FluteSettings::KEY_JSON_ENCODE_DEPTH] |
|
72
|
|
|
)); |
|
73
|
1 |
|
isset($settings[FluteSettings::KEY_META]) ? $encoder->withMeta($settings[FluteSettings::KEY_META]) : null; |
|
74
|
1 |
|
($settings[FluteSettings::KEY_IS_SHOW_VERSION] ?? false) ? $encoder->withJsonApiVersion() : null; |
|
75
|
|
|
|
|
76
|
1 |
|
return $encoder; |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
1 |
|
$container[FilterOperationsInterface::class] = function (PsrContainerInterface $container) { |
|
80
|
1 |
|
return new FilterOperations($container); |
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
1 |
|
$container[RepositoryInterface::class] = function (PsrContainerInterface $container) use ($factory) { |
|
84
|
1 |
|
$connection = $container->get(Connection::class); |
|
85
|
|
|
/** @var ModelSchemeInfoInterface $modelSchemes */ |
|
86
|
1 |
|
$modelSchemes = $container->get(ModelSchemeInfoInterface::class); |
|
87
|
|
|
|
|
88
|
|
|
/** @var FilterOperationsInterface $filerOps */ |
|
89
|
1 |
|
$filerOps = $container->get(FilterOperationsInterface::class); |
|
90
|
|
|
|
|
91
|
|
|
/** @var FormatterFactoryInterface $formatterFactory */ |
|
92
|
1 |
|
$formatterFactory = $container->get(FormatterFactoryInterface::class); |
|
93
|
1 |
|
$formatter = $formatterFactory->createFormatter(Messages::RESOURCES_NAMESPACE); |
|
94
|
|
|
|
|
95
|
1 |
|
return $factory->createRepository($connection, $modelSchemes, $filerOps, $formatter); |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
1 |
|
$container[PaginationStrategyInterface::class] = function (PsrContainerInterface $container) { |
|
99
|
1 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(FluteSettings::class); |
|
100
|
|
|
|
|
101
|
1 |
|
return new PaginationStrategy( |
|
102
|
1 |
|
$settings[FluteSettings::KEY_DEFAULT_PAGING_SIZE], |
|
103
|
1 |
|
$settings[FluteSettings::KEY_MAX_PAGING_SIZE] |
|
104
|
|
|
); |
|
105
|
|
|
}; |
|
106
|
|
|
|
|
107
|
1 |
|
$container[JsonApiValidatorFactoryInterface::class] = function (PsrContainerInterface $container) { |
|
108
|
1 |
|
$factory = new JsonApiValidatorFactory($container); |
|
109
|
|
|
|
|
110
|
1 |
|
return $factory; |
|
111
|
|
|
}; |
|
112
|
|
|
|
|
113
|
|
|
// register date/date time types |
|
114
|
1 |
|
if (Type::hasType(DateTimeJsonApiStringType::NAME) === false) { |
|
115
|
1 |
|
Type::addType(DateTimeJsonApiStringType::NAME, DateTimeJsonApiStringType::class); |
|
116
|
|
|
} |
|
117
|
1 |
|
if (Type::hasType(DateJsonApiStringType::NAME) === false) { |
|
118
|
1 |
|
Type::addType(DateJsonApiStringType::NAME, DateJsonApiStringType::class); |
|
119
|
|
|
} |
|
120
|
1 |
|
if (Type::hasType(JsonApiDateTimeType::NAME) === false) { |
|
121
|
1 |
|
Type::addType(JsonApiDateTimeType::NAME, JsonApiDateTimeType::class); |
|
122
|
|
|
} |
|
123
|
1 |
|
if (Type::hasType(JsonApiDateType::NAME) === false) { |
|
124
|
1 |
|
Type::addType(JsonApiDateType::NAME, JsonApiDateType::class); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param LimoncelloContainerInterface $container |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function configureExceptionHandler(LimoncelloContainerInterface $container) |
|
134
|
|
|
{ |
|
135
|
1 |
|
$container[ExceptionHandlerInterface::class] = function () { |
|
136
|
1 |
|
return new FluteExceptionHandler(); |
|
137
|
|
|
}; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|