middleout/mdo-bundle-zf2-auto-dep-inject

A Zend Framework 2 Module that allows you to skip the defining factories for your classes (but allows you to still use them) and it injects them based on the constructor.

1.0.4 2015-01-03 13:51 UTC

README

A Zend Framework 2 Module that allows you to skip the defining factories for your classes (but allows you to still use them) and it injects them based on the constructor. The resulting code is similar to angular's injector system if you are familiar with it.

Features

  • No more controllers mappign to classes
  • No more services (or any other class) mapping in the service manager
  • No more closures or factory classes to create in order to instantiate a class
  • It still allows you to create the factories if you DO need a custom building schema of the system
  • Bypasses the service manager completely if there is no configuration defined for a requested class
  • Injects dependencies in the constructor of any class starting from the controller down to any class
  • Injects based on the fully qualified namespace type hint.
  • Injects also based on symbol in the case of no class type hint given
  • Does not auto inject nor fail on optional parameters (this makes it compatible with any ZF Class)
  • Once an object is created, it is put in the ZF Service Manager in order to take advantage of all the features it offers (including not loading the same class 2 times)
  • The main difference is that all objects are created from scratch everytime you ask for them. To prevent this, you can just add in your service managerm in the "services" => "shared" array, your FULL class name that you want to have its instance shared.

Examples

// this class has not been defined in the Service Manager yet it is still loaded properly
class IndexController extends AbstractActionController {

    /**
     * @var SomeService $someService
     */
    private $someService;

    /**
     * @param SomeService $someService
     */
    public function __construct(SomeService $someService, $config, $auth) {
        $this->someService = $someService;
        $this->config = $config; // the key "config" is available in the ZF Service Manager by default
        $this->auth = $auth; // we assume we have the key "auth" in the service manager aliases
    }

    public function indexAction() {
        $this->someService->doSomething();
    }
}

class SomeService {

    private $someOtherService;

    public function __construct(SomeOtherService $someOtherService) {
        //this gets called BEFORE the controller's __construct()
        $this->someOtherService = $someOtherService;
    }
}

##Install (composer)

  • composer install mdo-zf2-auto-dependency-injector

##Install (manual)

  • download the module from github
  • put it in your zf2 "module" directory
  • in your application.config.php add your new module "MdoAutoDependencyInjector"
  • remove all references to your controllers
  • test your application
  • you can then granullary start removing more of your classes from the service manager
  • the rule of the thumb is to keep in the SM only aliases to factories or literally your factories (either closures or links to factory classses)

That's all !

Issues

License

Middleout Zend Framework 2 Auto Dependency Injector is released under the Apache License.