ServiceManagerConfig.php 9.72 KB
Newer Older
Ketan's avatar
Ketan committed
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Mvc\Service;

use Interop\Container\ContainerInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\SharedEventManager;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\ModuleManager\Listener\ServiceListener;
use Zend\ModuleManager\ModuleManager;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\Stdlib\ArrayUtils;

class ServiceManagerConfig extends Config
{

    /**
     * Default service configuration.
     *
     * In addition to these, the constructor registers several factories and
     * initializers; see that method for details.
     *
     * @var array
     */
    protected $config = [
        'abstract_factories' => [],
        'aliases'            => [
            'EventManagerInterface'            => EventManager::class,
            EventManagerInterface::class       => 'EventManager',
            ModuleManager::class               => 'ModuleManager',
            ServiceListener::class             => 'ServiceListener',
            SharedEventManager::class          => 'SharedEventManager',
            'SharedEventManagerInterface'      => 'SharedEventManager',
            SharedEventManagerInterface::class => 'SharedEventManager',
        ],
        'delegators' => [],
        'factories'  => [
            'EventManager'            => EventManagerFactory::class,
            'ModuleManager'           => ModuleManagerFactory::class,
            'ServiceListener'         => ServiceListenerFactory::class,
        ],
        'lazy_services' => [],
        'initializers'  => [],
        'invokables'    => [],
        'services'      => [],
        'shared'        => [
            'EventManager' => false,
        ],
    ];

    /**
     * Constructor
     *
     * Merges internal arrays with those passed via configuration, and also
     * defines:
     *
     * - factory for the service 'SharedEventManager'.
     * - initializer for EventManagerAwareInterface implementations
     * - initializer for ServiceManagerAwareInterface implementations
     * - initializer for ServiceLocatorAwareInterface implementations
     *
     * @param  array $config
     */
    public function __construct(array $config = [])
    {
        $this->config['factories']['ServiceManager'] = function ($container) {
            return $container;
        };

        $this->config['factories']['SharedEventManager'] = function () {
            return new SharedEventManager();
        };

        $this->config['initializers'] = ArrayUtils::merge($this->config['initializers'], [
            'EventManagerAwareInitializer' => function ($first, $second) {
                if ($first instanceof ContainerInterface) {
                    $container = $first;
                    $instance = $second;
                } else {
                    $container = $second;
                    $instance = $first;
                }

                if (! $instance instanceof EventManagerAwareInterface) {
                    return;
                }

                $eventManager = $instance->getEventManager();

                // If the instance has an EM WITH an SEM composed, do nothing.
                if ($eventManager instanceof EventManagerInterface
                    && $eventManager->getSharedManager() instanceof SharedEventManagerInterface
                ) {
                    return;
                }

                $instance->setEventManager($container->get('EventManager'));
            },
            'ServiceManagerAwareInitializer' => function ($first, $second) {
                if ($first instanceof ContainerInterface) {
                    // zend-servicemanager v3
                    $container = $first;
                    $instance = $second;
                } else {
                    // zend-servicemanager v2
                    $container = $second;
                    $instance = $first;
                }

                if ($container instanceof ServiceManager && $instance instanceof ServiceManagerAwareInterface) {
                    trigger_error(sprintf(
                        'ServiceManagerAwareInterface is deprecated and will be removed in version 3.0, along '
                        . 'with the ServiceManagerAwareInitializer. Please update your class %s to remove '
                        . 'the implementation, and start injecting your dependencies via factory instead.',
                        get_class($instance)
                    ), E_USER_DEPRECATED);
                    $instance->setServiceManager($container);
                }
            },
            'ServiceLocatorAwareInitializer' => function ($first, $second) {
                if ($first instanceof AbstractPluginManager) {
                    // Edge case under zend-servicemanager v2
                    $container = $second;
                    $instance = $first;
                } elseif ($first instanceof ContainerInterface) {
                    // zend-servicemanager v3
                    $container = $first;
                    $instance = $second;
                } else {
                    // zend-servicemanager v2
                    $container = $second;
                    $instance = $first;
                }

                // For service locator aware classes, inject the service
                // locator, but emit a deprecation notice. Skip plugin manager
                // implementations; they're dealt with later.
                if ($instance instanceof ServiceLocatorAwareInterface
                    && ! $instance instanceof AbstractPluginManager
                ) {
                    trigger_error(sprintf(
                        'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
                        . 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove '
                        . 'the implementation, and start injecting your dependencies via factory instead.',
                        get_class($instance)
                    ), E_USER_DEPRECATED);
                    $instance->setServiceLocator($container);
                }

                // For service locator aware plugin managers that do not have
                // the service locator already injected, inject it, but emit a
                // deprecation notice.
                if ($instance instanceof ServiceLocatorAwareInterface
                    && $instance instanceof AbstractPluginManager
                    && ! $instance->getServiceLocator()
                ) {
                    trigger_error(sprintf(
                        'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
                        . 'with the ServiceLocatorAwareInitializer. Please update your %s plugin manager factory '
                        . 'to inject the parent service locator via the constructor.',
                        get_class($instance)
                    ), E_USER_DEPRECATED);
                    $instance->setServiceLocator($container);
                }
            },
        ]);

        // In zend-servicemanager v2, incoming configuration is not merged
        // with existing; it replaces. So we need to detect that and merge.
        if (method_exists($this, 'getAllowOverride')) {
            $config = ArrayUtils::merge($this->config, $config);
        }

        parent::__construct($config);
    }

    /**
     * Configure service container.
     *
     * Uses the configuration present in the instance to configure the provided
     * service container.
     *
     * Before doing so, it adds a "service" entry for the ServiceManager class,
     * pointing to the provided service container.
     *
     * @param ServiceManager $services
     * @return ServiceManager
     */
    public function configureServiceManager(ServiceManager $services)
    {
        $this->config['services'][ServiceManager::class] = $services;

        /*
        printf("Configuration prior to configuring servicemanager:\n");
        foreach ($this->config as $type => $list) {
            switch ($type) {
                case 'aliases':
                case 'delegators':
                case 'factories':
                case 'invokables':
                case 'lazy_services':
                case 'services':
                case 'shared':
                    foreach (array_keys($list) as $name) {
                        printf("    %s (%s)\n", $name, $type);
                    }
                    break;

                case 'initializers':
                case 'abstract_factories':
                    foreach ($list as $callable) {
                        printf("    %s (%s)\n", (is_object($callable) ? get_class($callable) : $callable), $type);
                    }
                    break;

                default:
                    break;
            }
        }
         */

        // This is invoked as part of the bootstrapping process, and requires
        // the ability to override services.
        $services->setAllowOverride(true);
        parent::configureServiceManager($services);
        $services->setAllowOverride(false);

        return $services;
    }

    /**
     * Return all service configuration (v3)
     *
     * @return array
     */
    public function toArray()
    {
        return $this->config;
    }
}