Skip to content

Commit 48b32f2

Browse files
committed
refactor(MappingFactory): Separate cache warmer from WarmableMappingFactory.
1 parent eca1bf7 commit 48b32f2

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* fix(`ObjectToObjectMetadataFactory`): If the target is not writable, skip the
1414
mapping.
1515
* feat(`Profiler`): Show mapping table.
16+
* refactor(`MappingFactory`): Separate cache warmer from
17+
`WarmableMappingFactory`.
1618

1719
## 0.7.2
1820

config/services.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Rekalogika\Mapper\MainTransformer\MainTransformer;
2323
use Rekalogika\Mapper\Mapper;
2424
use Rekalogika\Mapper\MapperInterface;
25+
use Rekalogika\Mapper\Mapping\Implementation\MappingCacheWarmer;
2526
use Rekalogika\Mapper\Mapping\Implementation\MappingFactory;
2627
use Rekalogika\Mapper\Mapping\Implementation\WarmableMappingFactory;
2728
use Rekalogika\Mapper\Mapping\MappingFactoryInterface;
@@ -201,10 +202,18 @@
201202

202203
$services
203204
->set('rekalogika.mapper.mapping_factory.caching', WarmableMappingFactory::class)
204-
->decorate('rekalogika.mapper.mapping_factory', null, 500)
205+
->decorate('rekalogika.mapper.mapping_factory', null, 100)
205206
->args([
206-
service('rekalogika.mapper.mapping_factory.caching.inner'),
207+
service('.inner'),
207208
service('kernel')
209+
]);
210+
211+
# mapping cache warmer
212+
213+
$services
214+
->set('rekalogika.mapper.mapping_factory.warmer', MappingCacheWarmer::class)
215+
->args([
216+
service('rekalogika.mapper.mapping_factory.caching'),
208217
])
209218
->tag('kernel.cache_warmer');
210219

src/DependencyInjection/CompilerPass/DebugPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function process(ContainerBuilder $container)
6767
$serviceId = 'rekalogika.mapper.mapping_factory';
6868
$decoratedService = $container->getDefinition($serviceId);
6969
$container->register('debug.' . $serviceId, TraceableMappingFactory::class)
70-
->setDecoratedService($serviceId, null, 100)
70+
->setDecoratedService($serviceId, null, 50)
7171
->setArguments([
7272
$decoratedService,
7373
$dataCollector,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Mapping\Implementation;
15+
16+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
17+
18+
final class MappingCacheWarmer implements CacheWarmerInterface
19+
{
20+
public function __construct(
21+
private WarmableMappingFactory $warmableMappingFactory,
22+
) {
23+
}
24+
25+
public function isOptional(): bool
26+
{
27+
return false;
28+
}
29+
30+
public function warmUp(string $cacheDir, ?string $buildDir = null): array
31+
{
32+
$this->warmableMappingFactory->warmUp();
33+
34+
return [];
35+
}
36+
}

src/Mapping/Implementation/WarmableMappingFactory.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
use Rekalogika\Mapper\Exception\UnexpectedValueException;
1717
use Rekalogika\Mapper\Mapping\Mapping;
1818
use Rekalogika\Mapper\Mapping\MappingFactoryInterface;
19-
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
2019
use Symfony\Component\HttpKernel\KernelInterface;
2120
use Symfony\Component\VarExporter\VarExporter;
2221

23-
final class WarmableMappingFactory implements
24-
MappingFactoryInterface,
25-
CacheWarmerInterface
22+
final class WarmableMappingFactory implements MappingFactoryInterface
2623
{
2724
public const CACHE_FILE = 'rekalogika_mapper_mapping.php';
2825

@@ -41,7 +38,7 @@ private function getMappingFromInnerFactory(): Mapping
4138

4239
private function warmUpAndGetMapping(): Mapping
4340
{
44-
$this->warmUp($this->kernel->getCacheDir(), $this->kernel->getBuildDir());
41+
$this->warmUp();
4542

4643
return $this->getMappingFromInnerFactory();
4744
}
@@ -75,21 +72,14 @@ public function getMapping(): Mapping
7572
return $this->mapping = $result;
7673
}
7774

78-
public function isOptional(): bool
79-
{
80-
return false;
81-
}
82-
8375
private function getCacheFilePath(): string
8476
{
8577
return $this->kernel->getBuildDir() . '/' . self::CACHE_FILE;
8678
}
8779

88-
public function warmUp(string $cacheDir, ?string $buildDir = null): array
80+
public function warmUp(): void
8981
{
9082
$mapping = VarExporter::export($this->realMappingFactory->getMapping());
9183
file_put_contents($this->getCacheFilePath(), '<?php return ' . $mapping . ';');
92-
93-
return [];
9484
}
9585
}

0 commit comments

Comments
 (0)