view vendor/consolidation/annotated-command/src/Hooks/Dispatchers/InteractHookDispatcher.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
line wrap: on
line source
<?php

namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;

use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Consolidation\AnnotatedCommand\Hooks\InteractorInterface;
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)
    {
        if ($interactor instanceof InteractorInterface) {
            return $interactor->interact($input, $output, $annotationData);
        }
        if (is_callable($interactor)) {
            return $interactor($input, $output, $annotationData);
        }
    }
}