1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* Contains \Drupal\service_container\DependencyInjection\Container |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Drupal\service_container\DependencyInjection; |
8
|
|
|
|
9
|
|
|
use Drupal\Component\DependencyInjection\PhpArrayContainer; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Symfony\Component\DependencyInjection\ScopeInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Container is a DI container that provides services to users of the class. |
16
|
|
|
* |
17
|
|
|
* @ingroup dic |
18
|
|
|
*/ |
19
|
|
|
class Container extends PhpArrayContainer implements ContainerInterface { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function createInstance($plugin_id, $service_definition) { |
25
|
|
|
$temporary_name = 'plugin_' . $plugin_id; |
26
|
|
|
$this->serviceDefinitions[$temporary_name] = $service_definition; |
27
|
|
|
|
28
|
|
|
$plugin = $this->get($temporary_name); |
29
|
|
|
unset($this->serviceDefinitions[$temporary_name]); |
30
|
|
|
unset($this->services[$temporary_name]); |
31
|
|
|
|
32
|
|
|
return $plugin; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) { |
39
|
|
|
$definition = isset($this->serviceDefinitions[$plugin_id]) ? $this->serviceDefinitions[$plugin_id] : NULL; |
40
|
|
|
|
41
|
|
|
if (!$definition && $exception_on_invalid) { |
42
|
|
|
throw new RuntimeException(sprintf('The "%s" service definition does not exist.', $plugin_id)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $definition; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getDefinitions() { |
52
|
|
|
return $this->serviceDefinitions; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function hasDefinition($plugin_id) { |
59
|
|
|
return isset($this->serviceDefinitions[$plugin_id]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Camelizes a string. |
64
|
|
|
* |
65
|
|
|
* @param $name |
66
|
|
|
* The string to camelize. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
* The camelized string. |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
|
|
public static function camelize($name) { |
73
|
|
|
return strtr(ucwords(strtr($name, array('_' => ' ', '\\' => '_ '))), array(' ' => '')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Un-camelizes a string. |
78
|
|
|
* |
79
|
|
|
* @param $name |
80
|
|
|
* The string to underscore. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
* The underscored string. |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
|
public static function underscore($name) { |
87
|
|
|
return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), $name)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|