PHP code example of aimfeld / zend-di-compiler

1. Go to this page and download the library: Download aimfeld/zend-di-compiler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aimfeld / zend-di-compiler example snippets


'modules' => array(
    'SomeModule',
    'Application',
    'ZendDiCompiler',
),

class ServiceF
{
    public function __construct(ExampleInterface $example)
    {
        // ExampleImplementor is injected since it is a type preference for ExampleInterface
        $this->example = $example;
    }
}

'di' => array(
    'instance' => array(
        'preference' => array(
            'ZendDiCompiler\Example\ExampleInterface' => 'ZendDiCompiler\Example\ExampleImplementor',
        ),
    ),
),

'di' => array(
    'instance' => array(
        'preference' => array(
            'ZendDiCompiler\Example\NotExists' => 'ZendDiCompiler\Example\Exists',
        ),
    ),
),

use Laminas\Mvc\Controller\AbstractActionController;
use ZendDiCompiler\ZendDiCompiler;
use Laminas\Config\Config;

class ExampleController extends AbstractActionController
{
    public function __construct(ZendDiCompiler $zendDiCompiler, ServiceA $serviceA, ServiceC $serviceC, Config $config)
    {
        $this->zendDiCompiler = $zendDiCompiler;
        $this->serviceA = $serviceA;
        $this->serviceC = $serviceC;
        $this->config = $config;
    }
}


class ServiceA
{
    public function __construct(ServiceB $serviceB)
    {
        $this->serviceB = $serviceB;
    }
}

class ServiceB
{
    public function __construct($diParam)
    {
        $this->diParam = $diParam;
    }
}

class ServiceC
{
    public function init(MvcEvent $mvcEvent)
    {
        $sm = $mvcEvent->getApplication()->getServiceManager();
        $router = $mvcEvent->getRouter();
        // Some complicated bootstrapping using e.g. the service manager and the router
    }
}

// ZendDiCompiler configuration
'zendDiCompiler' => array(
    // Directories that will be code-scanned
    'scanDirectories' => array(
        // e.g. 'vendor/provider/module/src',
        __DIR__ . '/../src/ZendDiCompiler/Example',
    ),
),
// ZF2 DI definition and instance configuration used by ZendDiCompiler
'di' => array(
    // Instance configuration
    'instance' => array(
        // Type preferences for abstract classes and interfaces.
        'preference' => array(
            'ZendDiCompiler\Example\ExampleInterface' => 'ZendDiCompiler\Example\ExampleImplementor',
        ),
        // Add instance configuration if there are specific parameters to be used for instance creation.
        'ZendDiCompiler\Example\ServiceB' => array('parameters' => array(
            'diParam' => 'Hello',
        )),
    ),
),

class Module
{
    protected $zendDiCompiler;

    public function getControllerConfig()
    {
        return array(
            'factories' => array(
                // Suppose one of our routes specifies a controller named 'ExampleController'
                'ExampleController' => function() {
                    return $this->zendDiCompiler->get('ZendDiCompiler\Example\ExampleController');
                },
            ),
        );
    }

    public function onBootstrap(MvcEvent $mvcEvent)
    {
        $sm = $mvcEvent->getApplication()->getServiceManager();

        // Provide ZendDiCompiler as a local variable for convience
        $this->zendDiCompiler = $sm->get('ZendDiCompiler');

        // Set up shared instance
        $serviceC = new ServiceC;
        $serviceC->init($mvcEvent);

        // Provide shared instance
        $this->zendDiCompiler->addSharedInstances(array(
            'ZendDiCompiler\Example\ServiceC' => $serviceC,
        ));
    }
}

class RuntimeA
{
    public function __construct(Config $config, ServiceA $serviceA, array $zdcParams = array())
    {
        $this->config = $config;
        $this->serviceA = $serviceA;
        $this->params = $zdcParams;
    }
}

use ZendDiCompiler\DiFactory;

class ServiceD
{
    public function __construct(DiFactory $diFactory)
    {
        $this->diFactory = $diFactory;
    }

    public function serviceMethod()
    {
        $runtimeA1 = $this->diFactory->create('ZendDiCompiler\Example\RuntimeA', array('hello', 'world'));
        $runtimeA2 = $this->diFactory->create('ZendDiCompiler\Example\RuntimeA', array('goodbye', 'world'));
    }
}

class RuntimeB
{
    public function __construct(Config $config, ServiceA $serviceA, $runtimeParam1, $runtimeParam2)
    {
        $this->config = $config;
        $this->serviceA = $serviceA;
        $this->runtimeParam1 = $runtimeParam1;
        $this->runtimeParam2 = $runtimeParam2;
    }
}

class ExampleDiFactory extends DiFactory
{
    /**
     * @param string $runtimeParam1
     * @param int $runtimeParam2
     * @return RuntimeB
     */
    public function createRuntimeB($runtimeParam1, $runtimeParam2)
    {
        $config = $this->zendDiCompiler->get('Laminas\Config\Config');
        $serviceA = $this->zendDiCompiler->get('ZendDiCompiler\Example\ServiceA');
        return new RuntimeB($config, $serviceA, $runtimeParam1, $runtimeParam2);
    }
}

class ServiceE
{
    public function __construct(ExampleDiFactory $diFactory)
    {
        $this->diFactory = $diFactory;
    }

    public function serviceMethod()
    {
        $runtimeB1 = $this->diFactory->createRuntimeB('one', 1);
        $runtimeB2 = $this->diFactory->createRuntimeB('two', 2);
    }
}

namespace ZendDiCompiler;

use Laminas\Di\ServiceLocator;

/**
 * Generated by ZendDiCompiler\Generator (2013-03-07 21:11:39)
 */
