Skip to content

Commit

Permalink
NGSTACK-900: implement debug info and customized Symfony var dumper f…
Browse files Browse the repository at this point in the history
…or domain objects
  • Loading branch information
pspanja committed May 28, 2024
1 parent 2929063 commit 5ced4d2
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 5 deletions.
55 changes: 55 additions & 0 deletions bundle/DependencyInjection/Compiler/VarDumperPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Compiler;

use Netgen\IbexaSiteApi\API\Values\DebugInfo;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\VarDumper\Caster\Caster;

class VarDumperPass implements CompilerPassInterface
{
private const ClonerId = 'var_dumper.cloner';

public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition(self::ClonerId)) {
return;
}

$clonerDefinition = $container->findDefinition(self::ClonerId);

$clonerDefinition->addMethodCall(
'addCasters',
[
'$casters' => [
DebugInfo::class => [self::class, 'cast'],
],
],
);
}

public static function cast(DebugInfo $object, array $array): array
{
$debugInfo = $object->getDebugInfo();
$debugData = [];

foreach ($debugInfo as $key => $value) {
if (!isset($key[0]) || $key[0] !== "\0") {
if (array_key_exists(Caster::PREFIX_DYNAMIC . $key, $array)) {
continue;
}

$key = Caster::PREFIX_VIRTUAL . $key;
}

unset($array[$key]);

$debugData[$key] = $value;
}

return array_merge($debugData, $array);
}
}
2 changes: 2 additions & 0 deletions bundle/NetgenIbexaSiteApiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Compiler\RelationResolverRegistrationPass;
use Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Compiler\UrlAliasGeneratorOverridePass;
use Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Compiler\UrlAliasRouterOverridePass;
use Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Compiler\VarDumperPass;
use Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Compiler\ViewBuilderRegistrationPass;
use Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Configuration\Parser\ContentView;
use Netgen\Bundle\IbexaSiteApiBundle\DependencyInjection\Configuration\Parser\SiteApi;
Expand All @@ -32,6 +33,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new QueryTypeExpressionFunctionProviderPass());
$container->addCompilerPass(new RedirectExpressionFunctionProviderPass());
$container->addCompilerPass(new RelationResolverRegistrationPass());
$container->addCompilerPass(new VarDumperPass());
$container->addCompilerPass(new ViewBuilderRegistrationPass());

