-
Notifications
You must be signed in to change notification settings - Fork 0
Docs Cli Injection
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.