Skip to content

Commit 95ab990

Browse files
committed
Implementing Container Interoperability
1 parent d1c0830 commit 95ab990

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
}
1818
},
1919
"require": {
20-
"php": ">=5.3.0"
20+
"php": ">=5.3.0",
21+
"container-interop/container-interop": "^1.1"
2122
},
2223
"require-dev": {
2324
"phpunit/phpunit": "~4.4",

library/Respect/Config/Container.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use ArrayObject;
77
use ReflectionClass;
88
use ReflectionFunction;
9+
use Interop\Container\ContainerInterface;
910

10-
class Container extends ArrayObject
11+
class Container extends ArrayObject implements ContainerInterface
1112
{
1213
protected $configurator;
1314

@@ -17,6 +18,11 @@ public function __construct($configurator = null)
1718
}
1819

1920
public function __isset($name)
21+
{
22+
return $this->has($name);
23+
}
24+
25+
public function has($name)
2026
{
2127
if ($this->configurator) {
2228
$this->configure();
@@ -101,15 +107,15 @@ protected function configure()
101107

102108
throw new Argument("Invalid input. Must be a valid file or array");
103109
}
104-
110+
105111
public function getItem($name, $raw = false)
106112
{
107113
if ($this->configurator) {
108114
$this->configure();
109115
}
110116

111117
if (!isset($this[$name])) {
112-
throw new Argument("Item $name not found");
118+
throw new NotFoundException("Item $name not found");
113119
}
114120

115121
if ($raw || !is_callable($this[$name])) {
@@ -118,6 +124,11 @@ public function getItem($name, $raw = false)
118124

119125
return $this->lazyLoad($name);
120126
}
127+
128+
public function get($name)
129+
{
130+
return $this->getItem($name);
131+
}
121132

122133
public function loadString($configurator)
123134
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Respect\Config;
4+
5+
use Interop\Container\Exception\NotFoundException as BaseNotFoundException;
6+
7+
class NotFoundException extends \Exception implements BaseNotFoundException
8+
{
9+
}

tests/library/Respect/Config/ContainerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ public function testLoadFile()
4848
$this->assertEquals('bar', $c->getItem('foo'));
4949
$this->assertEquals('bat', $c->getItem('baz'));
5050
}
51+
52+
public function testContainerInterop()
53+
{
54+
$ini = <<<INI
55+
foo = bar
56+
baz = bat
57+
INI;
58+
$c = new Container;
59+
$c->loadArray(parse_ini_string($ini, true));
60+
$this->assertTrue($c->has('foo'));
61+
$this->assertEquals('bar', $c->get('foo'));
62+
$this->assertEquals('bat', $c->get('baz'));
63+
}
64+
65+
/**
66+
* @expectedException Interop\Container\Exception\NotFoundException
67+
* @expectedExceptionMessage Item baz not found
68+
*/
69+
public function testLoadInvalidName()
70+
{
71+
$ini = <<<INI
72+
foo = bar
73+
INI;
74+
$c = new Container;
75+
$c->loadArray(parse_ini_string($ini, true));
76+
$c->get('baz');
77+
}
5178

5279
/**
5380
* @expectedException InvalidArgumentException

0 commit comments

Comments
 (0)