/** @var \Ibexa\Bundle\Core\DependencyInjection\IbexaCoreExtension $coreExtension */
Expand Down
2 changes: 1 addition & 1 deletion lib/API/Values/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @property-read \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo $innerVersionInfo
* @property-read \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo $versionInfo
*/
abstract class Content extends ValueObject
abstract class Content extends ValueObject implements DebugInfo
{
/**
* Returns if Content has the field with the given field definition $identifier.
Expand Down
2 changes: 1 addition & 1 deletion lib/API/Values/ContentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
* @property-read \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $innerContentType
* @property-read ?\Netgen\IbexaSiteApi\API\Values\Location $mainLocation
*/
abstract class ContentInfo extends ValueObject {}
abstract class ContentInfo extends ValueObject implements DebugInfo {}
16 changes: 16 additions & 0 deletions lib/API/Values/DebugInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Netgen\IbexaSiteApi\API\Values;

/**
* Provides debug information for view developers.
*/
interface DebugInfo
{
/**
* @return array<string, mixed>
*/
public function getDebugInfo(): array;
}
2 changes: 1 addition & 1 deletion lib/API/Values/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @property-read \Ibexa\Contracts\Core\Repository\Values\Content\Field $innerField
* @property-read \Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition $innerFieldDefinition
*/
abstract class Field extends ValueObject
abstract class Field extends ValueObject implements DebugInfo
{
abstract public function isEmpty(): bool;

Expand Down
2 changes: 1 addition & 1 deletion lib/API/Values/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see \Netgen\IbexaSiteApi\API\Values\Field
*/
abstract class Fields implements IteratorAggregate, ArrayAccess, Countable
abstract class Fields implements IteratorAggregate, ArrayAccess, Countable, DebugInfo
{
/**
* Return whether the collection contains a field with the given $identifier.
Expand Down
2 changes: 1 addition & 1 deletion lib/API/Values/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @property-read ?\Netgen\IbexaSiteApi\API\Values\Location $parent
* @property-read \Ibexa\Contracts\Core\Repository\Values\Content\Location $innerLocation
*/
abstract class Location extends ValueObject
abstract class Location extends ValueObject implements DebugInfo
{
/**
* Return an array of children Locations, limited by optional $limit.
Expand Down
19 changes: 19 additions & 0 deletions lib/Core/Site/Values/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,23 @@ private function internalGetUrl(): Url

return $this->url;
}

public function getDebugInfo(): array
{
return [
'id' => $this->id,
'mainLocationId' => $this->mainLocationId,
'name' => $this->name,
'languageCode' => $this->languageCode,
'isVisible' => $this->getContentInfo()->isVisible,
'url' => $this->getUrl(),
'path' => $this->getPath(),
'owner' => $this->getOwner(),
'modifier' => $this->getModifier(),
'mainLocation' => $this->getMainLocation(),
'locations' => $this->getLocations(),
'contentInfo' => $this->getContentInfo(),
'fields' => $this->fields,
];
}
}
23 changes: 23 additions & 0 deletions lib/Core/Site/Values/ContentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,27 @@ private function getMainLocation(): ?APILocation

return $this->internalMainLocation;
}

public function getDebugInfo(): array
{
return [
'id' => $this->innerContentInfo->id,
'mainLocationId' => $this->innerContentInfo->mainLocationId,
'remoteId' => $this->innerContentInfo->remoteId,
'name' => $this->name,
'published' => $this->innerContentInfo->published,
'currentVersionNo' => $this->innerContentInfo->currentVersionNo,
'publicationDate' => $this->innerContentInfo->publishedDate,
'modificationDate' => $this->innerContentInfo->modificationDate,
'languageCode' => $this->languageCode,
'mainLanguageCode' => $this->innerContentInfo->mainLanguageCode,
'alwaysAvailable' => $this->innerContentInfo->alwaysAvailable,
'isVisible' => !$this->innerContentInfo->isHidden,
'isHidden' => $this->innerContentInfo->isHidden,
'sectionId' => $this->innerContentInfo->sectionId,
'ownerId' => $this->innerContentInfo->ownerId,
'contentType' => $this->innerContentType,
'mainLocation' => $this->getMainLocation(),
];
}
}
18 changes: 18 additions & 0 deletions lib/Core/Site/Values/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ public function isSurrogate(): bool
{
return $this->isSurrogate;
}

public function getDebugInfo(): array
{
return [
'id' => $this->id,
'contentId' => $this->content->id,
'fieldDefIdentifier' => $this->fieldDefIdentifier,
'fieldTypeIdentifier' => $this->fieldTypeIdentifier,
'languageCode' => $this->languageCode,
'name' => $this->name,
'description' => $this->description,
'isEmpty' => $this->isEmpty,
'isSurrogate' => $this->isSurrogate,
'content' => $this->content,
'fieldDefinition' => $this->innerFieldDefinition,
'value' => $this->value,
];
}
}
7 changes: 7 additions & 0 deletions lib/Core/Site/Values/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,11 @@ private function getSurrogateField(string $identifier, SiteContent $content): Fi
'isSurrogate' => true,
]);
}

public function getDebugInfo(): array
{
$this->initialize();

return $this->fieldsByIdentifier;
}
}
25 changes: 25 additions & 0 deletions lib/Core/Site/Values/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,29 @@ private function internalGetUrl(): Url

return $this->url;
}

public function getDebugInfo(): array
{
return [
'id' => $this->innerLocation->id,
'contentId' => $this->innerLocation->contentId,
'remoteId' => $this->innerLocation->remoteId,
'status' => $this->innerLocation->status,
'priority' => $this->innerLocation->priority,
'hidden' => $this->innerLocation->hidden,
'invisible' => $this->innerLocation->invisible,
'explicitlyHidden' => $this->innerLocation->explicitlyHidden,
'isVisible' => !$this->innerLocation->hidden && !$this->innerLocation->invisible,
'pathString' => $this->innerLocation->pathString,
'pathArray' => $this->innerLocation->path,
'depth' => $this->innerLocation->depth,
'sortField' => $this->innerLocation->sortField,
'sortOrder' => $this->innerLocation->sortOrder,
'path' => $this->getPath(),
'url' => $this->getUrl(),
'content' => $this->getContent(),
'contentInfo' => $this->getContentInfo(),
'parent' => $this->getParent(),
];
}
}

0 comments on commit 5ced4d2

Please sign in to comment.