Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Di Laravel Package

zendframework/zend-di

zendframework/zend-di is a PHP dependency injection container for Zend Framework apps. It supports autowiring, configurable definitions, and factories to build and wire objects at runtime, helping manage dependencies and improve testability.

View on GitHub
Deep Wiki
Context7

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.

Removal of zend-code dependencies

Before version 3.1, this feature required zend-code, which you can add to your project using Composer:

$ composer require --dev zendframework/zend-code

Since version 3.1 and up, this is no longer required.

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:
$resolver->setContainer($container);
$generator->setOutputDirectory('/path/to/generated/files');
$generator->generate([
    MyClassA::class,
    MyClassB::class,
    // ...
]);

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

$scanner = new DirectoryScanner(__DIR__);
$generator->generate($scanner->getClassNames());

MVC and Expressive integration

When you are using zend-di's ConfigProvider with Expressive or consuming the Module class via zend-mvc, you can obtain the generator instance from the service manager:

$generator = $serviceManager->get(\Zend\Di\CodeGenerator\InjectorGenerator::class);

AoT Config Options

The service factory uses options in your config service, located under the key dependencies.auto.aot. This should be defined as an associative array of options for creating the code generator instance. This array respects the following keys (unknown keys are ignored):

  • namespace: This will be used as base namespace to prefix the namespace of the generated classes. It will be passed to the constructor of Zend\Di\CodeGenerator\InjectorGenerator; the default value is Zend\Di\Generated.

  • directory: The directory where the generated PHP files will be stored. If this value is not provided, you will need to set it with the generator's setOutputDirectory() method before calling generate().

Below is an example detailing configuration of the generator factory:

return [
    'dependencies' => [
        'auto' => [
            'aot' => [
                'namespace' => 'AppAoT\Generated',
                'directory' => __DIR__ . '/../gen',
            ],
        ],
    ],
];

Logging

The InjectorGenerator allows passing a PSR-3 logger instance via an optional fourth constructor parameter.

The generator will log the following information:

  • When a factory is about to be generated for a class or alias (Log level: Debug)
  • When the factory generation caused an exception (Log level: Error)
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests