-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add caching for
ObjectMapperTable
.
- Loading branch information
Showing
5 changed files
with
156 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## 0.6.1 | ||
|
||
* fix: Add caching for `ObjectMapperTable`. | ||
|
||
## 0.6.0 | ||
|
||
* style: Remove remnants. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/CustomMapper/Implementation/ObjectMapperTableWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\CustomMapper\Implementation; | ||
|
||
use Rekalogika\Mapper\CustomMapper\ObjectMapperTableFactoryInterface; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Symfony\Component\VarExporter\VarExporter; | ||
|
||
class ObjectMapperTableWarmer implements CacheWarmerInterface | ||
{ | ||
public function __construct( | ||
private ObjectMapperTableFactoryInterface $objectMapperTableFactory, | ||
private KernelInterface $kernel, | ||
) { | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
{ | ||
$mapping = VarExporter::export($this->objectMapperTableFactory->createObjectMapperTable()); | ||
file_put_contents($this->getCacheFilePath(), '<?php return ' . $mapping . ';'); | ||
|
||
return []; | ||
} | ||
|
||
private function getCacheFilePath(): string | ||
{ | ||
return $this->kernel->getBuildDir() . '/rekalogika_mapper_mapper_table.php'; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/CustomMapper/Implementation/WarmableObjectMapperTableFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\CustomMapper\Implementation; | ||
|
||
use Rekalogika\Mapper\CustomMapper\ObjectMapperTable; | ||
use Rekalogika\Mapper\CustomMapper\ObjectMapperTableFactoryInterface; | ||
use Rekalogika\Mapper\Exception\UnexpectedValueException; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Symfony\Component\VarExporter\VarExporter; | ||
|
||
class WarmableObjectMapperTableFactory implements | ||
ObjectMapperTableFactoryInterface, | ||
CacheWarmerInterface | ||
{ | ||
public const CACHE_FILE = 'rekalogika_mapper_mapper_table.php'; | ||
|
||
private ?ObjectMapperTable $objectMapperTableCache = null; | ||
|
||
public function __construct( | ||
private ObjectMapperTableFactoryInterface $decorated, | ||
private KernelInterface $kernel, | ||
) { | ||
} | ||
|
||
public function createObjectMapperTable(): ObjectMapperTable | ||
{ | ||
if ($this->objectMapperTableCache !== null) { | ||
return $this->objectMapperTableCache; | ||
} | ||
|
||
try { | ||
/** @psalm-suppress UnresolvableInclude */ | ||
$result = require $this->getCacheFilePath(); | ||
|
||
if (!$result instanceof ObjectMapperTable) { | ||
throw new UnexpectedValueException(); | ||
} | ||
|
||
return $this->objectMapperTableCache = $result; | ||
} catch (\Throwable) { | ||
@unlink($this->getCacheFilePath()); | ||
|
||
return $this->objectMapperTableCache = $this->warmUpAndGetResult(); | ||
} | ||
} | ||
|
||
private function warmUpAndGetResult(): ObjectMapperTable | ||
{ | ||
$this->warmUp($this->kernel->getCacheDir(), $this->kernel->getBuildDir()); | ||
|
||
return $this->decorated->createObjectMapperTable(); | ||
} | ||
|
||
public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
{ | ||
$mapping = VarExporter::export($this->decorated->createObjectMapperTable()); | ||
file_put_contents($this->getCacheFilePath(), '<?php return ' . $mapping . ';'); | ||
|
||
return []; | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
private function getCacheFilePath(): string | ||
{ | ||
return $this->kernel->getBuildDir() . '/' . self::CACHE_FILE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters