Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ jobs:
path: /tmp/build/logs/phpunit
continue-on-error: true
- name: Upload coverage results to Codecov
if: matrix.coverage
if: matrix.php.coverage
uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand Down
77 changes: 77 additions & 0 deletions src/Symfony/Bundle/CacheWarmer/PropertyInfoCacheWarmer.php
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());
}
}
78 changes: 78 additions & 0 deletions src/Symfony/Bundle/CacheWarmer/PropertyMetadataCacheWarmer.php
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;
}
}
23 changes: 22 additions & 1 deletion src/Symfony/Bundle/Resources/config/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation\DashPathSegmentNameGenerator;
use ApiPlatform\Metadata\Operation\UnderscorePathSegmentNameGenerator;
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Metadata\ResourceClassResolver;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\UriVariablesConverter;
Expand All @@ -42,6 +43,7 @@
use ApiPlatform\State\SerializerContextBuilderInterface;
use ApiPlatform\Symfony\Action\NotExposedAction;
use ApiPlatform\Symfony\Action\NotFoundAction;
use ApiPlatform\Symfony\Bundle\CacheWarmer\PropertyInfoCacheWarmer;
use ApiPlatform\Symfony\Routing\ApiLoader;
use ApiPlatform\Symfony\Routing\IriConverter;
use ApiPlatform\Symfony\Routing\Router;
Expand All @@ -50,6 +52,7 @@
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\Types\ContextFactory;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
Expand Down Expand Up @@ -107,9 +110,27 @@
->decorate('api_platform.property_info')
->args([
service('.inner'),
service('cache.property_info'),
service('api_platform.property_info.cache.adapter'),
]);

$services->set('api_platform.property_info.cache.adapter', PhpArrayAdapter::class)

Copy link
Copy Markdown
Member

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

->factory([PhpArrayAdapter::class, 'create'])
->args(['%kernel.build_dir%/api_platform_property_info.php', service('cache.property_info')]);

$services->set('api_platform.property_info.cache_warmer', PropertyInfoCacheWarmer::class)
->args([
service('api_platform.property_info.cache.inner'),
service('api_platform.metadata.resource.name_collection_factory'),
'%kernel.build_dir%/api_platform_property_info.php',
])
->tag('kernel.cache_warmer', ['priority' => 75]);

$services->set('api_platform.metadata.symfony_warmup_classes', ResourceNameCollection::class)
->factory([service('api_platform.metadata.resource.name_collection_factory'), 'create'])
->tag('property_info.warmup_class_provider')
->tag('serializer.warmup_class_provider')
->tag('validator.warmup_class_provider');

$services->set('api_platform.negotiator', Negotiator::class);

$services->set('api_platform.resource_class_resolver', ResourceClassResolver::class)
Expand Down
18 changes: 17 additions & 1 deletion src/Symfony/Bundle/Resources/config/metadata/property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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'),
]);

Expand All @@ -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'),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 cache.system and then needs the deleteItems() cleanup, can we inject api_platform.metadata.property.metadata_factory.cached.inner (and the name collection one) like you did with api_platform.property_info.cache.inner?

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 cache:warmup, when I edit an #[ApiProperty] the container rebuilds and cache.system gets a new namespace but api_platform_property_metadata.php stays the old file and the PhpArrayAdapter never asks the fallback. FrameworkBundle only wires its php array caches when !kernel.debug, can we do the same here?

->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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$services->set('api_platform.metadata.property.name_collection_factory.cached', CachedPropertyNameCollectionFactory::class)
->decorate('api_platform.metadata.property.name_collection_factory', null, -10)
->args([
service('api_platform.cache.metadata.property'),
service('api_platform.cache.metadata.property.php_array'),
service('api_platform.metadata.property.name_collection_factory.cached.inner'),
]);

Expand Down
112 changes: 112 additions & 0 deletions src/Symfony/Tests/Bundle/CacheWarmer/PropertyInfoCacheWarmerTest.php
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;
}
Loading
Loading