2

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?

Community
  • 1
  • 1
isimmons
  • 2,016
  • 2
  • 20
  • 32
  • 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 Feb 21 '14 at 03:10
  • 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 Feb 21 '14 at 06:44
  • Your getKernel error message is a bit confusing. There is no kernel in the console component. And adding a container is not going to help. Or mabe it's a copyo? – Cerad Feb 21 '14 at 06:49
  • That getKernel message is from calling $this->getContainer()->get() in the command. I just followed the examples from the symfony docs. – isimmons Feb 21 '14 at 06:58
  • You are trying to mix the component application class with the framework command class. Not going to work. If you want a framework independent console command then you basically need to implement your own container away command and your own container. – Cerad Feb 21 '14 at 12:18
  • Same problem here, ridiculous that such a huge framework have such huge wound.. –  Mar 03 '14 at 14:36
  • 1
    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 Mar 04 '14 at 16:51
  • 2
    @isimmons Too bad you didn't post that answer, it seems I ran into the same issue here. – BartBiczBoży Jan 20 '16 at 04:35

1 Answers1

-1

You can use Consolefull application.

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

  • 2
    you are suggesting a use of another 3rd party library, not what OP is probably asking. whats the symfony way of doing it properly. – DevZer0 Dec 26 '14 at 03:50