Skip to content

Commit

Permalink
simplify function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
usu committed Dec 23, 2023
1 parent 74f1259 commit 6550208
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 39 deletions.
11 changes: 8 additions & 3 deletions src/JsonApi/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,23 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel
return $normalizedRelatedObject;
}

$data = [
$context['data'] = [
'data' => [
'type' => $this->getResourceShortName($resourceClass),
'id' => $iri,
],
];

$context['data'] = $iri;
$context['object'] = $relatedObject;
unset($context['property_metadata']);
unset($context['api_attribute']);

if ($this->tagCollector) {
$this->tagCollector->collect($relatedObject, $format, $context, $iri, $data);
$this->tagCollector->collect($context);
}

return $data;
return $context['data'];
}

/**
Expand Down
32 changes: 25 additions & 7 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,27 @@ public function normalize(mixed $object, string $format = null, array $context =
$context['resources'][$iri] = $iri;
}

$context['object'] = $object;
$context['format'] = $format;

$data = parent::normalize($object, $format, $context);

$context['data'] = $data;
unset($context['property_metadata']);
unset($context['api_attribute']);

if ($emptyResourceAsIri && \is_array($data) && 0 === \count($data)) {
$context['data'] = $iri;

if ($this->tagCollector) {
$this->tagCollector->collect($object, $format, $context, $iri, $iri);
$this->tagCollector->collect($context);
}

return $iri;
}

if ($this->tagCollector) {
$this->tagCollector->collect($object, $format, $context, $iri, $data);
$this->tagCollector->collect($context);
}

return $data;
Expand Down Expand Up @@ -641,7 +650,7 @@ protected function getFactoryOptions(array $context): array
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []): mixed
{
$context['api_attribute'] = $attribute;
$propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $this->getFactoryOptions($context));
$context['property_metadata'] = $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $this->getFactoryOptions($context));

if ($context['api_denormalize'] ?? false) {
return $this->propertyAccessor->getValue($object, $attribute);
Expand Down Expand Up @@ -679,9 +688,11 @@ protected function getAttributeValue(object $object, string $attribute, string $
$resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);

$data = $this->normalizeCollectionOfRelations($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
$context['data'] = $data;
$context['type'] = $type;

if ($this->tagCollector) {
$this->tagCollector->collect($object, $format, $context, $context['iri'], $data, $attribute, $propertyMetadata, $type);
$this->tagCollector->collect($context);
}

return $data;
Expand Down Expand Up @@ -712,9 +723,11 @@ protected function getAttributeValue(object $object, string $attribute, string $
$resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);

$data = $this->normalizeRelation($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
$context['data'] = $data;
$context['type'] = $type;

if ($this->tagCollector) {
$this->tagCollector->collect($object, $format, $context, $context['iri'], $data, $attribute, $propertyMetadata, $type);
$this->tagCollector->collect($context);
}

return $data;
Expand Down Expand Up @@ -809,10 +822,15 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel
return $normalizedRelatedObject;
}

$iri = $this->iriConverter->getIriFromResource(resource: $relatedObject, context: $context);
$context['iri'] = $iri = $this->iriConverter->getIriFromResource(resource: $relatedObject, context: $context);

$context['data'] = $iri;
$context['object'] = $relatedObject;
unset($context['property_metadata']);
unset($context['api_attribute']);

if ($this->tagCollector) {
$this->tagCollector->collect($relatedObject, $format, $context, $iri, $iri);
$this->tagCollector->collect($context);
} elseif (isset($context['resources'])) {
$context['resources'][$iri] = $iri;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Serializer/TagCollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@

namespace ApiPlatform\Serializer;

use ApiPlatform\Metadata\ApiProperty;
use Symfony\Component\PropertyInfo\Type;

/**
* Interface for collecting cache tags during normalization.
*
* @author Urban Suppiger <urban@suppiger.net>
*/
interface TagCollectorInterface
{
public function collect(mixed $object = null, string $format = null, array $context = [], string $iri = null, mixed $data = null, string $attribute = null, ApiProperty $propertyMetadata = null, Type $type = null): void;
public function collect(array $context = []): void;
}
38 changes: 25 additions & 13 deletions tests/Behat/HttpCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

