3

I'm having a problem whilst trying to create a command with Symfony2's Console component, in a full Symfony stack app.

If i try to pass my services in via DI, the command throws the following error when i try to run it:

[Symfony\Component\Debug\Exception\ContextErrorException]  
Notice: Trying to get property of non-object

If I create the command with ContainerAwareCommand and try to get my service with

$this->getContainer()->get('mtgu.api.card.list.response.data');

I get

  [LogicException]                                                               
  The container cannot be retrieved as the application instance is not yet set.

My service is defiantly being loaded, as its used in a front end controller. This problem gets stranger, as if I pass a repository service - I don't get this problem!

Is there some trick to setting up a service to be passible by this? Or have I messed up my configuration somehow?

Im "autoloading" all my services by doing this in my DI Extension rather than including them all through the the main services.yml. I thought this or the ordering of the yml includes maybe effecting it - but I have tried manually including everything but still no joy!

$finder = new Finder();
$finder->name('services.yml');
/**
* @var $file SplFileInfo
*/
foreach($finder->in(__DIR__.'/../') as $file) {
$loader = new Loader\YamlFileLoader($container, new FileLocator($file->getPath()));
$loader->load('services.yml');
}

Vendor/Bundle/Command/services.yml

services:

mtgu.command.slugify:
    class: MightyStudios\MtguBundle\Command\SlugifyCommand
    tags:
        - { name: console.command }
    arguments:
      - @mtgu.api.card.list.response.data

I think this maybe just some config issue, but Google has failed me to find the answer! Has anyone else run into this problem and can they shed any light!?

Many thanks

1 Answer 1

3

A better structure would be to put your services files in the Resources/config directory of your bundle (See also all core bundles). However, that's aside.

The problem is described by the exception: The container cannot be retrieved as the application instance is not yet set. Which is thrown in the ContainerAwareCommand#getContainer() method when $this->application is null. The application is set in the first line of the Application#add() method.

This means that you call $this->getContainer() before you add the command to the application. Maybe you use it in your constructor?

If so, remove it and only use the container in Command#execute(), Command#interact() or Command#initialize().

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it throws that if I use it in the constructor when using the ContainerAwareCommand. But I want to DI the services - so extending Command instead. And that's when it throws the first none object exception.
I've tried creating the command again extending ContainerAwareCommand - and using getContainer in all the places you said but I'm still getting a [Symfony\Component\Debug\Exception\ContextErrorException] Notice: Trying to get property of non-object Error =(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.