Repository navigation
perf(symfony): warm metadata for API resources #8847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a017bda
00a0f1f
0d52fcb
8f835ee
a14097d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Symfony\Bundle\CacheWarmer; | ||
|
|
||
| use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
| use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
| use Symfony\Component\Cache\Adapter\NullAdapter; | ||
| use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
| use Symfony\Component\Config\Resource\ClassExistenceResource; | ||
| use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
| use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor; | ||
| use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; | ||
|
|
||
| final class PropertyInfoCacheWarmer implements CacheWarmerInterface | ||
| { | ||
| public function __construct( | ||
| private readonly PropertyInfoExtractorInterface $extractor, | ||
| private readonly ResourceNameCollectionFactoryInterface $resourceNames, | ||
| private readonly string $file, | ||
| ) { | ||
| } | ||
|
|
||
| public function isOptional(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
| { | ||
| if (null === $buildDir) { | ||
| return []; | ||
| } | ||
|
|
||
| $pool = new ArrayAdapter(0, false); | ||
| $extractor = new PropertyInfoCacheExtractor($this->extractor, $pool); | ||
|
|
||
| spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']); | ||
| try { | ||
| foreach ($this->resourceNames->create() as $class) { | ||
| try { | ||
| $properties = array_unique(array_merge( | ||
| $extractor->getProperties($class) ?? [], | ||
| $extractor->getProperties($class, ['serializer_groups' => null]) ?? [], | ||
| )); | ||
|
|
||
| foreach ($properties as $property) { | ||
| $extractor->getType($class, $property); | ||
| $extractor->getShortDescription($class, $property); | ||
| $extractor->isReadable($class, $property); | ||
| $extractor->isWritable($class, $property); | ||
| $extractor->isInitializable($class, $property); | ||
| } | ||
| } catch (\Exception $e) { | ||
| try { | ||
| ClassExistenceResource::throwOnRequiredClass($class, $e); | ||
| } catch (\ReflectionException) { | ||
| } | ||
| } | ||
| } | ||
| } finally { | ||
| spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']); | ||
| } | ||
|
|
||
| return (new PhpArrayAdapter($this->file, new NullAdapter()))->warmUp($pool->getValues()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Symfony\Bundle\CacheWarmer; | ||
|
|
||
| use ApiPlatform\Metadata\Property\Factory\CachedPropertyMetadataFactory; | ||
| use ApiPlatform\Metadata\Property\Factory\CachedPropertyNameCollectionFactory; | ||
| use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
| use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; | ||
| use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
| use Psr\Cache\CacheItemPoolInterface; | ||
| use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
| use Symfony\Component\Cache\Adapter\NullAdapter; | ||
| use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
| use Symfony\Component\Config\Resource\ClassExistenceResource; | ||
| use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
|
|
||
| final class PropertyMetadataCacheWarmer implements CacheWarmerInterface | ||
| { | ||
| public function __construct( | ||
| private readonly ResourceNameCollectionFactoryInterface $resources, | ||
| private readonly PropertyNameCollectionFactoryInterface $names, | ||
| private readonly PropertyMetadataFactoryInterface $metadata, | ||
| private readonly CacheItemPoolInterface $fallbackPool, | ||
| private readonly string $file, | ||
| ) { | ||
| } | ||
|
|
||
| public function isOptional(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
| { | ||
| if (null === $buildDir) { | ||
| return []; | ||
| } | ||
|
|
||
| $pool = new ArrayAdapter(0, false); | ||
| $names = new CachedPropertyNameCollectionFactory($pool, $this->names); | ||
| $metadata = new CachedPropertyMetadataFactory($pool, $this->metadata); | ||
|
|
||
| spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']); | ||
| try { | ||
| foreach ($this->resources->create() as $class) { | ||
| try { | ||
| foreach ($names->create($class) as $property) { | ||
| $metadata->create($class, $property); | ||
| } | ||
| } catch (\Exception $e) { | ||
| try { | ||
| ClassExistenceResource::throwOnRequiredClass($class, $e); | ||
| } catch (\ReflectionException) { | ||
| } | ||
| } | ||
| } | ||
| } finally { | ||
| spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']); | ||
| } | ||
|
|
||
| $values = $pool->getValues(); | ||
| $preload = (new PhpArrayAdapter($this->file, new NullAdapter()))->warmUp($values); | ||
| $this->fallbackPool->deleteItems(array_keys($values)); | ||
|
|
||
| return $preload; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |
| use ApiPlatform\Metadata\Property\Factory\PropertyInfoPropertyMetadataFactory; | ||
| use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
| use ApiPlatform\Metadata\Property\Factory\SerializerPropertyMetadataFactory; | ||
| use ApiPlatform\Symfony\Bundle\CacheWarmer\PropertyMetadataCacheWarmer; | ||
| use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
|
|
||
| return function (ContainerConfigurator $container) { | ||
| $services = $container->services(); | ||
|
|
@@ -51,7 +53,7 @@ | |
| $services->set('api_platform.metadata.property.metadata_factory.cached', CachedPropertyMetadataFactory::class) | ||
| ->decorate('api_platform.metadata.property.metadata_factory', null, -10) | ||
| ->args([ | ||
| service('api_platform.cache.metadata.property'), | ||
| service('api_platform.cache.metadata.property.php_array'), | ||
| service('api_platform.metadata.property.metadata_factory.cached.inner'), | ||
| ]); | ||
|
|
||
|
|
@@ -69,6 +71,20 @@ | |
| $services->set('api_platform.metadata.property.metadata_factory.xml', ExtractorPropertyMetadataFactory::class) | ||
| ->args([service('api_platform.metadata.property_extractor.xml')]); | ||
|
|
||
| $services->set('api_platform.metadata.property.cache_warmer', PropertyMetadataCacheWarmer::class) | ||
| ->args([ | ||
| service('api_platform.metadata.resource.name_collection_factory'), | ||
| service('api_platform.metadata.property.name_collection_factory'), | ||
| service('api_platform.metadata.property.metadata_factory'), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the decorated factory so warming goes through the php_array pool, writes into |
||
| service('api_platform.cache.metadata.property'), | ||
| '%kernel.build_dir%/api_platform_property_metadata.php', | ||
| ]) | ||
| ->tag('kernel.cache_warmer', ['priority' => -10]); | ||
|
|
||
| $services->set('api_platform.cache.metadata.property.php_array', PhpArrayAdapter::class) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in debug this goes stale no? the warmer is optional so it only runs on |
||
| ->factory([PhpArrayAdapter::class, 'create']) | ||
| ->args(['%kernel.build_dir%/api_platform_property_metadata.php', service('api_platform.cache.metadata.property')]); | ||
|
|
||
| $services->set('api_platform.cache.metadata.property') | ||
| ->parent('cache.system') | ||
| ->tag('cache.pool'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Symfony\Tests\Bundle\CacheWarmer; | ||
|
|
||
| use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
| use ApiPlatform\Metadata\Resource\ResourceNameCollection; | ||
| use ApiPlatform\Symfony\Bundle\CacheWarmer\PropertyInfoCacheWarmer; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
| use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
| use Symfony\Component\Filesystem\Filesystem; | ||
| use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; | ||
| use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor; | ||
| use Symfony\Component\PropertyInfo\PropertyInfoExtractor; | ||
|
|
||
| final class PropertyInfoCacheWarmerTest extends TestCase | ||
| { | ||
| private string $cacheDir; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->cacheDir = sys_get_temp_dir().'/api_platform_property_info_warmer_'.bin2hex(random_bytes(4)); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| (new Filesystem())->remove($this->cacheDir); | ||
| } | ||
|
|
||
| public function testWarmsResourcePropertiesAndKeepsTheFallback(): void | ||
| { | ||
| $reflection = new ReflectionExtractor(); | ||
| $extractor = new PropertyInfoExtractor([$reflection], [$reflection], [], [$reflection], [$reflection]); | ||
| $resourceNames = $this->createMock(ResourceNameCollectionFactoryInterface::class); | ||
| $resourceNames->expects($this->once())->method('create')->willReturn(new ResourceNameCollection([PropertyInfoWarmupSubject::class])); | ||
| $file = $this->cacheDir.'/build/property_info.php'; | ||
|
|
||
| $warmer = new PropertyInfoCacheWarmer($extractor, $resourceNames, $file); | ||
| $warmer->warmUp($this->cacheDir, $this->cacheDir.'/build'); | ||
|
|
||
| $this->assertFileExists($file); | ||
|
|
||
| $fallback = new ArrayAdapter(); | ||
| $cached = new PropertyInfoCacheExtractor($extractor, PhpArrayAdapter::create($file, $fallback)); | ||
|
|
||
| $this->assertContains('name', $cached->getProperties(PropertyInfoWarmupSubject::class)); | ||
| $this->assertContains('name', $cached->getProperties(PropertyInfoWarmupSubject::class, ['serializer_groups' => null])); | ||
| $this->assertEquals($extractor->getType(PropertyInfoWarmupSubject::class, 'name'), $cached->getType(PropertyInfoWarmupSubject::class, 'name')); | ||
| $this->assertNull($cached->getShortDescription(PropertyInfoWarmupSubject::class, 'name')); | ||
| $this->assertSame($extractor->isReadable(PropertyInfoWarmupSubject::class, 'name'), $cached->isReadable(PropertyInfoWarmupSubject::class, 'name')); | ||
| $this->assertSame($extractor->isWritable(PropertyInfoWarmupSubject::class, 'name'), $cached->isWritable(PropertyInfoWarmupSubject::class, 'name')); | ||
| $this->assertSame($extractor->isInitializable(PropertyInfoWarmupSubject::class, 'name'), $cached->isInitializable(PropertyInfoWarmupSubject::class, 'name')); | ||
|
|
||
| foreach (['getType', 'getShortDescription', 'isReadable', 'isWritable', 'isInitializable'] as $method) { | ||
| $key = rawurlencode($method.'.'.serialize([PropertyInfoWarmupSubject::class, 'name', []])); | ||
| $this->assertFalse($fallback->hasItem($key), $method.' must use the warmed cache.'); | ||
| } | ||
|
|
||
| $cached->getShortDescription(PropertyInfoUnknownSubject::class, 'name'); | ||
| $key = rawurlencode('getShortDescription.'.serialize([PropertyInfoUnknownSubject::class, 'name', []])); | ||
| $this->assertTrue($fallback->hasItem($key)); | ||
| } | ||
|
|
||
| public function testSkipsWarmupWithoutBuildDirectory(): void | ||
| { | ||
| $resourceNames = $this->createMock(ResourceNameCollectionFactoryInterface::class); | ||
| $resourceNames->expects($this->never())->method('create'); | ||
| $file = $this->cacheDir.'/build/property_info.php'; | ||
| $warmer = new PropertyInfoCacheWarmer(new PropertyInfoExtractor(), $resourceNames, $file); | ||
|
|
||
| $this->assertTrue($warmer->isOptional()); | ||
| $this->assertSame([], $warmer->warmUp($this->cacheDir)); | ||
| $this->assertFileDoesNotExist($file); | ||
| } | ||
|
|
||
| public function testMissingResourceDoesNotStopWarmup(): void | ||
| { | ||
| $reflection = new ReflectionExtractor(); | ||
| $extractor = new PropertyInfoExtractor([$reflection], [$reflection], [], [$reflection], [$reflection]); | ||
| $resourceNames = $this->createMock(ResourceNameCollectionFactoryInterface::class); | ||
| $resourceNames->expects($this->once())->method('create')->willReturn(new ResourceNameCollection([__NAMESPACE__.'\\PropertyInfoMissingSubject', PropertyInfoWarmupSubject::class])); | ||
| $file = $this->cacheDir.'/build/property_info.php'; | ||
|
|
||
| (new PropertyInfoCacheWarmer($extractor, $resourceNames, $file))->warmUp($this->cacheDir, $this->cacheDir.'/build'); | ||
|
|
||
| $fallback = new ArrayAdapter(); | ||
| $cached = new PropertyInfoCacheExtractor($extractor, PhpArrayAdapter::create($file, $fallback)); | ||
| $this->assertContains('name', $cached->getProperties(PropertyInfoWarmupSubject::class)); | ||
| $this->assertSame([], $fallback->getValues()); | ||
| } | ||
| } | ||
|
|
||
| final class PropertyInfoWarmupSubject | ||
| { | ||
| public string $name; | ||
| } | ||
|
|
||
| final class PropertyInfoUnknownSubject | ||
| { | ||
| public string $name; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here for
api_platform_property_info.php