|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
+----------------------------------------------------------------------+ |
|
5
|
|
|
| This file is part of the pinepain/container-extras PHP library. | |
|
6
|
|
|
| | |
|
7
|
|
|
| Copyright (c) 2015-2016 Bogdan Padalko <[email protected]> | |
|
8
|
|
|
| | |
|
9
|
|
|
| Licensed under the MIT license: http://opensource.org/licenses/MIT | |
|
10
|
|
|
| | |
|
11
|
|
|
| For the full copyright and license information, please view the | |
|
12
|
|
|
| LICENSE file that was distributed with this source or visit | |
|
13
|
|
|
| http://opensource.org/licenses/MIT | |
|
14
|
|
|
+----------------------------------------------------------------------+ |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
namespace Pinepain\Container\Extras; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
use League\Container\ContainerInterface; |
|
22
|
|
|
use League\Container\Definition\ClassDefinition; |
|
23
|
|
|
use League\Container\Definition\DefinitionInterface; |
|
24
|
|
|
use Pinepain\Container\Extras\Exceptions\InvalidConfigException; |
|
25
|
|
|
use Traversable; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class AliasContainerConfigurator implements ContainerConfiguratorInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var AliasContainerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $container; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param AliasContainerInterface $container |
|
37
|
|
|
*/ |
|
38
|
8 |
|
public function __construct(AliasContainerInterface $container) |
|
39
|
|
|
{ |
|
40
|
8 |
|
$this->container = $container; |
|
41
|
8 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
5 |
View Code Duplication |
public function configure($config) |
|
47
|
|
|
{ |
|
48
|
5 |
|
if (!is_array($config) && !($config instanceof Traversable)) { |
|
49
|
1 |
|
throw new InvalidConfigException( |
|
50
|
|
|
'You can only load definitions from an array or an object that implements Traversable interface.' |
|
51
|
1 |
|
); |
|
52
|
|
|
} |
|
53
|
4 |
|
foreach ($config as $alias => $concrete) { |
|
54
|
|
|
|
|
55
|
2 |
|
if (!is_string($concrete)) { |
|
56
|
1 |
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
$this->configureOne($alias, $concrete); |
|
60
|
4 |
|
} |
|
61
|
4 |
|
} |
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
5 |
|
public function configureOne($alias, $concrete) |
|
66
|
|
|
{ |
|
67
|
5 |
|
if (!is_string($alias) || !is_string($concrete)) { |
|
68
|
3 |
|
throw new InvalidConfigException('Alias and concrete should be strings'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
$this->container->add($alias, $concrete); |
|
72
|
2 |
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|