1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Bundle\ModlrBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Provides utility/helper methods for DI related tasks. |
9
|
|
|
* |
10
|
|
|
* @author Jacob Bare <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Utility |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The library root namespace. |
16
|
|
|
*/ |
17
|
|
|
const LIBRARY_NS = 'As3\\Modlr'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The bundle root namespace. |
21
|
|
|
*/ |
22
|
|
|
const BUNDLE_NS = 'As3\\Bundle\\ModlrBundle'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The bundle alias. |
26
|
|
|
*/ |
27
|
|
|
const BUNDLE_ALIAS = 'as3_modlr'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Prevent instantiation. |
31
|
|
|
*/ |
32
|
|
|
private function __construct() |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Appends a key/value pair to a container parameter. |
38
|
|
|
* If the parameter name currently isn't set, the parameter is created. |
39
|
|
|
* |
40
|
|
|
* @param string $name |
41
|
|
|
* @param string $key |
42
|
|
|
* @param mixed $value |
43
|
|
|
* @param ContainerBuilder $container |
44
|
|
|
*/ |
45
|
|
|
public static function appendParameter($name, $key, $value, ContainerBuilder $container) |
46
|
|
|
{ |
47
|
|
|
$name = self::getAliasedName($name); |
48
|
|
|
|
49
|
|
|
if (false === $container->hasParameter($name)) { |
50
|
|
|
$current = []; |
51
|
|
|
} else { |
52
|
|
|
$current = (Array) $container->getParameter($name); |
53
|
|
|
} |
54
|
|
|
$current[$key] = $value; |
55
|
|
|
$container->setParameter($name, $current); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Cleans a service name by removing any '@' characters. |
60
|
|
|
* |
61
|
|
|
* @param string $name |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public static function cleanServiceName($name) |
65
|
|
|
{ |
66
|
|
|
return str_replace('@', '', $name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Gets the fully qualified class name for a Modlr bundle class. |
71
|
|
|
* |
72
|
|
|
* @static |
73
|
|
|
* @param string $subClass |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public static function getBundleClass($subClass) |
77
|
|
|
{ |
78
|
|
|
return sprintf('%s\\%s', self::BUNDLE_NS, $subClass); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets the fully qualified class name for a Modlr library class. |
83
|
|
|
* |
84
|
|
|
* @static |
85
|
|
|
* @param string $subClass |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public static function getLibraryClass($subClass) |
89
|
|
|
{ |
90
|
|
|
return sprintf('%s\\%s', self::LIBRARY_NS, $subClass); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the fully-qualified bundle alias name. |
95
|
|
|
* |
96
|
|
|
* @static |
97
|
|
|
* @param string $name |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public static function getAliasedName($name) |
101
|
|
|
{ |
102
|
|
|
return sprintf('%s.%s', self::BUNDLE_ALIAS, $name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Locates a file resource path for a given config path. |
107
|
|
|
* Is needed in order to retrieve a bundle's directory, if used. |
108
|
|
|
* |
109
|
|
|
* |
110
|
|
|
* @static |
111
|
|
|
* @param string $path |
112
|
|
|
* @param ContainerBuilder $container |
113
|
|
|
* @return string |
114
|
|
|
* @throws \RuntimeException |
115
|
|
|
*/ |
116
|
|
|
public static function locateResource($path, ContainerBuilder $container) |
117
|
|
|
{ |
118
|
|
|
if (0 === stripos($path, '@')) { |
119
|
|
|
// Treat as a bundle path, e.g. @SomeBundle/path/to/something. |
120
|
|
|
|
121
|
|
|
// Correct backslashed paths. |
122
|
|
|
$path = str_replace('\\', '/', $path); |
123
|
|
|
|
124
|
|
|
$parts = explode('/', $path); |
125
|
|
|
$bundle = array_shift($parts); |
126
|
|
|
|
127
|
|
|
$bundleName = str_replace('@', '', $bundle); |
128
|
|
|
$bundleClass = null; |
129
|
|
|
foreach ($container->getParameter('kernel.bundles') as $name => $class) { |
130
|
|
|
if ($name === $bundleName) { |
131
|
|
|
$bundleClass = $class; |
132
|
|
|
break; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (null === $bundleClass) { |
137
|
|
|
throw new \RuntimeException(sprintf('Unable to find a bundle named "%s" for resource path "%s"', $bundleName, $path)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$refl = new \ReflectionClass($bundleClass); |
141
|
|
|
$bundleDir = dirname($refl->getFileName()); |
142
|
|
|
return sprintf('%s/%s', $bundleDir, implode('/', $parts)); |
143
|
|
|
} |
144
|
|
|
return $path; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|