Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from 21TORR/next
Browse files Browse the repository at this point in the history
Add URL rewriter
  • Loading branch information
apfelbox authored May 31, 2022
2 parents f630d91 + 6a2b5d9 commit c84078b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
6.3.0
=====

* (feature) Add URL rewriter to rewrite static URLs.


6.2.1
=====

Expand Down
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ services:

Torr\PrismicApi\Transform\DataTransformer:
$sliceExtraDataGenerators: !tagged_iterator 'prismic.slice.extra-data-generator'
$urlRewriters: !tagged_iterator 'prismic.url_rewriter'

Torr\PrismicApi\Url\PrismicBackendUrlGenerator:
$repository: '%env(PRISMIC_REPOSITORY)%'
4 changes: 4 additions & 0 deletions src/PrismicApiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Torr\BundleHelpers\Bundle\BundleExtension;
use Torr\PrismicApi\Definition\DocumentDefinition;
use Torr\PrismicApi\RichText\Link\LinkGeneratorHandler;
use Torr\PrismicApi\Transform\Link\UrlRewriterInterface;
use Torr\PrismicApi\Transform\Slice\SliceExtraDataGeneratorInterface;

final class PrismicApiBundle extends Bundle
Expand All @@ -28,6 +29,9 @@ public function build (ContainerBuilder $container) : void
$container->registerForAutoconfiguration(LinkGeneratorHandler::class)
->addTag("prismic.link_generator");

$container->registerForAutoconfiguration(UrlRewriterInterface::class)
->addTag("prismic.url_rewriter");

$container->registerForAutoconfiguration(DocumentDefinition::class)
->addTag("prismic.document.definition");

Expand Down
4 changes: 2 additions & 2 deletions src/Structure/Field/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function transformValue (
if ("Web" === $type)
{
return parent::transformValue(
$data["url"] ?? null,
$dataTransformer->rewriteUrl($data["url"] ?? null),
$dataTransformer,
$dataVisitor,
);
Expand All @@ -110,7 +110,7 @@ public function transformValue (
}

return parent::transformValue(
$data["url"] ?? null,
$dataTransformer->rewriteUrl($data["url"] ?? null),
$dataTransformer,
$dataVisitor,
);
Expand Down
35 changes: 32 additions & 3 deletions src/Transform/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
use Torr\PrismicApi\Structure\Field\InputField;
use Torr\PrismicApi\Structure\Slice\Slice;
use Torr\PrismicApi\Structure\Slice\SliceZone;
use Torr\PrismicApi\Transform\Link\UrlRewriterInterface;
use Torr\PrismicApi\Transform\Slice\SliceExtraDataGeneratorInterface;

final class DataTransformer
{
public function __construct (
/** @var iterable<SliceExtraDataGeneratorInterface> */
private readonly iterable $sliceExtraDataGenerators,
/** @var iterable<UrlRewriterInterface> */
private readonly iterable $urlRewriters,
) {}

/**
*/
public function transformValue (
array $data,
InputField|SliceZone $field,
Expand All @@ -39,9 +44,20 @@ public function transformRichText (array $data) : array
$type = $span["type"];
$linkType = $span["data"]["link_type"] ?? null;

$spans[] = "hyperlink" === $type && "Document" === $linkType
? $this->rewriteDocumentLinkToUrl($span)
: $span;
if ("hyperlink" === $type && "Document" === $linkType)
{
$spans[] = $this->rewriteDocumentLinkToUrl($span);
continue;
}

if ("hyperlink" === $type && "Web" === $linkType)
{
$span["data"]["url"] = $this->rewriteUrl($span["data"]["url"] ?? null);
$spans[] = $span;
continue;
}

$spans[] = $span;
}

$data[$index]["spans"] = $spans;
Expand Down Expand Up @@ -77,6 +93,19 @@ private function rewriteDocumentLinkToUrl (array $span) : array
return $span;
}

/**
*/
public function rewriteUrl (?string $url) : ?string
{
/** @var UrlRewriterInterface $urlRewriter */
foreach ($this->urlRewriters as $urlRewriter)
{
$url = $urlRewriter->rewriteUrl($url);
}

return $url;
}

/**
* Generates the extra data for the given slice
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Transform/Link/UrlRewriterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Torr\PrismicApi\Transform\Link;

interface UrlRewriterInterface
{
/**
* Rewrites the URL
*/
public function rewriteUrl (?string $url) : ?string;
}

0 comments on commit c84078b

Please sign in to comment.