class GeneratedServiceLocator extends ServiceLocator
{
    /**
     * @param string $name
     * @param array $params
     * @param bool $newInstance
     * @return mixed
     */
    public function get($name, array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services[$name])) {
            return $this->services[$name];
        }

        switch ($name) {
            case 'ZendDiCompiler\Example\ExampleController':
                return $this->getZendDiCompilerExampleExampleController($params, $newInstance);

            case 'ZendDiCompiler\Example\ExampleDiFactory':
                return $this->getZendDiCompilerExampleExampleDiFactory($params, $newInstance);

            case 'ZendDiCompiler\Example\ExampleImplementor':
                return $this->getZendDiCompilerExampleExampleImplementor($params, $newInstance);

            case 'ZendDiCompiler\Example\Module':
                return $this->getZendDiCompilerExampleModule($params, $newInstance);

            case 'ZendDiCompiler\Example\RuntimeA':
                return $this->getZendDiCompilerExampleRuntimeA($params, $newInstance);

            case 'ZendDiCompiler\Example\ServiceA':
                return $this->getZendDiCompilerExampleServiceA($params, $newInstance);

            case 'ZendDiCompiler\Example\ServiceB':
                return $this->getZendDiCompilerExampleServiceB($params, $newInstance);

            case 'ZendDiCompiler\Example\ServiceC':
                return $this->getZendDiCompilerExampleServiceC($params, $newInstance);

            case 'ZendDiCompiler\Example\ServiceD':
                return $this->getZendDiCompilerExampleServiceD($params, $newInstance);

            case 'ZendDiCompiler\Example\ServiceE':
                return $this->getZendDiCompilerExampleServiceE($params, $newInstance);

            case 'ZendDiCompiler\Example\ServiceF':
                return $this->getZendDiCompilerExampleServiceF($params, $newInstance);

            default:
                return parent::get($name, $params);
        }
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ExampleController
     */
    public function getZendDiCompilerExampleExampleController(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ExampleController'])) {
            return $this->services['ZendDiCompiler\Example\ExampleController'];
        }

        $object = new \ZendDiCompiler\Example\ExampleController($this->get('ZendDiCompiler\ZendDiCompiler'), $this->getZendDiCompilerExampleServiceA(), $this->getZendDiCompilerExampleServiceC(), $this->get('Laminas\Config\Config'));
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ExampleController'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ExampleDiFactory
     */
    public function getZendDiCompilerExampleExampleDiFactory(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ExampleDiFactory'])) {
            return $this->services['ZendDiCompiler\Example\ExampleDiFactory'];
        }

        $object = new \ZendDiCompiler\Example\ExampleDiFactory($this->get('ZendDiCompiler\ZendDiCompiler'));
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ExampleDiFactory'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ExampleImplementor
     */
    public function getZendDiCompilerExampleExampleImplementor(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ExampleImplementor'])) {
            return $this->services['ZendDiCompiler\Example\ExampleImplementor'];
        }

        $object = new \ZendDiCompiler\Example\ExampleImplementor();
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ExampleImplementor'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\Module
     */
    public function getZendDiCompilerExampleModule(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\Module'])) {
            return $this->services['ZendDiCompiler\Example\Module'];
        }

        $object = new \ZendDiCompiler\Example\Module();
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\Module'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\RuntimeA
     */
    public function getZendDiCompilerExampleRuntimeA(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\RuntimeA'])) {
            return $this->services['ZendDiCompiler\Example\RuntimeA'];
        }

        $object = new \ZendDiCompiler\Example\RuntimeA($this->get('Laminas\Config\Config'), $this->getZendDiCompilerExampleServiceA(), $params);
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\RuntimeA'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ServiceA
     */
    public function getZendDiCompilerExampleServiceA(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ServiceA'])) {
            return $this->services['ZendDiCompiler\Example\ServiceA'];
        }

        $object = new \ZendDiCompiler\Example\ServiceA($this->getZendDiCompilerExampleServiceB());
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ServiceA'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ServiceB
     */
    public function getZendDiCompilerExampleServiceB(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ServiceB'])) {
            return $this->services['ZendDiCompiler\Example\ServiceB'];
        }

        $object = new \ZendDiCompiler\Example\ServiceB('Hello');
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ServiceB'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ServiceC
     */
    public function getZendDiCompilerExampleServiceC(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ServiceC'])) {
            return $this->services['ZendDiCompiler\Example\ServiceC'];
        }

        $object = new \ZendDiCompiler\Example\ServiceC();
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ServiceC'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ServiceD
     */
    public function getZendDiCompilerExampleServiceD(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ServiceD'])) {
            return $this->services['ZendDiCompiler\Example\ServiceD'];
        }

        $object = new \ZendDiCompiler\Example\ServiceD($this->get('ZendDiCompiler\DiFactory'));
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ServiceD'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ServiceE
     */
    public function getZendDiCompilerExampleServiceE(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ServiceE'])) {
            return $this->services['ZendDiCompiler\Example\ServiceE'];
        }

        $object = new \ZendDiCompiler\Example\ServiceE($this->getZendDiCompilerExampleExampleDiFactory());
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ServiceE'] = $object;
        }

        return $object;
    }

    /**
     * @param array $params
     * @param bool $newInstance
     * @return \ZendDiCompiler\Example\ServiceF
     */
    public function getZendDiCompilerExampleServiceF(array $params = array(), $newInstance = false)
    {
        if (!$newInstance && isset($this->services['ZendDiCompiler\Example\ServiceF'])) {
            return $this->services['ZendDiCompiler\Example\ServiceF'];
        }

        $object = new \ZendDiCompiler\Example\ServiceF($this->getZendDiCompilerExampleExampleImplementor());
        if (!$newInstance) {
            $this->services['ZendDiCompiler\Example\ServiceF'] = $object;
        }

        return $object;
    }
}