Code Generator

zend-di comes with Ahead-of-Time (AoT) generators to create optimized code for production. These generators will inspect the provided classes, resolve their dependencies, and generate factories based on these results.


This feature requires zend-code, which you can add to your project using Composer:

$ composer require zendframework/zend-code

Generating an optimized injector

The Zend\Di\CodeGenerator\InjectorGenerator class offers an implementation to generate an optimized injector based on the runtime configuration and a resolver instance.

use Zend\Di\Config;
use Zend\Di\Definition\RuntimeDefinition;
use Zend\Di\Resolver\DependencyResolver;
use Zend\Di\CodeGenerator\InjectorGenerator;

$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config)
$generator = new InjectorGenerator($config, $resolver);

// It is highly recommended to set the container that is used at runtime:
    // ...

You can also utilize Zend\Code\Scanner to scan your code for classes:

$scanner = new DirectoryScanner(__DIR__);

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!