PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr2/src/vendor/symfony/src/Symfony/Components/DependencyInjection/BuilderConfiguration.php

https://github.com/pmjones/php-framework-benchmarks
PHP | 396 lines | 162 code | 53 blank | 181 comment | 6 complexity | 9335ae6d9c7cc516b453454de8372787 MD5 | raw file
  1. <?php
  2. namespace Symfony\Components\DependencyInjection;
  3. use Symfony\Components\DependencyInjection\Loader\LoaderExtensionInterface;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * A BuilderConfiguration is a consistent set of definitions and parameters.
  14. *
  15. * @package Symfony
  16. * @subpackage Components_DependencyInjection
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class BuilderConfiguration
  20. {
  21. protected $definitions;
  22. protected $parameters;
  23. protected $aliases;
  24. protected $resources;
  25. protected $extensions;
  26. public function __construct(array $definitions = array(), array $parameters = array())
  27. {
  28. $this->aliases = array();
  29. $this->resources = array();
  30. $this->extensions = array();
  31. $this->setDefinitions($definitions);
  32. $this->setParameters($parameters);
  33. }
  34. /**
  35. * Returns an array of resources loaded to build this configuration.
  36. *
  37. * @return ResourceInterface[] An array of resources
  38. */
  39. public function getResources()
  40. {
  41. return $this->resources;
  42. }
  43. /**
  44. * Adds a resource for this configuration.
  45. *
  46. * @param ResourceInterface $resource A resource instance
  47. *
  48. * @return BuilderConfiguration The current instance
  49. */
  50. public function addResource(ResourceInterface $resource)
  51. {
  52. $this->resources[] = $resource;
  53. return $this;
  54. }
  55. /**
  56. * Merges a BuilderConfiguration with the current one.
  57. *
  58. * @param BuilderConfiguration $configuration
  59. *
  60. * @return BuilderConfiguration The current instance
  61. */
  62. public function merge(BuilderConfiguration $configuration = null)
  63. {
  64. if (null === $configuration) {
  65. return;
  66. }
  67. $this->addDefinitions($configuration->getDefinitions());
  68. $this->addAliases($configuration->getAliases());
  69. $this->addParameters($configuration->getParameters());
  70. foreach ($configuration->getResources() as $resource) {
  71. $this->addResource($resource);
  72. }
  73. return $this;
  74. }
  75. /**
  76. * Loads the configuration for an extension.
  77. *
  78. * @param $extension LoaderExtensionInterface A LoaderExtensionInterface instance
  79. * @param $tag string The extension tag to load (without the namespace - namespace.tag)
  80. * @param $values array An array of values that customizes the extension
  81. *
  82. * @return BuilderConfiguration The current instance
  83. */
  84. public function loadFromExtension(LoaderExtensionInterface $extension, $tag, array $values = array())
  85. {
  86. $namespace = $extension->getAlias();
  87. if (!isset($this->extensions[$namespace])) {
  88. $this->extensions[$namespace] = new self();
  89. $r = new \ReflectionObject($extension);
  90. $this->extensions[$namespace]->addResource(new FileResource($r->getFileName()));
  91. }
  92. $this->extensions[$namespace] = $extension->load($tag, $values, $this->extensions[$namespace]);
  93. return $this;
  94. }
  95. /**
  96. * Merges the extension configuration.
  97. *
  98. * @return BuilderConfiguration The current instance
  99. */
  100. public function mergeExtensionsConfiguration()
  101. {
  102. foreach ($this->extensions as $name => $configuration) {
  103. $this->merge($configuration);
  104. }
  105. $this->extensions = array();
  106. return $this;
  107. }
  108. /**
  109. * Sets the service container parameters.
  110. *
  111. * @param array $parameters An array of parameters
  112. *
  113. * @return BuilderConfiguration The current instance
  114. */
  115. public function setParameters(array $parameters)
  116. {
  117. $this->parameters = array();
  118. foreach ($parameters as $key => $value) {
  119. $this->parameters[strtolower($key)] = $value;
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Adds parameters to the service container parameters.
  125. *
  126. * @param array $parameters An array of parameters
  127. *
  128. * @return BuilderConfiguration The current instance
  129. */
  130. public function addParameters(array $parameters)
  131. {
  132. $this->setParameters(array_merge($this->parameters, $parameters));
  133. return $this;
  134. }
  135. /**
  136. * Gets the service container parameters.
  137. *
  138. * @return array An array of parameters
  139. */
  140. public function getParameters()
  141. {
  142. return $this->parameters;
  143. }
  144. /**
  145. * Returns true if a parameter name is defined.
  146. *
  147. * @param string $name The parameter name
  148. *
  149. * @return Boolean true if the parameter name is defined, false otherwise
  150. */
  151. public function hasParameter($name)
  152. {
  153. return array_key_exists(strtolower($name), $this->parameters);
  154. }
  155. /**
  156. * Gets a service container parameter.
  157. *
  158. * @param string $name The parameter name
  159. *
  160. * @return mixed The parameter value
  161. *
  162. * @throws \InvalidArgumentException if the parameter is not defined
  163. */
  164. public function getParameter($name)
  165. {
  166. if (!$this->hasParameter($name)) {
  167. throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
  168. }
  169. return $this->parameters[strtolower($name)];
  170. }
  171. /**
  172. * Sets a service container parameter.
  173. *
  174. * @param string $name The parameter name
  175. * @param mixed $parameters The parameter value
  176. *
  177. * @return BuilderConfiguration The current instance
  178. */
  179. public function setParameter($name, $value)
  180. {
  181. $this->parameters[strtolower($name)] = $value;
  182. return $this;
  183. }
  184. /**
  185. * Sets an alias for an existing service.
  186. *
  187. * @param string $alias The alias to create
  188. * @param string $id The service to alias
  189. *
  190. * @return BuilderConfiguration The current instance
  191. */
  192. public function setAlias($alias, $id)
  193. {
  194. unset($this->definitions[$alias]);
  195. $this->aliases[$alias] = $id;
  196. return $this;
  197. }
  198. /**
  199. * Adds definition aliases.
  200. *
  201. * @param array $aliases An array of aliases
  202. *
  203. * @return BuilderConfiguration The current instance
  204. */
  205. public function addAliases(array $aliases)
  206. {
  207. foreach ($aliases as $alias => $id) {
  208. $this->setAlias($alias, $id);
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Gets all defined aliases.
  214. *
  215. * @return array An array of aliases
  216. */
  217. public function getAliases()
  218. {
  219. return $this->aliases;
  220. }
  221. /**
  222. * Returns true if a service alias exists.
  223. *
  224. * @param string $alias The alias
  225. *
  226. * @return Boolean true if the alias exists, false otherwise
  227. */
  228. public function hasAlias($alias)
  229. {
  230. return array_key_exists($alias, $this->aliases);
  231. }
  232. /**
  233. * Gets the service id for a given alias.
  234. *
  235. * @param string $alias The alias
  236. *
  237. * @return string The aliased id
  238. *
  239. * @throws \InvalidArgumentException if the service alias does not exist
  240. */
  241. public function getAlias($alias)
  242. {
  243. if (!$this->hasAlias($alias)) {
  244. throw new \InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $alias));
  245. }
  246. return $this->aliases[$alias];
  247. }
  248. /**
  249. * Sets a definition.
  250. *
  251. * @param string $id The identifier
  252. * @param Definition $definition A Definition instance
  253. *
  254. * @return BuilderConfiguration The current instance
  255. */
  256. public function setDefinition($id, Definition $definition)
  257. {
  258. unset($this->aliases[$id]);
  259. $this->definitions[$id] = $definition;
  260. return $this;
  261. }
  262. /**
  263. * Adds the definitions.
  264. *
  265. * @param Definition[] $definitions An array of definitions
  266. *
  267. * @return BuilderConfiguration The current instance
  268. */
  269. public function addDefinitions(array $definitions)
  270. {
  271. foreach ($definitions as $id => $definition) {
  272. $this->setDefinition($id, $definition);
  273. }
  274. return $this;
  275. }
  276. /**
  277. * Sets the definitions.
  278. *
  279. * @param array $definitions An array of definitions
  280. *
  281. * @return BuilderConfiguration The current instance
  282. */
  283. public function setDefinitions(array $definitions)
  284. {
  285. $this->definitions = array();
  286. $this->addDefinitions($definitions);
  287. return $this;
  288. }
  289. /**
  290. * Gets all definitions.
  291. *
  292. * @return array An array of Definition instances
  293. */
  294. public function getDefinitions()
  295. {
  296. return $this->definitions;
  297. }
  298. /**
  299. * Returns true if a service definition exists under the given identifier.
  300. *
  301. * @param string $id The service identifier
  302. *
  303. * @return Boolean true if the service definition exists, false otherwise
  304. */
  305. public function hasDefinition($id)
  306. {
  307. return array_key_exists($id, $this->definitions);
  308. }
  309. /**
  310. * Gets a service definition.
  311. *
  312. * @param string $id The service identifier
  313. *
  314. * @return Definition A Definition instance
  315. *
  316. * @throws \InvalidArgumentException if the service definition does not exist
  317. */
  318. public function getDefinition($id)
  319. {
  320. if (!$this->hasDefinition($id)) {
  321. throw new \InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
  322. }
  323. return $this->definitions[$id];
  324. }
  325. /**
  326. * Gets a service definition by id or alias.
  327. *
  328. * The method "unaliases" recursively to return a Definition instance.
  329. *
  330. * @param string $id The service identifier or alias
  331. *
  332. * @return Definition A Definition instance
  333. *
  334. * @throws \InvalidArgumentException if the service definition does not exist
  335. */
  336. public function findDefinition($id)
  337. {
  338. if ($this->hasAlias($id)) {
  339. return $this->findDefinition($this->getAlias($id));
  340. }
  341. return $this->getDefinition($id);
  342. }
  343. }