-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5336 from neos/feature/5335-command-hooks
FEATURE: Command Hooks
- Loading branch information
Showing
10 changed files
with
258 additions
and
6 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Neos.ContentRepository.Core/Classes/CommandHandler/CommandHookInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\Core\CommandHandler; | ||
|
||
use Neos\ContentRepository\Core\ContentRepository; | ||
|
||
/** | ||
* Contract for a hook that is invoked just before any command is processed via {@see ContentRepository::handle()} | ||
* | ||
* A command hook can be used to replace/alter an incoming command before it is being passed to the corresponding {@see CommandHandlerInterface}. | ||
* This can be used to change or enrich the payload of the command. | ||
* A command hook can also be used to intercept commands based on their type or payload but this is not the intended use case because it can lead to a degraded user experience | ||
* | ||
* @api | ||
*/ | ||
interface CommandHookInterface | ||
{ | ||
/** | ||
* @param CommandInterface $command The command that is about to be handled | ||
* @return CommandInterface This hook must return a command instance. It can be the unaltered incoming $command or a new instance | ||
*/ | ||
public function onBeforeHandle(CommandInterface $command): CommandInterface; | ||
} |
56 changes: 56 additions & 0 deletions
56
Neos.ContentRepository.Core/Classes/CommandHandler/CommandHooks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\Core\CommandHandler; | ||
|
||
/** | ||
* Collection of {@see CommandHookInterface} instances, functioning as a delegating command hook implementation | ||
* | ||
* @implements \IteratorAggregate<CommandHookInterface> | ||
* @api | ||
*/ | ||
final readonly class CommandHooks implements CommandHookInterface, \IteratorAggregate, \Countable | ||
{ | ||
/** | ||
* @var array<CommandHookInterface> | ||
*/ | ||
private array $commandHooks; | ||
|
||
private function __construct( | ||
CommandHookInterface ...$commandHooks | ||
) { | ||
$this->commandHooks = $commandHooks; | ||
} | ||
|
||
/** | ||
* @param array<CommandHookInterface> $commandHooks | ||
*/ | ||
public static function fromArray(array $commandHooks): self | ||
{ | ||
return new self(...$commandHooks); | ||
} | ||
|
||
public static function none(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
{ | ||
yield from $this->commandHooks; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->commandHooks); | ||
} | ||
|
||
public function onBeforeHandle(CommandInterface $command): CommandInterface | ||
{ | ||
foreach ($this->commandHooks as $commandHook) { | ||
$command = $commandHook->onBeforeHandle($command); | ||
} | ||
return $command; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Neos.ContentRepository.Core/Classes/Factory/CommandHookFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\Core\Factory; | ||
|
||
use Neos\ContentRepository\Core\CommandHandler\CommandHookInterface; | ||
|
||
/** | ||
* @api for implementers of custom {@see CommandHookInterface}s | ||
*/ | ||
interface CommandHookFactoryInterface | ||
{ | ||
public function build( | ||
CommandHooksFactoryDependencies $commandHooksFactoryDependencies, | ||
): CommandHookInterface; | ||
} |
33 changes: 33 additions & 0 deletions
33
Neos.ContentRepository.Core/Classes/Factory/CommandHooksFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\Core\Factory; | ||
|
||
use Neos\ContentRepository\Core\CommandHandler\CommandHooks; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final readonly class CommandHooksFactory | ||
{ | ||
/** | ||
* @var array<CommandHookFactoryInterface> | ||
*/ | ||
private array $commandHookFactories; | ||
|
||
public function __construct( | ||
CommandHookFactoryInterface ...$commandHookFactories, | ||
) { | ||
$this->commandHookFactories = $commandHookFactories; | ||
} | ||
|
||
public function build( | ||
CommandHooksFactoryDependencies $commandHooksFactoryDependencies, | ||
): CommandHooks { | ||
return CommandHooks::fromArray(array_map( | ||
static fn (CommandHookFactoryInterface $factory) => $factory->build($commandHooksFactoryDependencies), | ||
$this->commandHookFactories | ||
)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
Neos.ContentRepository.Core/Classes/Factory/CommandHooksFactoryDependencies.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\Core\Factory; | ||
|
||
use Neos\ContentRepository\Core\CommandHandler\CommandHookInterface; | ||
use Neos\ContentRepository\Core\Dimension\ContentDimensionSourceInterface; | ||
use Neos\ContentRepository\Core\DimensionSpace\InterDimensionalVariationGraph; | ||
use Neos\ContentRepository\Core\NodeType\NodeTypeManager; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentGraphReadModelInterface; | ||
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId; | ||
|
||
/** | ||
* @api for implementers of custom {@see CommandHookInterface}s | ||
*/ | ||
final readonly class CommandHooksFactoryDependencies | ||
{ | ||
private function __construct( | ||
public ContentRepositoryId $contentRepositoryId, | ||
public ContentGraphReadModelInterface $contentGraphReadModel, | ||
public NodeTypeManager $nodeTypeManager, | ||
public ContentDimensionSourceInterface $contentDimensionSource, | ||
public InterDimensionalVariationGraph $variationGraph | ||
) { | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function create( | ||
ContentRepositoryId $contentRepositoryId, | ||
ContentGraphReadModelInterface $contentGraphReadModel, | ||
NodeTypeManager $nodeTypeManager, | ||
ContentDimensionSourceInterface $contentDimensionSource, | ||
InterDimensionalVariationGraph $variationGraph | ||
): self { | ||
return new self( | ||
$contentRepositoryId, | ||
$contentGraphReadModel, | ||
$nodeTypeManager, | ||
$contentDimensionSource, | ||
$variationGraph | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters