Skip to content

[Console] Add LazyCommandTrait to help writing lazy commands #39726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/Symfony/Component/Console/Command/LazyCommandTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Command;

use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
trait LazyCommandTrait
{
private $container;

/**
* Override this method and declare service dependencies as additional arguments.
*/
private function exec(InputInterface $input, OutputInterface $output/*, ...$services*/): int
{
throw new \LogicException(sprintf('Method "%s::exec()" should be overriden in "%s".', __TRAIT__, self::class));
}

/**
* @required
*/
public function setContainer(ContainerInterface $container): void
{
$this->container = $container;

if (\is_callable(['parent', __FUNCTION__])) {
parent::setContainer($container);
}
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this instanceof ServiceSubscriberInterface) {
throw new \LogicException(sprintf('Class "%s" should declare that it implements "%s".', self::class, ServiceSubscriberInterface::class));
}

$arguments = [$input, $output];

foreach (self::getSubscribedServices() as $id => $type) {
if (0 === strpos($id, self::class.'::get')) {
$arguments[] = $this->container->has($id) ? $this->container->get($id) : null;
}
}

return $this->exec(...$arguments);
}

public static function getSubscribedServices(): array
{
$m = new \ReflectionMethod(self::class, 'exec');

if (!(\ReflectionMethod::IS_PRIVATE & $m->getModifiers())) {
throw new \LogicException(sprintf('Method "%s::exec()" should be private.', self::class));
}
$r = $m->getReturnType();

if ('int' !== ($r instanceof \ReflectionNamedType ? $r->getName() : (string) $r)) {
throw new \LogicException(sprintf('Method "%s::exec()" should declare "int" as return type.', self::class));
}

$services = \is_callable(['parent', __FUNCTION__]) ? parent::getSubscribedServices() : [];
$i = 0;

foreach ($m->getParameters() as $i => $p) {
$r = $p->getType();
$type = $r instanceof \ReflectionNamedType ? $r->getName() : (string) $r;

if (0 === $i && InputInterface::class !== $type) {
throw new \LogicException(sprintf('Method "%s::exec()" should declare argument #1 as "%s".', self::class, InputInterface::class));
} elseif (1 === $i && OutputInterface::class !== $type) {
throw new \LogicException(sprintf('Method "%s::exec()" should declare argument #2 as "%s".', self::class, OutputInterface::class));
} elseif (2 <= $i) {
$services[self::class.'::get'.$p->name] = ($p->allowsNull() ? '?' : '').$type;
}
}

if (1 > $i) {
throw new \LogicException(sprintf('Method "%s::exec()" should declare 2 or more arguments.', self::class));
}

return $services;
}
}