Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Docs Cli Injection

Frank Kleine edited this page Apr 7, 2012 · 1 revision

Dependency injection for commands

Most times you implement a command line script you require other classes for the work - your dependencies. So, when we have support for dependency injection all the way down to web applications, while not have this for command line script. To get dependency injection for command line scripts you should not instantiate your class directly but use the net::stubbles::ioc::stubApp::createInstance() method for this:

$myCommand = stubApp::createInstance('my::package::MyCommand', $projectPath);

This will create an instance of my::package::MyCommand with the IoC container of Stubbles. The first parameter tells which class to create an instance of, the second parameter denotes the path to the project which should be used.

But where are the bindings? To tell the IoC container about your bindings your command needs to have a static ___bindings() method:

class MyCommand extends stubBaseObject implements stubConsoleCommand
{
    /**
***return list of bindings required for this command
     *
***@param   string                           $projectPath
***@return  array<string|stubBindingModule>
***/
    public static function __bindings($projectPath)
    {
        return array(new stubPropertiesBindingModule($projectPath),
                     new stubLogBindingModule($projectPath . '/log/sla')
               );
    }

    /**
***runs the command
***/
    public function run() { ... }

    ...other methods...
}

The __bindings() method returns a list of binding modules which should be used to create the bindings.

Clone this wiki locally