Skip to content

Commit

Permalink
Fix serialization and improve static analysis results
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schmitt committed Aug 3, 2022
1 parent 9cf1ad8 commit 8149d14
Show file tree
Hide file tree
Showing 20 changed files with 9,304 additions and 178 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/.ddev/
/.idea/
/Packages/
/vendor/
2 changes: 1 addition & 1 deletion Classes/Application/CacheTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace Nezaniel\ComponentView\Application;

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\NodeType\NodeTypeName;
use Neos\ContentRepository\Domain\Projection\Content\NodeInterface;
use Neos\Flow\Annotations as Flow;

/**
Expand Down
2 changes: 1 addition & 1 deletion Classes/Application/ContentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function resolveCacheSegmentForContentCollection(
CacheTag::forNode($contentCollection, $workspaceName)
);
$content = new ComponentCollection(... array_map(
function (Node $childNode) use($subgraph, $inBackend, &$cacheTags): ComponentInterface {
function (Node $childNode) use ($subgraph, $inBackend, &$cacheTags): ComponentInterface {
return $this->delegate($childNode, $subgraph, $inBackend, $cacheTags);
},
$contentCollection->findChildNodes()->toArray()
Expand Down
12 changes: 0 additions & 12 deletions Classes/Domain/ComponentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,4 @@ public function __toString(): string
{
return $this->render();
}

/**
* @return array<int|QualifiedComponentSerialization>
*/
public function jsonSerialize(): array
{
return array_map(
fn (ComponentInterface|string $component): QualifiedComponentSerialization
=> QualifiedComponentSerialization::create($component),
$this->components
);
}
}
9 changes: 1 addition & 8 deletions Classes/Domain/ComponentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@
/**
* The interface for self-rendering components
*/
interface ComponentInterface extends \Stringable, \JsonSerializable
interface ComponentInterface extends \Stringable
{
public function render(): string;

/**
* Serializes the component to cache
*
* @return array<string,string|ComponentInterface|null>
*/
public function jsonSerialize(): array;
}
8 changes: 7 additions & 1 deletion Classes/Domain/NodeMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ final class NodeMetadataFactory extends ContentElementWrappingService
#[Flow\Inject]
protected UriService $uriService;

/**
* @return array<string,mixed>
*/
public function getAugmenterAttributesForContentNode(NodeInterface $contentNode, ?string $locator = null): ?array
{
if (!$this->needsMetadata($contentNode, false)) {
Expand All @@ -50,9 +53,12 @@ public function getScriptForContentNode(NodeInterface $contentNode): string
$this->uriService->getControllerContext()
));

return "<script data-neos-nodedata>(function(){(this['@Neos.Neos.Ui:Nodes'] = this['@Neos.Neos.Ui:Nodes'] || {})['". $contentNode->getContextPath() . "'] = {$metadata}})()</script>";
return "<script data-neos-nodedata>(function(){(this['@Neos.Neos.Ui:Nodes'] = this['@Neos.Neos.Ui:Nodes'] || {})['" . $contentNode->getContextPath() . "'] = {$metadata}})()</script>";
}

