Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: TAKS: Improve node address serialisation #5067

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -90,7 +90,13 @@ final public static function fromLegacyDimensionArray(array $legacyDimensionValu

final public static function fromUriRepresentation(string $encoded): self
{
return self::instance(json_decode(base64_decode($encoded), true));
parse_str(base64_decode(str_replace( '~', '=', $encoded)), $parsed);
return self::instance($parsed);
}

final public function toUriRepresentation(): string
{
return str_replace('=', '~', base64_encode(http_build_query($this->coordinates)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, I might need ot understand how this is supposed to be used? Why not just jsonify?

}

/**
Original file line number Diff line number Diff line change
@@ -99,6 +99,31 @@ public function toJson(): string
}
}

public static function fromUriString(string $encoded): self
{
$parts = explode('/', $encoded);
if (count($parts) !== 4) {
throw new \RuntimeException(sprintf('Failed to decode NodeAddress from uri string: %s', $encoded), 1716051847);
}
return new self(
ContentRepositoryId::fromString($parts[0]),
WorkspaceName::fromString($parts[1]),
DimensionSpacePoint::fromUriRepresentation($parts[2]),
NodeAggregateId::fromString($parts[3])
);
}

public function toUriString(): string
{
return sprintf(
'%s/%s/%s/%s',
$this->contentRepositoryId->value,
$this->workspaceName->value,
$this->dimensionSpacePoint->toUriRepresentation(),
$this->aggregateId->value
);
}

public function jsonSerialize(): mixed
{
return get_object_vars($this);
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Neos\ContentRepository\Core\Tests\Unit\SharedModel\Node;

/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAddress;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use PHPUnit\Framework\TestCase;

class NodeAddressTest extends TestCase
{
public static function urlCompatibleSerialization(): iterable
{
yield 'no dimensions' => [
'nodeAddress' => NodeAddress::create(
ContentRepositoryId::fromString('default'),
WorkspaceName::forLive(),
DimensionSpacePoint::createWithoutDimensions(),
NodeAggregateId::fromString('marcus-heinrichus')
),
'serialized' => 'default/live//marcus-heinrichus'
];

yield 'one dimension' => [
'nodeAddress' => NodeAddress::create(
ContentRepositoryId::fromString('default'),
WorkspaceName::fromString('user-mh'),
DimensionSpacePoint::fromArray(['language' => 'de']),
NodeAggregateId::fromString('79e69d1c-b079-4535-8c8a-37e76736c445')
),
'serialized' => 'default/user-mh/bGFuZ3VhZ2U9ZGU~/79e69d1c-b079-4535-8c8a-37e76736c445'
];

yield 'two dimensions' => [
'nodeAddress' => NodeAddress::create(
ContentRepositoryId::fromString('second'),
WorkspaceName::fromString('user-mh'),
DimensionSpacePoint::fromArray(['language' => 'en_US', 'audience' => 'nice people']),
NodeAggregateId::fromString('my-node-id')
),
'serialized' => 'second/user-mh/bGFuZ3VhZ2U9ZW5fVVMmYXVkaWVuY2U9bmljZStwZW9wbGU~/my-node-id'
];
}

/**
* @dataProvider urlCompatibleSerialization
*/
public function testUrlCompatibleSerialization(NodeAddress $nodeAddress, string $expected): void
{
self::assertEquals($expected, $nodeAddress->toUriString());
}

/**
* @dataProvider urlCompatibleSerialization
*/
public function testUrlCompatibleDeserialization(NodeAddress $expectedNodeAddress, string $encoded): void
{
$nodeAddress = NodeAddress::fromUriString($encoded);
self::assertInstanceOf(NodeAddress::class, $nodeAddress);
self::assertTrue($expectedNodeAddress->equals($nodeAddress));
}
}
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/FrontendRouting/NodeAddressFactory.php
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ public function createFromUriString(string $serializedNodeAddress): NodeAddress
list($workspaceNameSerialized, $dimensionSpacePointSerialized, $nodeAggregateIdSerialized)
= explode('__', $serializedNodeAddress);
$workspaceName = WorkspaceName::fromString($workspaceNameSerialized);
$dimensionSpacePoint = DimensionSpacePoint::fromUriRepresentation($dimensionSpacePointSerialized);
$dimensionSpacePoint = DimensionSpacePoint::fromArray(json_decode(base64_decode($dimensionSpacePointSerialized), true));
$nodeAggregateId = NodeAggregateId::fromString($nodeAggregateIdSerialized);

$contentStreamId = $this->contentRepository->getWorkspaceFinder()->findOneByName($workspaceName)