|
1
|
|
|
<?php namespace Limoncello\ContainerLight; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Interop\Container\ContainerInterface; |
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
use Limoncello\ContainerLight\Exceptions\NotFoundException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Limoncello\ContainerLight |
|
25
|
|
|
*/ |
|
26
|
|
|
class Container extends \Pimple\Container implements ContainerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var callable[]|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $destructorHandlers = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function get($identity) |
|
37
|
|
|
{ |
|
38
|
|
|
try { |
|
39
|
2 |
|
return $this->offsetGet($identity); |
|
40
|
1 |
|
} catch (InvalidArgumentException $exception) { |
|
41
|
1 |
|
throw new NotFoundException($exception->getMessage(), $exception->getCode(), $exception); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function has($identity) |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $this->offsetExists($identity); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param callable $handler |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function registerDestructor(callable $handler) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->destructorHandlers[] = $handler; |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function __destruct() |
|
67
|
|
|
{ |
|
68
|
3 |
|
if ($this->destructorHandlers !== null) { |
|
69
|
1 |
|
foreach ($this->destructorHandlers as $handler) { |
|
70
|
1 |
|
call_user_func($handler); |
|
71
|
1 |
|
} |
|
72
|
1 |
|
unset($this->destructorHandlers); |
|
73
|
1 |
|
} |
|
74
|
3 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|