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

add Extension System for StructureResolver #100

Open
wants to merge 1 commit into
base: 0.10
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion Content/StructureResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sulu\Bundle\HeadlessBundle\Content;

use Sulu\Bundle\HeadlessBundle\Content\StructureResolver\StructureResolverExtensionInterface;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\PageBundle\Document\BasePageDocument;
use Sulu\Bundle\PageBundle\Preview\PageRouteDefaultsProvider;
Expand Down Expand Up @@ -50,16 +51,23 @@ class StructureResolver implements StructureResolverInterface
*/
private $referenceStorePool;

/**
* @var StructureResolverExtensionInterface[]
*/
private iterable $structureResolverExtensions;

public function __construct(
ContentResolverInterface $contentResolver,
StructureManagerInterface $structureManager,
DocumentInspector $documentInspector,
ReferenceStorePoolInterface $referenceStorePool
ReferenceStorePoolInterface $referenceStorePool,
iterable $structureResolverExtensions = []
) {
$this->contentResolver = $contentResolver;
$this->structureManager = $structureManager;
$this->documentInspector = $documentInspector;
$this->referenceStorePool = $referenceStorePool;
$this->structureResolverExtensions = $structureResolverExtensions;
}

/**
Expand Down Expand Up @@ -95,6 +103,10 @@ public function resolve(
$data['view'][$property->getName()] = $contentView->getView();
}

foreach ($this->structureResolverExtensions as $structureResolverExtension) {
$data[$structureResolverExtension->getKey()] = $structureResolverExtension->resolve($requestedStructure, $locale);
Copy link
Member

Choose a reason for hiding this comment

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

As written in the other comments we need to go here with a array_merge/array_replace as there will be multiple extensions adding a breadcrumb for example.

Suggested change
$data[$structureResolverExtension->getKey()] = $structureResolverExtension->resolve($requestedStructure, $locale);
$data = \array_replace($data, $structureResolverExtension->resolve($requestedStructure, $locale));

}

return $data;
}

Expand Down
26 changes: 26 additions & 0 deletions Content/StructureResolver/StructureResolverExtensionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\HeadlessBundle\Content\StructureResolver;

use Sulu\Component\Content\Compat\StructureInterface;

interface StructureResolverExtensionInterface
{
public function getKey(): string;

Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

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

The getKey can not be used as we will have different provider providing a breadcrumb depending on instance of structure.

Suggested change
public function getKey(): string;

public function resolve(
StructureInterface $requestedStructure,
string $locale
): mixed;
Comment on lines +22 to +25
Copy link
Member

Choose a reason for hiding this comment

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

We would more go here with array as return value and use beneath array_merge so all extension can manipulate different data. This is the case for breadcrumb which can be set by multiple extensions e.g. PageBreadcrumb service will do:

if ($structure is not page structure) {
    return [];
}

return [
    'breadcrumbs' => $this->navigationMapper->getBreadcrumb($structure->getUuid()),
];

and the ARticleBreadcrumbProvider (living in the article bundle):

if ($structure is not article structure) {
    return [];
}

$parent = $structure->getPageTreeParent();

if (!$parent) {
    return [];
}

return [
    'breadcrumbs' => $this->navigationMapper->getBreadcrumb($parentarent()->getUuid()),
];
Suggested change
public function resolve(
StructureInterface $requestedStructure,
string $locale
): mixed;
/**
* @return array<string, mixed>
*/
public function resolve(StructureInterface $requestedStructure, string $locale): array;

}
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,31 @@ export default HeadlessTemplatePage;
Finally, you can build your frontend application by executing `npm install` and `npm run build` in the `assets/headless`
directory.

### Extending the StructureResolver
To add more data to the json that is returned for a site, you can extend the StructureResolver by registering an service with the tag `sulu_headless.structure_resolver_extension`.
Comment on lines +369 to +370
Copy link
Member

Choose a reason for hiding this comment

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

Some markdown viewers require a empty line after a headline to work correctly.

Suggested change
### Extending the StructureResolver
To add more data to the json that is returned for a site, you can extend the StructureResolver by registering an service with the tag `sulu_headless.structure_resolver_extension`.
### Extending the StructureResolver
To add more data to the json that is returned for a site, you can extend the StructureResolverExtension implementing the `StructureResolverExtensionInterface`. If autowire is not activated for your project you need to tag the service with `sulu_headless.structure_resolver_extension`.


```
use Sulu\Bundle\HeadlessBundle\Content\StructureResolver\StructureResolverExtensionInterface;

class MyStructureExtension implements StructureResolverExtensionInterface
{
public function getKey(): string
{
return 'myKey';
}

public function resolve(
StructureInterface $requestedStructure,
string $locale
): mixed {
return [
'foo' => 'bar',
'fruits' => Fruits::getArray($locale),
'webspaceKey' => $requestedStructure->getWebspaceKey()
];
}
Comment on lines +377 to +391
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public function getKey(): string
{
return 'myKey';
}
public function resolve(
StructureInterface $requestedStructure,
string $locale
): mixed {
return [
'foo' => 'bar',
'fruits' => Fruits::getArray($locale),
'webspaceKey' => $requestedStructure->getWebspaceKey()
];
}
public function resolve(StructureInterface $requestedStructure, string $locale): array
{
return [
'myKey' => [
'foo' => 'bar',
'fruits' => Fruits::getArray($locale),
'webspaceKey' => $requestedStructure->getWebspaceKey(),
],
];
}

}
```

## ❤️&nbsp; Support and Contributions

Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<argument type="service" id="sulu.content.structure_manager"/>
<argument type="service" id="sulu_document_manager.document_inspector"/>
<argument type="service" id="sulu_website.reference_store_pool"/>
<argument type="tagged_iterator" tag="sulu_headless.structure_resolver_extension"/>
</service>
<service id="Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface" alias="sulu_headless.structure_resolver"/>

Expand Down