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
12 changes: 11 additions & 1 deletion src/Metadata/Extractor/AbstractPropertyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Base file extractor.
*
* @author Vincent Chalamon <vincentchalamon@gmail.com>
*/
abstract class AbstractPropertyExtractor implements PropertyExtractorInterface
abstract class AbstractPropertyExtractor implements PropertyExtractorInterface, ResetInterface
{
protected ?array $properties = null;
private array $collectedParameters = [];
Expand Down Expand Up @@ -55,6 +56,15 @@ public function getProperties(): array
*/
abstract protected function extractPath(string $path): void;

/**
* {@inheritdoc}
*/
public function reset(): void
{
$this->properties = null;
$this->collectedParameters = [];
}

/**
* Recursively replaces placeholders with the service container parameters.
*
Expand Down
12 changes: 11 additions & 1 deletion src/Metadata/Extractor/AbstractResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Base file extractor.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
abstract class AbstractResourceExtractor implements ResourceExtractorInterface
abstract class AbstractResourceExtractor implements ResourceExtractorInterface, ResetInterface
{
protected ?array $resources = null;
private array $collectedParameters = [];
Expand Down Expand Up @@ -55,6 +56,15 @@ public function getResources(): array
*/
abstract protected function extractPath(string $path): void;

/**
* {@inheritdoc}
*/
public function reset(): void
{
$this->resources = null;
$this->collectedParameters = [];
}

/**
* Recursively replaces placeholders with the service container parameters.
*
Expand Down
12 changes: 11 additions & 1 deletion src/Metadata/ResourceClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use Symfony\Contracts\Service\ResetInterface;

/**
* {@inheritdoc}
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
final class ResourceClassResolver implements ResourceClassResolverInterface
final class ResourceClassResolver implements ResourceClassResolverInterface, ResetInterface
{
use ClassInfoTrait;
private array $localIsResourceClassCache = [];
Expand Down Expand Up @@ -105,4 +106,13 @@ public function isResourceClass(string $type): bool

return $this->localIsResourceClassCache[$type] = false;
}

/**
* {@inheritdoc}
*/
public function reset(): void
{
$this->localIsResourceClassCache = [];
$this->localMostSpecificResourceClassCache = [];
}
}
43 changes: 43 additions & 0 deletions src/Metadata/Tests/Extractor/YamlPropertyExtractorResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Metadata\Tests\Extractor;

use ApiPlatform\Metadata\Extractor\AbstractPropertyExtractor;
use ApiPlatform\Metadata\Extractor\YamlPropertyExtractor;
use PHPUnit\Framework\TestCase;

class YamlPropertyExtractorResetTest extends TestCase
{
public function testReset(): void
{
$extractor = new YamlPropertyExtractor([]);

$refl = new \ReflectionClass($extractor);
$properties = $refl->getProperty('properties');
$properties->setAccessible(true);
$properties->setValue($extractor, ['foo' => 'bar']);

$collectedParameters = new \ReflectionProperty(AbstractPropertyExtractor::class, 'collectedParameters');
$collectedParameters->setAccessible(true);
$collectedParameters->setValue($extractor, ['param' => 'value']);

$this->assertNotEmpty($properties->getValue($extractor));
$this->assertNotEmpty($collectedParameters->getValue($extractor));

$extractor->reset();

$this->assertNull($properties->getValue($extractor));
$this->assertEmpty($collectedParameters->getValue($extractor));
}
}
43 changes: 43 additions & 0 deletions src/Metadata/Tests/Extractor/YamlResourceExtractorResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Metadata\Tests\Extractor;

use ApiPlatform\Metadata\Extractor\AbstractResourceExtractor;
use ApiPlatform\Metadata\Extractor\YamlResourceExtractor;
use PHPUnit\Framework\TestCase;

class YamlResourceExtractorResetTest extends TestCase
{
public function testReset(): void
{
$extractor = new YamlResourceExtractor([]);

$refl = new \ReflectionClass($extractor);
$resources = $refl->getProperty('resources');
$resources->setAccessible(true);
$resources->setValue($extractor, ['foo' => 'bar']);

$collectedParameters = new \ReflectionProperty(AbstractResourceExtractor::class, 'collectedParameters');
$collectedParameters->setAccessible(true);
$collectedParameters->setValue($extractor, ['param' => 'value']);

$this->assertNotEmpty($resources->getValue($extractor));
$this->assertNotEmpty($collectedParameters->getValue($extractor));

$extractor->reset();

$this->assertNull($resources->getValue($extractor));
$this->assertEmpty($collectedParameters->getValue($extractor));
}
}
43 changes: 43 additions & 0 deletions src/Metadata/Tests/ResourceClassResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Metadata\Tests;

use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Metadata\ResourceClassResolver;
use PHPUnit\Framework\TestCase;

class ResourceClassResolverTest extends TestCase
{
public function testReset(): void
{
$resourceNameCollectionFactoryProphecy = $this->createMock(ResourceNameCollectionFactoryInterface::class);
$resourceNameCollectionFactoryProphecy->method('create')->willReturn(new ResourceNameCollection([ \stdClass::class ]));

$resolver = new ResourceClassResolver($resourceNameCollectionFactoryProphecy);

$this->assertTrue($resolver->isResourceClass(\stdClass::class));

$resolver->reset();

$refl = new \ReflectionClass($resolver);
$localIsResourceClassCache = $refl->getProperty('localIsResourceClassCache');
$localIsResourceClassCache->setAccessible(true);
$localMostSpecificResourceClassCache = $refl->getProperty('localMostSpecificResourceClassCache');
$localMostSpecificResourceClassCache->setAccessible(true);

$this->assertEmpty($localIsResourceClassCache->getValue($resolver));
$this->assertEmpty($localMostSpecificResourceClassCache->getValue($resolver));
}
}
Loading