default services conflit ?
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\AnnotatedCommand;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\State\State;
|
||||
use Consolidation\AnnotatedCommand\State\StateHelper;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\ConsoleEvents;
|
||||
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class CommandEventHookDispatcher extends HookDispatcher
|
||||
{
|
||||
/**
|
||||
* @param ConsoleCommandEvent $event
|
||||
*/
|
||||
public function callCommandEventHooks(ConsoleCommandEvent $event)
|
||||
{
|
||||
$input = $event->getInput();
|
||||
$output = $event->getOutput();
|
||||
|
||||
$hooks = [
|
||||
HookManager::PRE_COMMAND_EVENT,
|
||||
HookManager::COMMAND_EVENT,
|
||||
HookManager::POST_COMMAND_EVENT
|
||||
];
|
||||
$commandEventHooks = $this->getHooks($hooks);
|
||||
foreach ($commandEventHooks as $commandEvent) {
|
||||
if ($commandEvent instanceof EventDispatcherInterface) {
|
||||
$commandEvent->dispatch($event, ConsoleEvents::COMMAND);
|
||||
}
|
||||
if (is_callable($commandEvent)) {
|
||||
$state = StateHelper::injectIntoCallbackObject($commandEvent, $input, $output);
|
||||
$commandEvent($event);
|
||||
$state->restore();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\Hooks\ExtractOutputInterface;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\OutputDataInterface;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class ExtracterHookDispatcher extends HookDispatcher implements ExtractOutputInterface
|
||||
{
|
||||
/**
|
||||
* Convert the result object to printable output in
|
||||
* structured form.
|
||||
*/
|
||||
public function extractOutput($result)
|
||||
{
|
||||
if ($result instanceof OutputDataInterface) {
|
||||
return $result->getOutputData();
|
||||
}
|
||||
|
||||
$hooks = [
|
||||
HookManager::EXTRACT_OUTPUT,
|
||||
];
|
||||
$extractors = $this->getHooks($hooks);
|
||||
foreach ($extractors as $extractor) {
|
||||
$structuredOutput = $this->callExtractor($extractor, $result);
|
||||
if (isset($structuredOutput)) {
|
||||
return $structuredOutput;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function callExtractor($extractor, $result)
|
||||
{
|
||||
if ($extractor instanceof ExtractOutputInterface) {
|
||||
return $extractor->extractOutput($result);
|
||||
}
|
||||
if (is_callable($extractor)) {
|
||||
return $extractor($result);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\AnnotationData;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class HookDispatcher
|
||||
{
|
||||
/** var HookManager */
|
||||
protected $hookManager;
|
||||
protected $names;
|
||||
|
||||
public function __construct(HookManager $hookManager, $names)
|
||||
{
|
||||
$this->hookManager = $hookManager;
|
||||
$this->names = $names;
|
||||
}
|
||||
|
||||
public function getHooks($hooks, $annotationData = null)
|
||||
{
|
||||
return $this->hookManager->getHooks($this->names, $hooks, $annotationData);
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\AnnotationData;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\Hooks\InitializeHookInterface;
|
||||
use Consolidation\AnnotatedCommand\State\State;
|
||||
use Consolidation\AnnotatedCommand\State\StateHelper;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class InitializeHookDispatcher extends HookDispatcher implements InitializeHookInterface
|
||||
{
|
||||
public function initialize(
|
||||
InputInterface $input,
|
||||
AnnotationData $annotationData
|
||||
) {
|
||||
$hooks = [
|
||||
HookManager::PRE_INITIALIZE,
|
||||
HookManager::INITIALIZE,
|
||||
HookManager::POST_INITIALIZE
|
||||
];
|
||||
$providers = $this->getHooks($hooks, $annotationData);
|
||||
foreach ($providers as $provider) {
|
||||
$this->callInitializeHook($provider, $input, $annotationData);
|
||||
}
|
||||
}
|
||||
|
||||
protected function callInitializeHook($provider, $input, AnnotationData $annotationData)
|
||||
{
|
||||
$state = StateHelper::injectIntoCallbackObject($provider, $input);
|
||||
$result = $this->doInitializeHook($provider, $input, $annotationData);
|
||||
$state->restore();
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function doInitializeHook($provider, $input, AnnotationData $annotationData)
|
||||
{
|
||||
if ($provider instanceof InitializeHookInterface) {
|
||||
return $provider->initialize($input, $annotationData);
|
||||
}
|
||||
if (is_callable($provider)) {
|
||||
return $provider($input, $annotationData);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\AnnotationData;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\Hooks\InteractorInterface;
|
||||
use Consolidation\AnnotatedCommand\State\State;
|
||||
use Consolidation\AnnotatedCommand\State\StateHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class InteractHookDispatcher extends HookDispatcher
|
||||
{
|
||||
public function interact(
|
||||
InputInterface $input,
|
||||
OutputInterface $output,
|
||||
AnnotationData $annotationData
|
||||
) {
|
||||
$hooks = [
|
||||
HookManager::PRE_INTERACT,
|
||||
HookManager::INTERACT,
|
||||
HookManager::POST_INTERACT
|
||||
];
|
||||
$interactors = $this->getHooks($hooks, $annotationData);
|
||||
foreach ($interactors as $interactor) {
|
||||
$this->callInteractor($interactor, $input, $output, $annotationData);
|
||||
}
|
||||
}
|
||||
|
||||
protected function callInteractor($interactor, $input, $output, AnnotationData $annotationData)
|
||||
{
|
||||
$state = StateHelper::injectIntoCallbackObject($interactor, $input, $output);
|
||||
$result = $this->doInteractor($interactor, $input, $output, $annotationData);
|
||||
$state->restore();
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function doInteractor($interactor, $input, $output, AnnotationData $annotationData)
|
||||
{
|
||||
if ($interactor instanceof InteractorInterface) {
|
||||
return $interactor->interact($input, $output, $annotationData);
|
||||
}
|
||||
if (is_callable($interactor)) {
|
||||
return $interactor($input, $output, $annotationData);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Consolidation\AnnotatedCommand\AnnotatedCommand;
|
||||
use Consolidation\AnnotatedCommand\AnnotationData;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\Hooks\OptionHookInterface;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class OptionsHookDispatcher extends HookDispatcher implements OptionHookInterface
|
||||
{
|
||||
public function getOptions(
|
||||
Command $command,
|
||||
AnnotationData $annotationData
|
||||
) {
|
||||
$hooks = [
|
||||
HookManager::PRE_OPTION_HOOK,
|
||||
HookManager::OPTION_HOOK,
|
||||
HookManager::POST_OPTION_HOOK
|
||||
];
|
||||
$optionHooks = $this->getHooks($hooks, $annotationData);
|
||||
foreach ($optionHooks as $optionHook) {
|
||||
$this->callOptionHook($optionHook, $command, $annotationData);
|
||||
}
|
||||
$commandInfoList = $this->hookManager->getHookOptionsForCommand($command);
|
||||
if ($command instanceof AnnotatedCommand) {
|
||||
$command->optionsHookForHookAnnotations($commandInfoList);
|
||||
}
|
||||
}
|
||||
|
||||
protected function callOptionHook($optionHook, $command, AnnotationData $annotationData)
|
||||
{
|
||||
if ($optionHook instanceof OptionHookInterface) {
|
||||
return $optionHook->getOptions($command, $annotationData);
|
||||
}
|
||||
if (is_callable($optionHook)) {
|
||||
return $optionHook($command, $annotationData);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\AnnotationData;
|
||||
use Consolidation\AnnotatedCommand\CommandData;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\Hooks\ProcessResultInterface;
|
||||
use Consolidation\AnnotatedCommand\State\State;
|
||||
use Consolidation\AnnotatedCommand\State\StateHelper;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class ProcessResultHookDispatcher extends HookDispatcher implements ProcessResultInterface
|
||||
{
|
||||
/**
|
||||
* Process result and decide what to do with it.
|
||||
* Allow client to add transformation / interpretation
|
||||
* callbacks.
|
||||
*/
|
||||
public function process($result, CommandData $commandData)
|
||||
{
|
||||
$hooks = [
|
||||
HookManager::PRE_PROCESS_RESULT,
|
||||
HookManager::PROCESS_RESULT,
|
||||
HookManager::POST_PROCESS_RESULT,
|
||||
HookManager::PRE_ALTER_RESULT,
|
||||
HookManager::ALTER_RESULT,
|
||||
HookManager::POST_ALTER_RESULT,
|
||||
HookManager::POST_COMMAND_HOOK,
|
||||
];
|
||||
$processors = $this->getHooks($hooks, $commandData->annotationData());
|
||||
foreach ($processors as $processor) {
|
||||
$result = $this->callProcessor($processor, $result, $commandData);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function callProcessor($processor, $result, CommandData $commandData)
|
||||
{
|
||||
$state = StateHelper::injectIntoCallbackObject($processor, $commandData->input(), $commandData->output());
|
||||
$result = $this->doProcessor($processor, $result, $commandData);
|
||||
$state->restore();
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function doProcessor($processor, $result, CommandData $commandData)
|
||||
{
|
||||
$processed = null;
|
||||
if ($processor instanceof ProcessResultInterface) {
|
||||
$processed = $processor->process($result, $commandData);
|
||||
}
|
||||
if (is_callable($processor)) {
|
||||
$processed = $processor($result, $commandData);
|
||||
}
|
||||
if (isset($processed)) {
|
||||
return $processed;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\CommandData;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
|
||||
/**
|
||||
* Call hooks.
|
||||
*/
|
||||
class ReplaceCommandHookDispatcher extends HookDispatcher implements LoggerAwareInterface
|
||||
{
|
||||
|
||||
use LoggerAwareTrait;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function hasReplaceCommandHook()
|
||||
{
|
||||
return (bool) count($this->getReplaceCommandHooks());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \callable[]
|
||||
*/
|
||||
public function getReplaceCommandHooks()
|
||||
{
|
||||
$hooks = [
|
||||
HookManager::REPLACE_COMMAND_HOOK,
|
||||
];
|
||||
$replaceCommandHooks = $this->getHooks($hooks);
|
||||
|
||||
return $replaceCommandHooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public function getReplacementCommand(CommandData $commandData)
|
||||
{
|
||||
$replaceCommandHooks = $this->getReplaceCommandHooks();
|
||||
|
||||
// We only take the first hook implementation of "replace-command" as the replacement. Commands shouldn't have
|
||||
// more than one replacement.
|
||||
$replacementCommand = reset($replaceCommandHooks);
|
||||
|
||||
if ($this->logger && count($replaceCommandHooks) > 1) {
|
||||
$command_name = $commandData->annotationData()->get('command', 'unknown');
|
||||
$message = "Multiple implementations of the \"replace - command\" hook exist for the \"$command_name\" command.\n";
|
||||
foreach ($replaceCommandHooks as $replaceCommandHook) {
|
||||
$class = get_class($replaceCommandHook[0]);
|
||||
$method = $replaceCommandHook[1];
|
||||
$hook_name = "$class->$method";
|
||||
$message .= " - $hook_name\n";
|
||||
}
|
||||
$this->logger->warning($message);
|
||||
}
|
||||
|
||||
return $replacementCommand;
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\ExitCodeInterface;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\Hooks\StatusDeterminerInterface;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class StatusDeterminerHookDispatcher extends HookDispatcher implements StatusDeterminerInterface
|
||||
{
|
||||
/**
|
||||
* Call all status determiners, and see if any of them
|
||||
* know how to convert to a status code.
|
||||
*/
|
||||
public function determineStatusCode($result)
|
||||
{
|
||||
// If the result (post-processing) is an object that
|
||||
// implements ExitCodeInterface, then we will ask it
|
||||
// to give us the status code.
|
||||
if ($result instanceof ExitCodeInterface) {
|
||||
return $result->getExitCode();
|
||||
}
|
||||
|
||||
$hooks = [
|
||||
HookManager::STATUS_DETERMINER,
|
||||
];
|
||||
// If the result does not implement ExitCodeInterface,
|
||||
// then we'll see if there is a determiner that can
|
||||
// extract a status code from the result.
|
||||
$determiners = $this->getHooks($hooks);
|
||||
foreach ($determiners as $determiner) {
|
||||
$status = $this->callDeterminer($determiner, $result);
|
||||
if (isset($status)) {
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function callDeterminer($determiner, $result)
|
||||
{
|
||||
if ($determiner instanceof StatusDeterminerInterface) {
|
||||
return $determiner->determineStatusCode($result);
|
||||
}
|
||||
if (is_callable($determiner)) {
|
||||
return $determiner($result);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
||||
|
||||
use Consolidation\AnnotatedCommand\AnnotationData;
|
||||
use Consolidation\AnnotatedCommand\CommandData;
|
||||
use Consolidation\AnnotatedCommand\CommandError;
|
||||
use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
||||
use Consolidation\AnnotatedCommand\Hooks\ValidatorInterface;
|
||||
use Consolidation\AnnotatedCommand\State\State;
|
||||
use Consolidation\AnnotatedCommand\State\StateHelper;
|
||||
|
||||
/**
|
||||
* Call hooks
|
||||
*/
|
||||
class ValidateHookDispatcher extends HookDispatcher implements ValidatorInterface
|
||||
{
|
||||
public function validate(CommandData $commandData)
|
||||
{
|
||||
$hooks = [
|
||||
HookManager::PRE_ARGUMENT_VALIDATOR,
|
||||
HookManager::ARGUMENT_VALIDATOR,
|
||||
HookManager::POST_ARGUMENT_VALIDATOR,
|
||||
HookManager::PRE_COMMAND_HOOK,
|
||||
HookManager::COMMAND_HOOK,
|
||||
];
|
||||
$validators = $this->getHooks($hooks, $commandData->annotationData());
|
||||
foreach ($validators as $validator) {
|
||||
$validated = $this->callValidator($validator, $commandData);
|
||||
if ($validated === false) {
|
||||
return new CommandError();
|
||||
}
|
||||
if (is_object($validated)) {
|
||||
return $validated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function callValidator($validator, CommandData $commandData)
|
||||
{
|
||||
$state = StateHelper::injectIntoCallbackObject($validator, $commandData->input(), $commandData->output());
|
||||
$result = $this->doValidator($validator, $commandData);
|
||||
$state->restore();
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function doValidator($validator, CommandData $commandData)
|
||||
{
|
||||
if ($validator instanceof ValidatorInterface) {
|
||||
return $validator->validate($commandData);
|
||||
}
|
||||
if (is_callable($validator)) {
|
||||
return $validator($commandData);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user