Created
May 13, 2020 20:22
-
-
Save itsgoingd/bc7ae61d2272672a4de42dff7c37b518 to your computer and use it in GitHub Desktop.
clockwork symfony support poc
This file contains hidden or 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
<?php namespace Clockwork\Support\Symfony; | |
use Clockwork\Clockwork; | |
use Clockwork\Support\Laravel\Console\CapturingFormatter; | |
use Symfony\Component\Console\Application; | |
use Symfony\Component\Console\ConsoleEvents; | |
use Symfony\Component\Console\Event\ConsoleCommandEvent; | |
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | |
use Symfony\Component\EventDispatcher\EventDispatcher; | |
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
class ClockworkConsoleSupport | |
{ | |
protected $events; | |
public function __construct(EventDispatcherInterface $events) | |
{ | |
$this->events = $events; | |
} | |
public static function forApplication(Application $app) | |
{ | |
$app->setDispatcher($dispatcher = new EventDispatcher); | |
return static::forDispatcher($dispatcher); | |
} | |
public static function forDispatcher(EventDispatcherInterface $events) | |
{ | |
return new static($events); | |
} | |
public function collect(Clockwork $clockwork) | |
{ | |
$this->events->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) { | |
$event->getOutput()->setFormatter( | |
new CapturingFormatter($event->getOutput()->getFormatter()) | |
); | |
}); | |
$this->events->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) use ($clockwork) { | |
$command = $event->getCommand(); | |
$argumentsDefaults = $command->getDefinition()->getArgumentDefaults(); | |
$optionsDefaults = $command->getDefinition()->getOptionDefaults(); | |
$clockwork | |
->resolveAsCommand( | |
$command->getName(), | |
$event->getExitCode(), | |
array_diff($event->getInput()->getArguments(), $argumentsDefaults), | |
array_diff($event->getInput()->getOptions(), $optionsDefaults), | |
$argumentsDefaults, | |
$optionsDefaults, | |
$event->getOutput()->getFormatter()->capturedOutput() | |
) | |
->storeRequest(); | |
}); | |
} | |
} |
This file contains hidden or 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
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Symfony\Component\Console\Application; | |
use Clockwork\Support\Symfony\ClockworkConsoleSupport; | |
use Clockwork\Support\Vanilla\Clockwork; | |
$clockwork = Clockwork::init(); | |
$app = new Application('Sample app', '1.0'); | |
ClockworkConsoleSupport::forApplication($app)->collect($clockwork->getClockwork()); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment