1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Mvc\Service;
11
12use Zend\EventManager\EventManagerAwareInterface;
13use Zend\EventManager\EventManagerInterface;
14use Zend\ServiceManager\Config;
15use Zend\ServiceManager\ServiceLocatorAwareInterface;
16use Zend\ServiceManager\ServiceLocatorInterface;
17use Zend\ServiceManager\ServiceManager;
18use Zend\ServiceManager\ServiceManagerAwareInterface;
19use Zend\Stdlib\ArrayUtils;
20
21class ServiceManagerConfig extends Config
22{
23    /**
24     * Services that can be instantiated without factories
25     *
26     * @var array
27     */
28    protected $invokables = array(
29        'SharedEventManager' => 'Zend\EventManager\SharedEventManager',
30    );
31
32    /**
33     * Service factories
34     *
35     * @var array
36     */
37    protected $factories = array(
38        'EventManager'  => 'Zend\Mvc\Service\EventManagerFactory',
39        'ModuleManager' => 'Zend\Mvc\Service\ModuleManagerFactory',
40    );
41
42    /**
43     * Abstract factories
44     *
45     * @var array
46     */
47    protected $abstractFactories = array();
48
49    /**
50     * Aliases
51     *
52     * @var array
53     */
54    protected $aliases = array(
55        'Zend\EventManager\EventManagerInterface'     => 'EventManager',
56        'Zend\ServiceManager\ServiceLocatorInterface' => 'ServiceManager',
57        'Zend\ServiceManager\ServiceManager'          => 'ServiceManager',
58    );
59
60    /**
61     * Shared services
62     *
63     * Services are shared by default; this is primarily to indicate services
64     * that should NOT be shared
65     *
66     * @var array
67     */
68    protected $shared = array(
69        'EventManager' => false,
70    );
71
72    /**
73     * Delegators
74     *
75     * @var array
76     */
77    protected $delegators = array();
78
79    /**
80     * Initializers
81     *
82     * @var array
83     */
84    protected $initializers = array();
85
86    /**
87     * Constructor
88     *
89     * Merges internal arrays with those passed via configuration
90     *
91     * @param  array $configuration
92     */
93    public function __construct(array $configuration = array())
94    {
95        $this->initializers = array(
96            'EventManagerAwareInitializer' => function ($instance, ServiceLocatorInterface $serviceLocator) {
97                if ($instance instanceof EventManagerAwareInterface) {
98                    $eventManager = $instance->getEventManager();
99
100                    if ($eventManager instanceof EventManagerInterface) {
101                        $eventManager->setSharedManager($serviceLocator->get('SharedEventManager'));
102                    } else {
103                        $instance->setEventManager($serviceLocator->get('EventManager'));
104                    }
105                }
106            },
107            'ServiceManagerAwareInitializer' => function ($instance, ServiceLocatorInterface $serviceLocator) {
108                if ($serviceLocator instanceof ServiceManager && $instance instanceof ServiceManagerAwareInterface) {
109                    $instance->setServiceManager($serviceLocator);
110                }
111            },
112            'ServiceLocatorAwareInitializer' => function ($instance, ServiceLocatorInterface $serviceLocator) {
113                if ($instance instanceof ServiceLocatorAwareInterface) {
114                    $instance->setServiceLocator($serviceLocator);
115                }
116            },
117        );
118
119        $this->factories['ServiceManager'] = function (ServiceLocatorInterface $serviceLocator) {
120            return $serviceLocator;
121        };
122
123        parent::__construct(ArrayUtils::merge(
124            array(
125                'invokables'         => $this->invokables,
126                'factories'          => $this->factories,
127                'abstract_factories' => $this->abstractFactories,
128                'aliases'            => $this->aliases,
129                'shared'             => $this->shared,
130                'delegators'         => $this->delegators,
131                'initializers'       => $this->initializers,
132            ),
133            $configuration
134        ));
135    }
136}
137