2
votes

I've seen this How can i inject dependencies to Symfony Console commands? but that answer doesn't really give enough information and is already explained here http://symfony.com/doc/current/cookbook/console/console_command.html

The problem is a containerAwareCommand doesn't work with the setup here http://symfony.com/doc/current/components/console/introduction.html

In order to use containerAwareCommand from what I can tell, I need my application to use Symfony\Bundle\FrameworkBundle\Console\Application

instead of

Symfony\Component\Console\Application

But using the frameworkBundle Application class requires an instance of KernelInterface and won't allow me to pass in a name and version to my application.

Here is what I have that won't work with containerAwareCommands

#!/usr/bin/env php
<?php

require __DIR__.'/../src/vendor/autoload.php';

$app = new Symfony\Component\Console\Application('spud', '0.0.1');

$app->add(new Isimmons\Spudster\Console\Commands\SayHelloCommand);

$app->run();

The command it's self runs but I get an error when trying to use getContainer

 Call to undefined method Symfony\Component\Console\Application::getKernel()

On a related topic which will probably come up next, The documentation for registering a class in the container shows using a app/config/config.php file. But I don't have an app directory since this is not a full symfony application. My base directory in which all of the app except for the file above is located, is src/lib. If I can figure out the first part above, will symfony be able to find the config file at src/lib/config/config.php?

1
If you're not running a full Symfony application then you should probably state that at the start as people would use their knowledge of Symfony to answer your question.qooplmao
Updated the title. Thanksisimmons
The Symfony console component has no built in support for the dependency injection component. So you will need to build your own container, extend the application class and add the container to it then extend the command class to pull the container from the application.Cerad
While it would be nice if containerAware commands could be added to simple non-framework symfony console commands, I think the real issue is just that it needs to be made more clear in the docs for containerAware commands that they are not the same as, and won't work with the simple commands that are also demonstrated in other parts of the docs. I did finally figure out how to make my own container and container aware commands and will post an answer today.isimmons
@isimmons Too bad you didn't post that answer, it seems I ran into the same issue here.BartBiczBoży

1 Answers

0
votes

You can use Consolefull application.

Consolefull is a simple library to work with Symfony Console Component and Symfony Dependency Injection Component