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

Commit f666f93

Browse files
committed
Merge branch 'feature/15' into develop
Close #15
2 parents 6a54283 + 5bcc8c9 commit f666f93

File tree

13 files changed

+281
-102
lines changed

13 files changed

+281
-102
lines changed

.travis.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,34 @@ cache:
1111
directories:
1212
- $HOME/.composer/cache
1313

14+
env:
15+
global:
16+
- CODE_VERSION="^3.0"
17+
1418
matrix:
1519
fast_finish: true
1620
include:
1721
- php: 5.5
1822
env:
1923
- EXECUTE_CS_CHECK=true
24+
- php: 5.5
25+
env:
26+
- CODE_VERSION="^2.6"
2027
- php: 5.6
2128
env:
2229
- EXECUTE_TEST_COVERALLS=true
30+
- php: 5.6
31+
env:
32+
- CODE_VERSION="^2.6"
2333
- php: 7
24-
- php: hhvm
25-
allow_failures:
2634
- php: 7
35+
env:
36+
- CODE_VERSION="^2.6"
37+
- php: hhvm
38+
- php: hhvm
39+
env:
40+
- CODE_VERSION="^2.6"
41+
allow_failures:
2742
- php: hhvm
2843

2944
notifications:
@@ -34,6 +49,7 @@ before_install:
3449
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
3550
- composer self-update
3651
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi
52+
- composer require --no-update "zendframework/zend-code:$CODE_VERSION"
3753

3854
install:
3955
- travis_retry composer install --no-interaction --ignore-platform-reqs

CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file, in reverse chronological order by release.
4+
5+
## 2.6.0 - 2016-02-23
6+
7+
### Added
8+
9+
- [#16](https://github.com/zendframework/zend-di/pull/16) adds container-interop
10+
as a dependency, and updates the `LocatorInterface` to extend
11+
`Interop\Container\ContainerInterface`. This required adding the following
12+
methods:
13+
- `Zend\Di\Di::has()`
14+
- `Zend\Di\ServiceLocator::has()`
15+
16+
### Deprecated
17+
18+
- Nothing.
19+
20+
### Removed
21+
22+
- [#15](https://github.com/zendframework/zend-di/pull/15) and
23+
[#16](https://github.com/zendframework/zend-di/pull/16) remove most
24+
development dependencies, as the functionality could be reproduced with
25+
generic test assets or PHP built-in classes. These include:
26+
- zend-config
27+
- zend-db
28+
- zend-filter
29+
- zend-log
30+
- zend-mvc
31+
- zend-view
32+
- zend-servicemanager
33+
34+
### Fixed
35+
36+
- [#16](https://github.com/zendframework/zend-di/pull/16) updates the try/catch
37+
block in `Zend\Di\Di::resolveMethodParameters()` to catch container-interop
38+
exceptions instead of the zend-servicemanager-specific exception class. Since
39+
all zend-servicemanager exceptions derive from container-interop, this
40+
provides more flexibility in using any container-interop implementation as a
41+
peering container.

composer.json

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,21 @@
1313
}
1414
},
1515
"require": {
16-
"php": ">=5.5",
17-
"zendframework/zend-code": "~2.5",
18-
"zendframework/zend-stdlib": "dev-develop as 2.8.0"
16+
"php": "^5.5 || ^7.0",
17+
"container-interop/container-interop": "^1.1",
18+
"zendframework/zend-code": "^2.6 || ^3.0",
19+
"zendframework/zend-stdlib": "^2.7 || ^3.0"
1920
},
2021
"require-dev": {
21-
"zendframework/zend-config": "dev-develop as 2.6.0",
22-
"zendframework/zend-db": "dev-develop as 2.7.0",
23-
"zendframework/zend-filter": "~2.5",
24-
"zendframework/zend-form": "~2.5",
25-
"zendframework/zend-log": "~2.5",
26-
"zendframework/zend-mvc": "dev-develop as 2.7.0",
27-
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
28-
"zendframework/zend-view": "dev-develop as 2.6.0",
2922
"fabpot/php-cs-fixer": "1.7.*",
3023
"phpunit/PHPUnit": "~4.0"
3124
},
32-
"suggest": {
33-
"zendframework/zend-servicemanager": "Zend\\ServiceManager component"
34-
},
3525
"minimum-stability": "dev",
3626
"prefer-stable": true,
3727
"extra": {
3828
"branch-alias": {
3929
"dev-master": "2.5-dev",
40-
"dev-develop": "3.0-dev"
30+
"dev-develop": "2.6-dev"
4131
}
4232
},
4333
"autoload-dev": {

src/Di.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
namespace Zend\Di;
1111

1212
use Closure;
13+
use Interop\Container\Exception\ContainerException;
1314
use Zend\Di\Exception\RuntimeException as DiRuntimeException;
14-
use Zend\ServiceManager\Exception\ExceptionInterface as ServiceManagerException;
1515

1616
/**
1717
* Dependency injector that can generate instances using class definitions and configured instance parameters
@@ -212,6 +212,24 @@ protected function getCallParameters($name, array $params, $method = "__construc
212212
return $params;
213213
}
214214

215+
/**
216+
* Is the DI container capable of returning the named instance?
217+
*
218+
* @param string $name
219+
* @return bool
220+
*/
221+
public function has($name)
222+
{
223+
$definitions = $this->definitions;
224+
$instanceManager = $this->instanceManager();
225+
226+
$class = $instanceManager->hasAlias($name)
227+
? $instanceManager->getClassFromAlias($name)
228+
: $name;
229+
230+
return $definitions->hasClass($class);
231+
}
232+
215233
/**
216234
* Lazy-load a class
217235
*
@@ -831,8 +849,8 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
831849
}
832850
return false;
833851
}
834-
} catch (ServiceManagerException $e) {
835-
// Zend\ServiceManager\Exception\ServiceNotCreatedException
852+
} catch (ContainerException $e) {
853+
// Exceptions thrown by nested/peered containers (e.g., zend-servicemanager)
836854
if ($methodRequirementType & self::RESOLVE_STRICT) {
837855
//finally ( be aware to do at the end of flow)
838856
array_pop($this->currentDependencies);

src/LocatorInterface.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@
99

1010
namespace Zend\Di;
1111

12-
interface LocatorInterface
12+
use Interop\Container\ContainerInterface;
13+
14+
interface LocatorInterface extends ContainerInterface
1315
{
14-
/**
15-
* Retrieve a class instance
16-
*
17-
* @param string $name Class name or service name
18-
* @param null|array $params Parameters to be used when instantiating a new instance of $name
19-
* @return object|null
20-
*/
21-
public function get($name, array $params = []);
2216
}

src/ServiceLocator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ public function set($name, $service)
5252
return $this;
5353
}
5454

55+
/**
56+
* Can the locator return the named instance?
57+
*
58+
* @param string $name
59+
* @return bool
60+
*/
61+
public function has($name)
62+
{
63+
if (! isset($this->services[$name])
64+
&& ! isset($this->map[$name])
65+
) {
66+
return false;
67+
}
68+
69+
return true;
70+
}
71+
5572
/**
5673
* Retrieve a registered service
5774
*

test/ConfigTest.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
use Zend\Di\Config;
1313
use Zend\Di\Di;
14-
use Zend\Config\Factory as ConfigFactory;
1514
use PHPUnit_Framework_TestCase as TestCase;
1615

1716
class ConfigTest extends TestCase
1817
{
1918
public function testConfigCanConfigureInstanceManagerWithIniFile()
2019
{
21-
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-a');
22-
$config = new Config($ini->di);
20+
$ini = include __DIR__ . '/_files/sample-definitions.php';
21+
$ini = $ini['section-a'];
22+
$config = new Config($ini['di']);
2323
$di = new Di();
2424
$di->configure($config);
2525

@@ -52,8 +52,9 @@ public function testConfigCanConfigureInstanceManagerWithIniFile()
5252
public function testConfigCanConfigureBuilderDefinitionFromIni()
5353
{
5454
$this->markTestIncomplete('Builder not updated to new DI yet');
55-
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-b');
56-
$config = new Config($ini->di);
55+
$ini = include __DIR__ . '/_files/sample-definitions.php';
56+
$ini = $ini['section-b'];
57+
$config = new Config($ini['di']);
5758
$di = new Di($config);
5859
$definition = $di->getDefinition();
5960

@@ -62,27 +63,28 @@ public function testConfigCanConfigureBuilderDefinitionFromIni()
6263
$this->assertEquals(
6364
['username' => null, 'password' => null],
6465
$definition->getInjectionMethodParameters('My\DbAdapter', '__construct')
65-
);
66+
);
6667

6768
$this->assertTrue($definition->hasClass('My\Mapper'));
6869
$this->assertEquals('__construct', $definition->getInstantiator('My\Mapper'));
6970
$this->assertEquals(
7071
['dbAdapter' => 'My\DbAdapter'],
7172
$definition->getInjectionMethodParameters('My\Mapper', '__construct')
72-
);
73+
);
7374

7475
$this->assertTrue($definition->hasClass('My\Repository'));
7576
$this->assertEquals('__construct', $definition->getInstantiator('My\Repository'));
7677
$this->assertEquals(
7778
['mapper' => 'My\Mapper'],
7879
$definition->getInjectionMethodParameters('My\Repository', '__construct')
79-
);
80+
);
8081
}
8182

8283
public function testConfigCanConfigureRuntimeDefinitionDefaultFromIni()
8384
{
84-
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-c');
85-
$config = new Config($ini->di);
85+
$ini = include __DIR__ . '/_files/sample-definitions.php';
86+
$ini = $ini['section-c'];
87+
$config = new Config($ini['di']);
8688
$di = new Di();
8789
$di->configure($config);
8890
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
@@ -92,8 +94,9 @@ public function testConfigCanConfigureRuntimeDefinitionDefaultFromIni()
9294

9395
public function testConfigCanConfigureRuntimeDefinitionDisabledFromIni()
9496
{
95-
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-d');
96-
$config = new Config($ini->di);
97+
$ini = include __DIR__ . '/_files/sample-definitions.php';
98+
$ini = $ini['section-d'];
99+
$config = new Config($ini['di']);
97100
$di = new Di();
98101
$di->configure($config);
99102
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
@@ -102,8 +105,9 @@ public function testConfigCanConfigureRuntimeDefinitionDisabledFromIni()
102105

103106
public function testConfigCanConfigureRuntimeDefinitionUseAnnotationFromIni()
104107
{
105-
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-e');
106-
$config = new Config($ini->di);
108+
$ini = include __DIR__ . '/_files/sample-definitions.php';
109+
$ini = $ini['section-e'];
110+
$config = new Config($ini['di']);
107111
$di = new Di();
108112
$di->configure($config);
109113
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
@@ -112,8 +116,8 @@ public function testConfigCanConfigureRuntimeDefinitionUseAnnotationFromIni()
112116

113117
public function testConfigCanConfigureCompiledDefinition()
114118
{
115-
$config = ConfigFactory::fromFile(__DIR__ . '/_files/sample.php', true);
116-
$config = new Config($config->di);
119+
$config = include __DIR__ . '/_files/sample.php';
120+
$config = new Config($config['di']);
117121
$di = new Di();
118122
$di->configure($config);
119123
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\ArrayDefinition');

test/Definition/BuilderDefinitionTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Zend\Di\Definition\BuilderDefinition;
1313
use Zend\Di\Definition\Builder;
14-
use Zend\Config\Factory as ConfigFactory;
1514
use PHPUnit_Framework_TestCase as TestCase;
1615

1716
class BuilderDefinitionTest extends TestCase
@@ -73,7 +72,7 @@ public function testBuilderDefinitionHasMethods()
7372

7473
public function testBuilderCanBuildFromArray()
7574
{
76-
$ini = ConfigFactory::fromFile(__DIR__ . '/../_files/sample.ini');
75+
$ini = include __DIR__ . '/../_files/sample-definitions.php';
7776
$iniAsArray = $ini['section-b'];
7877
$definitionArray = $iniAsArray['di']['definitions'][1];
7978
unset($definitionArray['class']);

test/DiCompatibilityTest.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use BadMethodCallException;
1313
use Exception;
1414
use PHPUnit_Framework_Error;
15+
use SplStack;
1516
use Zend\Di\Di;
1617

1718
class DiCompatibilityTest extends \PHPUnit_Framework_TestCase
@@ -41,13 +42,9 @@ public function testDiSimple($class)
4142
public function providesSimpleClasses()
4243
{
4344
return [
44-
['Zend\Di\Di'],
45-
['Zend\EventManager\EventManager'],
46-
['Zend\Filter\ToNull'],
47-
['Zend\Form\Form'],
48-
['Zend\Log\Logger'],
49-
['Zend\Stdlib\SplStack'],
50-
['Zend\View\Model\ViewModel'],
45+
[Di::class],
46+
[SplStack::class],
47+
[TestAsset\BasicClass::class],
5148
];
5249
}
5350

@@ -123,21 +120,9 @@ public function testCanCreateInstanceWithConstructorRequiredParameterWithDi($cla
123120

124121
public function providesClassWithConstructionParameters()
125122
{
126-
$serviceManager = new \Zend\ServiceManager\ServiceManager;
127-
$eventManager = new \Zend\EventManager\EventManager;
128-
$request = $this->getMockBuilder('Zend\Stdlib\RequestInterface')->getMock();
129-
$response = $this->getMockBuilder('Zend\Stdlib\ResponseInterface')->getMock();
130-
131123
return [
132-
['Zend\Config\Config', ['array' => []]],
133-
['Zend\Db\Adapter\Adapter', ['driver' => ['driver' => 'Pdo_Sqlite']]],
134-
['Zend\Mvc\Application', [
135-
'configuration' => [],
136-
'serviceManager' => $serviceManager,
137-
'events' => $eventManager,
138-
'request' => $request,
139-
'response' => $response
140-
]],
124+
[TestAsset\BasicClassWithParam::class, ['foo' => 'bar']],
125+
[TestAsset\ConstructorInjection\X::class, ['one' => 1, 'two' => 2]],
141126
];
142127
}
143128
}

0 commit comments

Comments
 (0)