/**
* @return array<string,mixed>
*/
public function forDocumentNode(NodeInterface $documentNode, ?string $locator = null): ?array
{
if (!$this->needsMetadata($documentNode, true)) {
Expand Down
10 changes: 1 addition & 9 deletions Classes/Domain/NodeMetadataWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* A component for wrapping node metadata around a component
*/
#[Flow\Proxy(false)]
final class NodeMetadataWrapper implements ComponentInterface, ComponentContainerInterface
final class NodeMetadataWrapper implements ComponentInterface
{
/**
* @param array<string,mixed> $attributes
Expand Down Expand Up @@ -43,14 +43,6 @@ public function render(): string
return $result;
}

public function jsonSerialize(): array
{
return [
'attributes' => $this->attributes,
'content' => QualifiedComponentSerialization::create($this->content)
];
}

public function isEmpty(): bool
{
return $this->content instanceof ComponentContainerInterface
Expand Down
43 changes: 0 additions & 43 deletions Classes/Domain/QualifiedComponentSerialization.php

This file was deleted.

105 changes: 105 additions & 0 deletions Classes/Domain/UriService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the Nezaniel.ComponentView package
* and taken over from PackageFactory.AtomicFusion.PresentationObjects.
* @see https://github.com/PackageFactory/atomic-fusion-presentationobjects
*/

declare(strict_types=1);

namespace PackageFactory\AtomicFusion\PresentationObjects\Infrastructure;

use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Uri;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Service\Context as ContentContext;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\Http;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Neos\Service\LinkingService;
use Neos\Flow\Mvc;
use Neos\Flow\Core\Bootstrap;

/**
* The URI service
*/
final class UriService
{
#[Flow\Inject]
protected ResourceManager $resourceManager;

#[Flow\Inject]
protected LinkingService $linkingService;

#[Flow\Inject]
protected AssetRepository $assetRepository;

#[Flow\Inject]
protected Bootstrap $bootstrap;

protected ?ControllerContext $controllerContext = null;

public function getNodeUri(NodeInterface $documentNode, bool $absolute = false, ?string $format = null): Uri
{
return new Uri($this->linkingService->createNodeUri($this->getControllerContext(), $documentNode, null, $format, $absolute));
}

public function getResourceUri(string $packageKey, string $resourcePath): Uri
{
return new Uri($this->resourceManager->getPublicPackageResourceUri($packageKey, $resourcePath));
}

public function getAssetUri(AssetInterface $asset): Uri
{
$uri = $this->resourceManager->getPublicPersistentResourceUri($asset->getResource());

return new Uri(is_string($uri) ? $uri : '#');
}

public function getControllerContext(): ControllerContext
{
if (is_null($this->controllerContext)) {
$requestHandler = $this->bootstrap->getActiveRequestHandler();
if ($requestHandler instanceof Http\RequestHandler) {
$request = $requestHandler->getHttpRequest();
} else {
$request = ServerRequest::fromGlobals();
}
$actionRequest = Mvc\ActionRequest::fromHttpRequest($request);
$uriBuilder = new Mvc\Routing\UriBuilder();
$uriBuilder->setRequest($actionRequest);
$this->controllerContext = new Mvc\Controller\ControllerContext(
$actionRequest,
new Mvc\ActionResponse(),
new Mvc\Controller\Arguments(),
$uriBuilder
);
}

return $this->controllerContext;
}

public function resolveLinkUri(string $rawLinkUri, ContentContext $subgraph): Uri
{
if (\mb_substr($rawLinkUri, 0, 7) === 'node://') {
$nodeIdentifier = \mb_substr($rawLinkUri, 7);
$node = $subgraph->getNodeByIdentifier($nodeIdentifier);
$linkUri = $node ? $this->getNodeUri($node) : new Uri('#');
} elseif (\mb_substr($rawLinkUri, 0, 8) === 'asset://') {
$assetIdentifier = \mb_substr($rawLinkUri, 8);
/** @var ?AssetInterface $asset */
$asset = $this->assetRepository->findByIdentifier($assetIdentifier);
$linkUri = $asset ? $this->getAssetUri($asset) : new Uri('#');
} elseif (\str_starts_with($rawLinkUri, 'https://') || \str_starts_with($rawLinkUri, 'http://')) {
$linkUri = new Uri($rawLinkUri);
} else {
$linkUri = new Uri('#');
}

return $linkUri;
}
}
5 changes: 3 additions & 2 deletions Classes/Infrastructure/ComponentCacheFlusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class ComponentCacheFlusher
*/
private array $workspaceGraph;

public function __construct(
) {
public function __construct()
{
$this->cacheTagsToFlush = new CacheTags();
$this->workspaceGraph = [];
}
Expand Down Expand Up @@ -107,6 +107,7 @@ public function whenSiteWasImported(): void
private function handleAssetChange(AssetInterface $asset): void
{
$cacheTagsToFlush = [CacheTag::forEverything(null)];
/** @var string $assetIdentifier */
$assetIdentifier = $this->persistenceManager->getIdentifierByObject($asset);
$cacheTagsToFlush[] = CacheTag::forAsset($assetIdentifier, null);
foreach ($this->workspaceRepository->findAll() as $workspace) {
Expand Down
12 changes: 9 additions & 3 deletions Classes/Infrastructure/ComponentSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function serializeComponent(ComponentInterface $component): array
];
} else {
$serializedPropertyValue = [
'__type' => gettype($propertyValue),
'__type' => get_debug_type($propertyValue),
'value' => $propertyValue
];
}
Expand All @@ -76,12 +76,15 @@ private static function serializeComponentCollection(
ComponentCollection $componentCollection,
\ReflectionClass $reflectionClass
): array {
/** @var array<int,string|ComponentInterface> $components */
$components = $reflectionClass->getProperties()[0]->getValue($componentCollection);

return [
'__class' => get_class($componentCollection),
'components' => array_map(
fn (ComponentInterface|string $childComponent): array|string
=> is_string($childComponent) ? $childComponent : self::serializeComponent($childComponent),
$reflectionClass->getProperties()[0]->getValue($componentCollection)
$components
)
];
}
Expand All @@ -93,11 +96,14 @@ private static function serializeCollectionType(
ComponentInterface $componentCollection,
\ReflectionClass $reflectionClass
): array {
/** @var array<int,ComponentInterface> $components */
$components = $reflectionClass->getProperties()[0]->getValue($componentCollection);

return [
'__class' => get_class($componentCollection),
$reflectionClass->getProperties()[0]->getName() => array_map(
fn(ComponentInterface $childComponent): array => self::serializeComponent($childComponent),
$reflectionClass->getProperties()[0]->getValue($componentCollection)
$components
)
];
}
Expand Down
Loading

0 comments on commit 8149d14

Please sign in to comment.