Skip to content

Instantly share code, notes, and snippets.

@itsgoingd
Created May 13, 2020 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsgoingd/bc7ae61d2272672a4de42dff7c37b518 to your computer and use it in GitHub Desktop.
Save itsgoingd/bc7ae61d2272672a4de42dff7c37b518 to your computer and use it in GitHub Desktop.
clockwork symfony support poc
<?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();
});
}
}
<?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