namespace ApiPlatform\Tests\Behat;

use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use PHPUnit\Framework\ExpectationFailedException;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Context\Context;
use ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollectorDefault;
use ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollectorCustom;
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\Driver\BrowserKitDriver;
use Behat\MinkExtension\Context\MinkContext;
use FriendsOfBehat\SymfonyExtension\Context\Environment\InitializedSymfonyExtensionEnvironment;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand Down Expand Up @@ -57,15 +59,25 @@ public function irisShouldBePurged(string $iris): void

/**
* this is necessary to allow overriding services
* see https://github.com/FriendsOfBehat/SymfonyExtension/issues/149 for details
* see https://github.com/FriendsOfBehat/SymfonyExtension/issues/149 for details.
*/
private function disableReboot(BeforeScenarioScope $scope){
private function disableReboot(BeforeScenarioScope $scope): void
{
$env = $scope->getEnvironment();
if (!$env instanceof InitializedSymfonyExtensionEnvironment) {
return;
}

/** @var MinkContext $minkContext */
$minkContext = $scope->getEnvironment()->getContext(MinkContext::class);
$client = $minkContext->getSession()->getDriver()->getClient();
$client->disableReboot();
$driver = $env->getContext(MinkContext::class)->getSession()->getDriver();
if (!$driver instanceof BrowserKitDriver) {
return;
}

$client = $driver->getClient();
if (!$client instanceof KernelBrowser) {
return;
}

$client->disableReboot();
}
}
12 changes: 7 additions & 5 deletions tests/Fixtures/TestBundle/HttpCache/TagCollectorCustom.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Serializer\TagCollectorInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelationEmbedder;
use Symfony\Component\PropertyInfo\Type;

/**
* Collects cache tags during normalization.
Expand All @@ -27,15 +26,18 @@ class TagCollectorCustom implements TagCollectorInterface
{
public const IRI_RELATION_DELIMITER = '#';

public function collect(mixed $object = null, string $format = null, array $context = [], string $iri = null, mixed $data = null, string $attribute = null, ApiProperty $propertyMetadata = null, Type $type = null): void
public function collect(array $context = []): void
{
$iri = $context['iri'];
$object = $context['object'];

if ($object instanceof RelationEmbedder) {
$iri = '/RE/'.$object->id;
}

if ($attribute) {
$this->addCacheTagsForRelation($context, $iri, $propertyMetadata);
} elseif (\is_array($data)) {
if (isset($context['property_metadata'])) {
$this->addCacheTagsForRelation($context, $iri, $context['property_metadata']);
} elseif (\is_array($context['data'])) {
$this->addCacheTagForResource($context, $iri);
}
}
Expand Down
13 changes: 6 additions & 7 deletions tests/Fixtures/TestBundle/HttpCache/TagCollectorDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

namespace ApiPlatform\Tests\Fixtures\TestBundle\HttpCache;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Serializer\TagCollectorInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelationEmbedder;
use Symfony\Component\PropertyInfo\Type;

/**
* Collects cache tags during normalization.
Expand All @@ -25,15 +22,17 @@
*/
class TagCollectorDefault implements TagCollectorInterface
{
public function collect(mixed $object = null, string $format = null, array $context = [], string $iri = null, mixed $data = null, string $attribute = null, ApiProperty $propertyMetadata = null, Type $type = null): void
public function collect(array $context = []): void
{
if (!$attribute) {
$this->addResourceToContext($context, $iri);
if (!isset($context['property_metadata'])) {
$this->addResourceToContext($context);
}
}

private function addResourceToContext(array $context, ?string $iri): void
private function addResourceToContext(array $context): void
{
$iri = $context['iri'];

if (isset($context['resources']) && isset($iri)) {
$context['resources'][$iri] = $iri;
}
Expand Down

0 comments on commit 6550208

Please sign in to comment.