Skip to content

Commit e0295c2

Browse files
committed
added interface; changed namespace
1 parent 1db0381 commit e0295c2

File tree

8 files changed

+124
-53
lines changed

8 files changed

+124
-53
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.gitattributes export-ignore
2+
.gitignore export-ignore
3+
tests export-ignore
4+
phpunit-xml.dist export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ thumbs.db
88
.settings
99
.idea
1010
composer.lock
11+
composer.phar
1112
vendor/*

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Ivan Kudinov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
PHP Container
2-
====================
1+
# PHP Container
32

4-
Example
5-
-------
6-
```php
7-
<?php
3+
Simple Dependency Injection Container.
4+
5+
## Usage
86

7+
```php
98
use frostealth\Container\Container;
109

1110
$container = new Container();
@@ -39,6 +38,28 @@ $container->singleton('log', function ($container) {
3938
$log = $container->get('log');
4039
```
4140

42-
Requirements
43-
------------
41+
## Dependency Injection
42+
43+
```php
44+
use Interop\Container\ContainerInterface;
45+
46+
class MyClass
47+
{
48+
/**
49+
* @var ContainerInterface
50+
*/
51+
protected $container;
52+
53+
/**
54+
* @param ContainerInterface $container
55+
*/
56+
public function __construct(ContainerInterface $container)
57+
{
58+
$this->container = $container;
59+
}
60+
}
61+
```
62+
63+
## Requirements
64+
4465
* PHP >= 5.4

composer.json

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
{
2-
"name": "frostealth/php-container",
3-
"description": "PHP Container",
4-
"keywords": [
5-
"di",
6-
"container"
7-
],
8-
"license": "MIT",
9-
"authors": [
10-
{
11-
"name": "Ivan Kudinov",
12-
"email": "frostealth@gmail.com",
13-
"homepage": "http://frostealth.ru"
2+
"name": "frostealth/php-container",
3+
"description": "Simple Dependency Injection Container",
4+
"keywords": ["di", "dependency injection", "container"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Ivan Kudinov",
9+
"email": "frostealth@gmail.com",
10+
"homepage": "http://frostealth.ru"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.0",
15+
"container-interop/container-interop": "~1.0",
16+
"frostealth/php-data-storage": "~2.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"frostealth\\": "src/"
21+
}
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "4.2.*"
1425
}
15-
],
16-
"autoload": {
17-
"psr-4": {
18-
"frostealth\\": "src/"
19-
}
20-
},
21-
"require": {
22-
"php": ">=5.4.0",
23-
"frostealth/php-data-storage": "1.0.*"
24-
},
25-
"autoload-dev": {
26-
"psr-4": {
27-
"frostealth\\Tests\\Container\\": "tests/"
28-
}
29-
},
30-
"require-dev": {
31-
"phpunit/phpunit": "4.2.*"
32-
}
3326
}

src/Container/Container.php renamed to src/container/Container.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
<?php
22

3-
namespace frostealth\Container;
3+
namespace frostealth\container;
44

5-
use frostealth\Storage\Data;
5+
use frostealth\container\exceptions\ContainerException;
6+
use frostealth\storage\Data;
7+
use frostealth\storage\DataInterface;
8+
use Interop\Container\ContainerInterface;
69

710
/**
811
* Class Container
912
*
10-
* @package frostealth\Container
13+
* @package frostealth\container
1114
*/
12-
class Container
15+
class Container implements ContainerInterface
1316
{
14-
/** @var Data */
17+
/**
18+
* @var DataInterface
19+
*/
1520
protected $storage;
1621

17-
public function __construct()
22+
/**
23+
* @param array|DataInterface $values
24+
*/
25+
public function __construct($values = [])
1826
{
19-
$this->storage = new Data();
27+
if (is_array($values)) {
28+
$values = new Data($values);
29+
}
30+
31+
if (!$values instanceof DataInterface) {
32+
throw new ContainerException('Expected a DataInterface');
33+
}
34+
35+
$this->storage = $values;
2036
}
2137

2238
/**
@@ -36,7 +52,7 @@ public function get($name)
3652

3753
/**
3854
* @param string $name
39-
* @param mixed $value
55+
* @param mixed $value
4056
*/
4157
public function set($name, $value)
4258
{
@@ -62,7 +78,9 @@ public function remove($name)
6278
}
6379

6480
/**
65-
* @param string $name
81+
* Ensure a value or object will remain globally unique
82+
*
83+
* @param string $name
6684
* @param callable $callable
6785
*/
6886
public function singleton($name, callable $callable)
@@ -78,6 +96,19 @@ public function singleton($name, callable $callable)
7896
});
7997
}
8098

99+
/**
100+
* Protect closure from being directly invoked
101+
*
102+
* @param string $name
103+
* @param callable $callable
104+
*/
105+
public function protect($name, callable $callable)
106+
{
107+
$this->storage->set($name, function () use ($callable) {
108+
return $callable;
109+
});
110+
}
111+
81112
/**
82113
* @param string $name
83114
*
@@ -90,7 +121,7 @@ public function __get($name)
90121

91122
/**
92123
* @param string $name
93-
* @param mixed $value
124+
* @param mixed $value
94125
*/
95126
public function __set($name, $value)
96127
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace frostealth\container\exceptions;
4+
5+
use Interop\Container\Exception\ContainerException as InteropContainerException;
6+
use RuntimeException;
7+
8+
/**
9+
* Class ContainerException
10+
*
11+
* @package frostealth\container
12+
*/
13+
class ContainerException extends RuntimeException implements InteropContainerException
14+
{
15+
16+
}

tests/ContainerTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<?php
22

3-
namespace frostealth\Tests\Container;
4-
5-
use frostealth\Container\Container;
3+
use frostealth\container\Container;
64

75
/**
86
* Class ContainerTest
9-
*
10-
* @package frostealth\Tests\Container
117
*/
128
class ContainerTest extends \PHPUnit_Framework_TestCase
139
{

0 commit comments

Comments
 (0)