From ad7b9c2ede1b7f433ff95bd0e8e427c16ebea3fe Mon Sep 17 00:00:00 2001 From: Jarek Jakubowski Date: Sun, 14 Apr 2019 01:08:08 +0200 Subject: [PATCH 1/2] Fix integration with API Platform 2.4, the other way around --- src/Content/MediaFactory.php | 2 +- src/Entity/Content.php | 2 +- src/Entity/ContentExtrasTrait.php | 31 --------- src/Repository/ContentRepository.php | 2 +- src/Repository/FieldRepository.php | 2 +- src/Twig/JsonExtension.php | 64 +++++++++++++++++++ .../_partials/_content_listing.html.twig | 2 +- 7 files changed, 69 insertions(+), 36 deletions(-) create mode 100644 src/Twig/JsonExtension.php diff --git a/src/Content/MediaFactory.php b/src/Content/MediaFactory.php index 8ad99d318..9702d141e 100644 --- a/src/Content/MediaFactory.php +++ b/src/Content/MediaFactory.php @@ -92,7 +92,7 @@ private function updateImageDimensions(Media $media, SplFileInfo $file): void return; } - $size = getimagesize($file->getRealpath()); + $size = @getimagesize($file->getRealpath()); if ($size !== false) { $media->setWidth($size[0]) diff --git a/src/Entity/Content.php b/src/Entity/Content.php index 87e339ef5..ffca0a07c 100644 --- a/src/Entity/Content.php +++ b/src/Entity/Content.php @@ -30,7 +30,7 @@ * }) * @ORM\HasLifecycleCallbacks */ -class Content implements \JsonSerializable +class Content { use ContentLocalizeTrait; use ContentExtrasTrait; diff --git a/src/Entity/ContentExtrasTrait.php b/src/Entity/ContentExtrasTrait.php index 50e5c218d..b7d1fdfba 100644 --- a/src/Entity/ContentExtrasTrait.php +++ b/src/Entity/ContentExtrasTrait.php @@ -40,35 +40,4 @@ public function getExtras(): array 'editLink' => $this->contentExtension->getEditLink($content), ]; } - - public function jsonSerialize(): array - { - /** @var Content $content */ - $content = $this; - - if ($this->getDefinition() === null) { - return []; - } - - return [ - 'id' => $content->getId(), - 'contentType' => $content->getContentType(), - 'slug' => $content->getSlug(), - 'author' => [ - 'id' => $content->getAuthor()->getId(), - 'displayName' => $content->getAuthor()->getDisplayName(), - 'username' => $content->getAuthor()->getUsername(), - 'email' => $content->getAuthor()->getEmail(), - ], - 'fields' => $content->getFieldValues(), - 'taxonomies' => $content->getTaxonomyValues(), - 'extras' => $this->getExtras(), - 'status' => $content->getStatus(), - 'icon' => $content->getIcon(), - 'createdAt' => $content->getCreatedAt(), - 'modifiedAt' => $content->getModifiedAt(), - 'publishedAt' => $content->getPublishedAt(), - 'depublishedAt' => $content->getDepublishedAt(), - ]; - } } diff --git a/src/Repository/ContentRepository.php b/src/Repository/ContentRepository.php index 8652f4f73..ac6a124cb 100644 --- a/src/Repository/ContentRepository.php +++ b/src/Repository/ContentRepository.php @@ -138,7 +138,7 @@ public function findOneBySlug(string $slug): ?Content 'field.id = slug.id' ) ->andWhere('slug.value = :slug') - ->setParameter('slug', json_encode([$slug])) + ->setParameter('slug', \GuzzleHttp\json_encode([$slug])) ->getQuery() ->getOneOrNullResult(); } diff --git a/src/Repository/FieldRepository.php b/src/Repository/FieldRepository.php index a5f544115..7c159063b 100644 --- a/src/Repository/FieldRepository.php +++ b/src/Repository/FieldRepository.php @@ -27,7 +27,7 @@ private function getQueryBuilder(?QueryBuilder $qb = null) return $qb ?: $this->createQueryBuilder('field'); } - public function findOneBySlug($slug): ?Field + public function findOneBySlug(string $slug): ?Field { return $this->getQueryBuilder() ->andWhere('field.value = :slug') diff --git a/src/Twig/JsonExtension.php b/src/Twig/JsonExtension.php new file mode 100644 index 000000000..6f2658cab --- /dev/null +++ b/src/Twig/JsonExtension.php @@ -0,0 +1,64 @@ +normalizer = $normalizer; + } + + /** + * {@inheritdoc} + */ + public function getFilters() + { + return [ + new TwigFilter('normalize_records', [$this, 'normalizeRecords']), + new TwigFilter('json_records', [$this, 'jsonRecords']), + ]; + } + + public function jsonRecords($records): string + { + return \GuzzleHttp\json_encode($this->normalizeRecords($records)); + } + + public function normalizeRecords($records): array + { + if ($records instanceof Content) { + return $this->contentToArray($records); + } + + if (is_array($records)) { + $normalizedRecords = $records; + } elseif (is_iterable($records)) { + $normalizedRecords = iterator_to_array($records); + } else { + throw new \InvalidArgumentException(); + } + + return array_map([$this, 'contentToArray'], $normalizedRecords); + } + + private function contentToArray(Content $content): array + { + // we do it that way because in current API Platform version a Resource can't implement \JsonSerializable + return $this->normalizer->normalize($content, null, ['group' => [self::SERIALIZE_GROUP]]); + } +} diff --git a/templates/_partials/_content_listing.html.twig b/templates/_partials/_content_listing.html.twig index 118677bad..dabceeedf 100644 --- a/templates/_partials/_content_listing.html.twig +++ b/templates/_partials/_content_listing.html.twig @@ -2,7 +2,7 @@ From c4772e30eabd741c707e344738ba0855e321d974 Mon Sep 17 00:00:00 2001 From: Jarek Jakubowski Date: Sun, 14 Apr 2019 10:42:51 +0200 Subject: [PATCH 2/2] Update API platform version in composer.json --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 7793fad9b..d92148a54 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "ext-json": "*", "ext-mbstring": "*", "ext-pdo": "*", - "api-platform/core": "v2.4.0-beta.1", + "api-platform/core": "^2.4", "bolt/common": "^2.0.2", "cocur/slugify": "^3.1", "doctrine/annotations": "^1.0", @@ -129,8 +129,7 @@ ] }, "conflict": { - "symfony/symfony": "*", - "api-platform/core": "v2.4.0-beta.2" + "symfony/symfony": "*" }, "extra": { "symfony": {