Kdyby / Console

Symfony Console integration for Kdyby components
https://packagist.org/packages/kdyby/console
Other
52 stars 54 forks source link

Command cannot depend on other command #48

Open foglcz opened 8 years ago

foglcz commented 8 years ago

So I have interesting use-case. I have a command, which I want to execute from other command -- because one is maintenance and one is a one-time migration, which is quite extensive (essentially, we're upgrading systems.)

Soooo, I've did this:

console:
    commands:
        - App\Commands\Maintenance\Focus\Paths(%focusPaths%, @focusDibi.connection)
        - App\Commands\MigrationScripts\Y2016Q1\CleanupFocus(%focusPaths%, @focusDibi.connection)

Constructor looks like this:

namespace App\Commands\MigrationScripts\Y2016Q1;

use App\Commands\Maintenance\Focus\Paths;
use Dibi\Connection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CleanupFocus extends Command
{
    /** @var Paths */
    private $pathsCommand;

    public function __construct(array $paths, Connection $focusDibi, Paths $pathsCommand)
    {
        parent::__construct('migrate:y2016q1:focus');
        $this->setDescription('Migrations for FOCUS database, 2016/Q1, the infrastructure overhaul');
        $this->pathsCommand = $pathsCommand;
        // ...
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        // ...

        // set options & run the maintenance
        $input->setOption('all', true);
        $input->setOption('force', true);
        $this->pathsCommand->execute($input, $output);

        // ...
    }
}

BUT the DI builder throws a following exception:

[2016-02-08 19-50-14] Nette\DI\ServiceCreationException:
Service 'console.command.0':
Service of type App\Commands\Maintenance\Focus\Paths needed by App\Commands\MigrationScripts\Y2016Q1\CleanupFocus::__construct() not found. 
Did you register it in configuration file? 

Stack trace:
#0 C:\Users\pptacek\www\projectname,ha!\vendor\nette\di\src\DI\Compiler.php(213): Nette\DI\ContainerBuilder->generateClasses(NULL, NULL)
#1 C:\Users\pptacek\www\projectname,ha!\vendor\nette\di\src\DI\Compiler.php(139): Nette\DI\Compiler->generateCode(NULL, NULL)
#2 C:\Users\pptacek\www\projectname,ha!\vendor\nette\bootstrap\src\Bootstrap\Configurator.php(264): Nette\DI\Compiler->compile()
#3 [internal function]: Nette\Configurator->generateContainer(Object(Nette\DI\Compiler))
#4 C:\Users\pptacek\www\projectname,ha!\vendor\nette\di\src\DI\ContainerLoader.php(111): call_user_func_array(Array, Array)
#5 C:\Users\pptacek\www\projectname,ha!\vendor\nette\di\src\DI\ContainerLoader.php(76): Nette\DI\ContainerLoader->generate('Container_168f2...', Array)
#6 C:\Users\pptacek\www\projectname,ha!\vendor\nette\di\src\DI\ContainerLoader.php(41): Nette\DI\ContainerLoader->loadFile('Container_168f2...', Array)
#7 C:\Users\pptacek\www\projectname,ha!\vendor\nette\bootstrap\src\Bootstrap\Configurator.php(218): Nette\DI\ContainerLoader->load(Array, Array)
#8 C:\Users\pptacek\www\projectname,ha!\app\bootstrap.php(23): Nette\Configurator->createContainer()
#9 C:\Users\pptacek\www\projectname,ha!\www\index.php(6): require('C:\\Users\\pptace...')
#10 {main}
(stored in C:\Users\pptacek\www\projectname,ha!\app\..\log\exception--2016-02-08--19-50--1b383ca34c.html)

If you'd point me to the proper point where I can fix this one, I'd apprecieate it. Cannot continue on it today, but will jump on it first thing tomorrow morning (basically - I need it working :)) )

Creating an issue, since in services section of Nette DI, you can reference other services from constructors -- so this is solvable, somehow.

I know, hotfix might be using @inject - didn't test.

fprochazka commented 8 years ago

You could in fact name the command and pass it using the name reference.

console:
    commands:
        paths: App\Commands\Maintenance\Focus\Paths(%focusPaths%, @focusDibi.connection)
        - App\Commands\MigrationScripts\Y2016Q1\CleanupFocus(%focusPaths%, @focusDibi.connection, @console.command.paths)

But allowing autowiring might not be a bad idea, as I really see no reason to disallow it.

fprochazka commented 7 years ago

I will remove the disabling of autowiring, but this might cause some problems => it should go to next major.