|
18 | 18 | use TypeLang\Mapper\DenormalizerInterface; |
19 | 19 | use TypeLang\Mapper\Mapper; |
20 | 20 | use TypeLang\Mapper\MapperInterface; |
21 | | -use TypeLang\Mapper\Mapping\MetadataProviderFactory; |
| 21 | +use TypeLang\Mapper\Mapping\MetadataBuilder; |
22 | 22 | use TypeLang\Mapper\Mapping\Provider\CacheMetadataProvider; |
23 | 23 | use TypeLang\Mapper\Mapping\Provider\MetadataProviderInterface; |
24 | 24 | use TypeLang\Mapper\Mapping\Reader\FileMetadataReader; |
|
60 | 60 | * selector: non-empty-string|null, |
61 | 61 | * cache: array{ |
62 | 62 | * enabled: bool|null, |
63 | | - * pool: non-empty-string|null, |
| 63 | + * pools: list<array{ |
| 64 | + * psr6: non-empty-string|null, |
| 65 | + * psr16: non-empty-string|null, |
| 66 | + * prefix: string|null, |
| 67 | + * }>, |
64 | 68 | * }, |
65 | 69 | * }, |
66 | 70 | * ... |
|
86 | 90 | * selector: non-empty-string|null, |
87 | 91 | * cache: array{ |
88 | 92 | * enabled: bool|null, |
89 | | - * pool: non-empty-string|null, |
| 93 | + * pools: list<array{ |
| 94 | + * psr6: non-empty-string|null, |
| 95 | + * psr16: non-empty-string|null, |
| 96 | + * prefix: string|null, |
| 97 | + * }>, |
90 | 98 | * }, |
91 | 99 | * }, |
92 | 100 | * mappers: array<non-empty-string, MapperSettingsType>, |
@@ -207,7 +215,7 @@ public static function getExtensionTargetId(string $name, ExtensionKind $kind): |
207 | 215 | ExtensionKind::ClassHook, |
208 | 216 | ExtensionKind::InstantiationStrategy, |
209 | 217 | ExtensionKind::AccessStrategy => self::getMapperId($name), |
210 | | - ExtensionKind::MetadataReader => self::getInternalId($name, 'metadata'), |
| 218 | + ExtensionKind::MetadataReader => self::getMetadataBuilderId($name), |
211 | 219 | }; |
212 | 220 | } |
213 | 221 |
|
@@ -350,18 +358,7 @@ private function registerMapper(string $name, array $settings, ?array $defaults |
350 | 358 | ?? null, |
351 | 359 | ])); |
352 | 360 |
|
353 | | - $selector = $settings['metadata']['selector'] ?? $defaults['metadata']['selector'] ?? null; |
354 | | - |
355 | | - $this->container->register(self::getInternalId($name, 'metadata'), MetadataProviderInterface::class) |
356 | | - ->setFactory([MetadataProviderFactory::class, 'create']) |
357 | | - ->setArguments(self::filterNulls([ |
358 | | - '$readers' => [], |
359 | | - '$strategy' => $this->createNamingStrategy($settings['naming'] ?? $defaults['naming'] ?? null), |
360 | | - '$cache' => $this->createCacheReference($settings, $defaults), |
361 | | - '$prefix' => CacheMetadataProvider::DEFAULT_PREFIX . $name . '_', |
362 | | - '$parser' => new Reference(self::PARSER_ID), |
363 | | - '$selector' => $selector === null ? null : new Reference($selector), |
364 | | - ])); |
| 361 | + $this->registerMetadata($name, $settings, $defaults); |
365 | 362 |
|
366 | 363 | $this->container->register(self::getMapperId($name), Mapper::class) |
367 | 364 | ->setArguments([ |
@@ -391,6 +388,52 @@ private function registerMapper(string $name, array $settings, ?array $defaults |
391 | 388 | return $extensions; |
392 | 389 | } |
393 | 390 |
|
| 391 | + /** |
| 392 | + * Returns the id of the builder of the metadata provider of the mapper. |
| 393 | + * |
| 394 | + * @param non-empty-string $name |
| 395 | + * @return non-empty-string |
| 396 | + */ |
| 397 | + public static function getMetadataBuilderId(string $name): string |
| 398 | + { |
| 399 | + return self::getInternalId($name, 'metadata_builder'); |
| 400 | + } |
| 401 | + |
| 402 | + /** |
| 403 | + * Registers the builder of the metadata provider of the mapper and the |
| 404 | + * provider built by it. The readers are added by |
| 405 | + * {@see MapperExtensionsPass}. |
| 406 | + * |
| 407 | + * @param non-empty-string $name |
| 408 | + * @param MapperSettingsType $settings |
| 409 | + * @param MapperSettingsType|null $defaults |
| 410 | + */ |
| 411 | + private function registerMetadata(string $name, array $settings, ?array $defaults): void |
| 412 | + { |
| 413 | + $builder = $this->container->register(self::getMetadataBuilderId($name), MetadataBuilder::class) |
| 414 | + ->setFactory([MetadataBuilder::class, 'createDefault']) |
| 415 | + ->setArguments(['$parser' => new Reference(self::PARSER_ID)]); |
| 416 | + |
| 417 | + $strategy = $this->createNamingStrategy($settings['naming'] ?? $defaults['naming'] ?? null); |
| 418 | + |
| 419 | + if ($strategy !== null) { |
| 420 | + $builder->addMethodCall('withNamingStrategy', [$strategy], true); |
| 421 | + } |
| 422 | + |
| 423 | + $selector = $settings['metadata']['selector'] ?? $defaults['metadata']['selector'] ?? null; |
| 424 | + |
| 425 | + if ($selector !== null) { |
| 426 | + $builder->addMethodCall('withReflectionReader', [new Reference($selector)], true); |
| 427 | + } |
| 428 | + |
| 429 | + foreach ($this->createCacheCalls($name, $settings, $defaults) as [$method, $arguments]) { |
| 430 | + $builder->addMethodCall($method, $arguments, true); |
| 431 | + } |
| 432 | + |
| 433 | + $this->container->register(self::getInternalId($name, 'metadata'), MetadataProviderInterface::class) |
| 434 | + ->setFactory([new Reference(self::getMetadataBuilderId($name)), 'build']); |
| 435 | + } |
| 436 | + |
394 | 437 | /** |
395 | 438 | * Registers a reader per mapping config file, returns their ids in the |
396 | 439 | * order of the paths, the files of a directory by the name. |
@@ -526,33 +569,57 @@ private function createNamingStrategy(?string $name): Definition|Reference|null |
526 | 569 | } |
527 | 570 |
|
528 | 571 | /** |
529 | | - * Returns the cache of the metadata, {@see null} in case of it is |
530 | | - * disabled. The default pool is skipped in case of it is not declared, |
531 | | - * a pool given in the config or a cache enabled in the config is |
532 | | - * required. |
| 572 | + * Returns the calls of the builder adding the caches of the metadata, |
| 573 | + * none in case of the cache is disabled. The pools of a named mapper |
| 574 | + * replace the ones of the default mapper. Without pools the default |
| 575 | + * pool is used, it is skipped in case of it is not declared and the |
| 576 | + * cache is not enabled in the config. |
533 | 577 | * |
| 578 | + * @param non-empty-string $name |
534 | 579 | * @param MapperSettingsType $settings |
535 | 580 | * @param MapperSettingsType|null $defaults |
| 581 | + * @return list<array{non-empty-string, list<mixed>}> |
536 | 582 | */ |
537 | | - private function createCacheReference(array $settings, ?array $defaults): ?Reference |
| 583 | + private function createCacheCalls(string $name, array $settings, ?array $defaults): array |
538 | 584 | { |
539 | 585 | $isEnabled = $settings['metadata']['cache']['enabled'] |
540 | 586 | ?? $defaults['metadata']['cache']['enabled'] |
541 | 587 | ?? null; |
542 | 588 |
|
543 | | - $pool = $settings['metadata']['cache']['pool'] |
544 | | - ?? $defaults['metadata']['cache']['pool'] |
545 | | - ?? null; |
546 | | - |
547 | 589 | if ($isEnabled === false || ($isEnabled === null && $this->isDebug())) { |
548 | | - return null; |
| 590 | + return []; |
| 591 | + } |
| 592 | + |
| 593 | + $prefix = CacheMetadataProvider::DEFAULT_PREFIX . $name . '_'; |
| 594 | + $pools = $settings['metadata']['cache']['pools']; |
| 595 | + |
| 596 | + if ($pools === [] && $defaults !== null) { |
| 597 | + $pools = $defaults['metadata']['cache']['pools']; |
549 | 598 | } |
550 | 599 |
|
551 | | - if ($isEnabled === null && $pool === null) { |
552 | | - return new Reference(self::DEFAULT_CACHE_POOL, ContainerInterface::NULL_ON_INVALID_REFERENCE); |
| 600 | + if ($pools === [] && $isEnabled === null) { |
| 601 | + $pool = new Reference(self::DEFAULT_CACHE_POOL, ContainerInterface::IGNORE_ON_INVALID_REFERENCE); |
| 602 | + |
| 603 | + return [['withPsr6Cache', [$pool, $prefix]]]; |
553 | 604 | } |
554 | 605 |
|
555 | | - return new Reference($pool ?? self::DEFAULT_CACHE_POOL); |
| 606 | + if ($pools === []) { |
| 607 | + return [['withPsr6Cache', [new Reference(self::DEFAULT_CACHE_POOL), $prefix]]]; |
| 608 | + } |
| 609 | + |
| 610 | + $result = []; |
| 611 | + |
| 612 | + foreach ($pools as $pool) { |
| 613 | + if ($pool['psr6'] !== null) { |
| 614 | + $result[] = ['withPsr6Cache', [new Reference($pool['psr6']), $pool['prefix'] ?? $prefix]]; |
| 615 | + } |
| 616 | + |
| 617 | + if ($pool['psr16'] !== null) { |
| 618 | + $result[] = ['withPsr16Cache', [new Reference($pool['psr16']), $pool['prefix'] ?? $prefix]]; |
| 619 | + } |
| 620 | + } |
| 621 | + |
| 622 | + return $result; |
556 | 623 | } |
557 | 624 |
|
558 | 625 | private function isDebug(): bool |
|
0 commit comments