Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 169 lines (140 sloc) 5.019 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Di\ServiceLocator;

use Zend\Di\Di;
use Zend\Di\Exception;

/**
* Proxy used to analyze how instances are created by a given Di. Overrides Zend\Di\Di to produce artifacts that
* represent the process used to instantiate a particular instance
*/
class DependencyInjectorProxy extends Di
{
    /**
* @var Di
*/
    protected $di;

    /**
* @param Di $di
*/
    public function __construct(Di $di)
    {
        $this->di = $di;
        $this->definitions = $di->definitions();
        $this->instanceManager = $di->instanceManager();
    }

    /**
* {@inheritDoc}
* @return GeneratorInstance
*/
    public function get($name, array $params = array())
    {
        return parent::get($name, $params);
    }

    /**
* {@inheritDoc}
* @return GeneratorInstance
*/
    public function newInstance($name, array $params = array(), $isShared = true)
    {
        $instance = parent::newInstance($name, $params, $isShared);

        if ($instance instanceof GeneratorInstance) {
            /* @var $instance GeneratorInstance */
            $instance->setShared($isShared);

            // When a callback is used, we don't know instance the class name.
            // That's why we assume $name as the instance alias
            if (null === $instance->getName()) {
                $instance->setAlias($name);
            }
        }

        return $instance;
    }

    /**
* {@inheritDoc}
* @return GeneratorInstance
*/
    public function createInstanceViaConstructor($class, $params, $alias = null)
    {
        $callParameters = array();

        if ($this->di->definitions->hasMethod($class, '__construct')
            && (count($this->di->definitions->getMethodParameters($class, '__construct')) > 0)
        ) {
            $callParameters = $this->resolveMethodParameters($class, '__construct', $params, $alias, true, true);
            $callParameters = $callParameters ?: array();
        }

        return new GeneratorInstance($class, $alias, '__construct', $callParameters);
    }

    /**
* {@inheritDoc}
* @throws \Zend\Di\Exception\InvalidCallbackException
* @return GeneratorInstance
*/
    public function createInstanceViaCallback($callback, $params, $alias)
    {
        if (is_string($callback)) {
            $callback = explode('::', $callback);
        }

        if (!is_callable($callback)) {
            throw new Exception\InvalidCallbackException('An invalid constructor callback was provided');
        }

        if (!is_array($callback) || is_object($callback[0])) {
            throw new Exception\InvalidCallbackException(
                'For purposes of service locator generation, constructor callbacks must refer to static methods only'
            );
        }

        $class = $callback[0];
        $method = $callback[1];

        $callParameters = array();
        if ($this->di->definitions->hasMethod($class, $method)) {
            $callParameters = $this->resolveMethodParameters($class, $method, $params, $alias, true, true);
        }

        $callParameters = $callParameters ?: array();

        return new GeneratorInstance(null, $alias, $callback, $callParameters);
    }

    /**
* {@inheritDoc}
*/
    public function handleInjectionMethodForObject($class, $method, $params, $alias, $isRequired)
    {
        return array(
            'method' => $method,
            'params' => $this->resolveMethodParameters($class, $method, $params, $alias, $isRequired),
        );
    }

    /**
* {@inheritDoc}
*/
    protected function resolveAndCallInjectionMethodForInstance($instance, $method, $params, $alias, $methodIsRequired, $methodClass = null)
    {
        if (!$instance instanceof GeneratorInstance) {
            return parent::resolveAndCallInjectionMethodForInstance($instance, $method, $params, $alias, $methodIsRequired, $methodClass);
        }

        /* @var $instance GeneratorInstance */
        $methodClass = $instance->getClass();
        $callParameters = $this->resolveMethodParameters($methodClass, $method, $params, $alias, $methodIsRequired);

        if ($callParameters !== false) {
            $instance->addMethod(array(
                'method' => $method,
                'params' => $callParameters,
            ));

            return true;
        }

        return false;
    }

    /**
* {@inheritDoc}
*/
    protected function getClass($instance)
    {
        if ($instance instanceof GeneratorInstance) {
            /* @var $instance GeneratorInstance */

            return $instance->getClass();
        }

        return parent::getClass($instance);
    }
}
Something went wrong with that request. Please try again.