Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit dcdf348

Browse files
committed
Re-added Config class
- Default `ConfigInterface` implementation. Extracts the relevant keys from the provided configuration array to create a configuration array to pass to the service manager's `withConfig()` method, returning the result.
1 parent b25d50c commit dcdf348

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

src/Config.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
10+
namespace Zend\ServiceManager;
11+
12+
class Config implements ConfigInterface
13+
{
14+
/**
15+
* @var array
16+
*/
17+
protected $config = [];
18+
19+
/**
20+
* Constructor
21+
*
22+
* @param array $config
23+
*/
24+
public function __construct($config = [])
25+
{
26+
$this->config = $config;
27+
}
28+
29+
/**
30+
* Configure service manager
31+
*
32+
* @param ServiceManager $serviceManager
33+
* @return ServiceManager Returns a new instance with the merged configuration.
34+
*/
35+
public function configureServiceManager(ServiceManager $serviceManager)
36+
{
37+
$config = [];
38+
$abstractFactories = isset($this->config['abstract_factories']) ? $this->config['abstract_factories'] : [];
39+
$aliases = isset($this->config['aliases']) ? $this->config['aliases'] : [];
40+
$delegators = isset($this->config['delegators']) ? $this->config['delegators'] : [];
41+
$factories = isset($this->config['factories']) ? $this->config['factories'] : [];
42+
$initializers = isset($this->config['initializers']) ? $this->config['initializers'] : [];
43+
$invokables = isset($this->config['invokables']) ? $this->config['invokables'] : [];
44+
$lazyServices = isset($this->config['lazy_services']) ? $this->config['lazy_services'] : [];
45+
$services = isset($this->config['services']) ? $this->config['services'] : [];
46+
$shared = isset($this->config['shared']) ? $this->config['shared'] : [];
47+
48+
if (! empty($abstractFactories)) {
49+
$config['abstract_factories'] = $abstractFactories;
50+
}
51+
52+
if (! empty($aliases)) {
53+
$config['aliases'] = $aliases;
54+
}
55+
56+
if (! empty($delegators)) {
57+
$config['delegators'] = $delegators;
58+
}
59+
60+
if (! empty($factories)) {
61+
$config['factories'] = $factories;
62+
}
63+
64+
if (! empty($initializers)) {
65+
$config['initializers'] = $initializers;
66+
}
67+
68+
if (! empty($invokables)) {
69+
$config['invokables'] = $invokables;
70+
}
71+
72+
if (! empty($lazyServices)) {
73+
$config['lazy_services'] = $lazyServices;
74+
}
75+
76+
if (! empty($services)) {
77+
$config['services'] = $services;
78+
}
79+
80+
if (! empty($shared)) {
81+
$config['shared'] = $shared;
82+
}
83+
84+
return $serviceManager->withConfig($config);
85+
}
86+
}

test/ConfigTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
10+
namespace ZendTest\ServiceManager;
11+
12+
use PHPUnit_Framework_TestCase as TestCase;
13+
use Zend\ServiceManager\Config;
14+
use Zend\ServiceManager\ServiceManager;
15+
16+
class ConfigTest extends TestCase
17+
{
18+
public function testPassesKnownServiceConfigKeysToServiceManagerWithConfigMethod()
19+
{
20+
$expected = [
21+
'abstract_factories' => [
22+
__CLASS__,
23+
__NAMESPACE__,
24+
],
25+
'aliases' => [
26+
'foo' => __CLASS__,
27+
'bar' => __NAMESPACE__,
28+
],
29+
'delegators' => [
30+
'foo' => [
31+
__CLASS__,
32+
__NAMESPACE__,
33+
]
34+
],
35+
'factories' => [
36+
'foo' => __CLASS__,
37+
'bar' => __NAMESPACE__,
38+
],
39+
'initializers' => [
40+
__CLASS__,
41+
__NAMESPACE__,
42+
],
43+
'invokables' => [
44+
'foo' => __CLASS__,
45+
'bar' => __NAMESPACE__,
46+
],
47+
'lazy_services' => [
48+
'class_map' => [
49+
__CLASS__ => __CLASS__,
50+
__NAMESPACE__ => __NAMESPACE__,
51+
],
52+
],
53+
'services' => [
54+
'foo' => $this,
55+
],
56+
'shared' => [
57+
__CLASS__ => true,
58+
__NAMESPACE__ => false,
59+
],
60+
];
61+
62+
$config = $expected + [
63+
'foo' => 'bar',
64+
'baz' => 'bat',
65+
];
66+
67+
$services = $this->prophesize(ServiceManager::class);
68+
$services->withConfig($expected)->willReturn('CALLED');
69+
70+
$configuration = new Config($config);
71+
$this->assertEquals('CALLED', $configuration->configureServiceManager($services->reveal()));
72+
}
73+
}

0 commit comments

Comments
 (0)