PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Bundle/DoctrineAbstractBundle/DependencyInjection/AbstractDoctrineExtension.php

https://bitbucket.org/Leimz/leimzwebsite
PHP | 319 lines | 198 code | 24 blank | 97 comment | 21 complexity | 3c2f055e50f7e997582dbe9d27dc8b56 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\DoctrineAbstractBundle\DependencyInjection;
  11. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\Config\Resource\FileResource;
  16. /**
  17. * This abstract classes groups common code that Doctrine Object Manager extensions (ORM, MongoDB, CouchDB) need.
  18. */
  19. abstract class AbstractDoctrineExtension extends Extension
  20. {
  21. /**
  22. * Used inside metadata driver method to simplify aggregation of data.
  23. *
  24. * @var array
  25. */
  26. protected $aliasMap = array();
  27. /**
  28. * Used inside metadata driver method to simplify aggregation of data.
  29. *
  30. * @var array
  31. */
  32. protected $drivers = array();
  33. /*
  34. * @param array $objectManager A configured object manager.
  35. * @param ContainerBuilder $container A ContainerBuilder instance
  36. */
  37. protected function loadMappingInformation(array $objectManager, ContainerBuilder $container)
  38. {
  39. if ($objectManager['auto_mapping']) {
  40. // automatically register bundle mappings
  41. foreach (array_keys($container->getParameter('kernel.bundles')) as $bundle) {
  42. if (!isset($objectManager['mappings'][$bundle])) {
  43. $objectManager['mappings'][$bundle] = null;
  44. }
  45. }
  46. }
  47. foreach ($objectManager['mappings'] as $mappingName => $mappingConfig) {
  48. if (null !== $mappingConfig && false === $mappingConfig['mapping']) {
  49. continue;
  50. }
  51. $mappingConfig = array_replace(array(
  52. 'dir' => false,
  53. 'type' => false,
  54. 'prefix' => false,
  55. ), (array) $mappingConfig);
  56. $mappingConfig['dir'] = $container->getParameterBag()->resolveValue($mappingConfig['dir']);
  57. // a bundle configuration is detected by realizing that the specified dir is not absolute and existing
  58. if (!isset($mappingConfig['is_bundle'])) {
  59. $mappingConfig['is_bundle'] = !file_exists($mappingConfig['dir']);
  60. }
  61. if ($mappingConfig['is_bundle']) {
  62. $bundle = null;
  63. foreach ($container->getParameter('kernel.bundles') as $name => $class) {
  64. if ($mappingName === $name) {
  65. $bundle = new \ReflectionClass($class);
  66. break;
  67. }
  68. }
  69. if (null === $bundle) {
  70. throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled.', $mappingName));
  71. }
  72. $mappingConfig = $this->getMappingDriverBundleConfigDefaults($mappingConfig, $bundle, $container);
  73. if (!$mappingConfig) {
  74. continue;
  75. }
  76. }
  77. $this->assertValidMappingConfiguration($mappingConfig, $objectManager['name']);
  78. $this->setMappingDriverConfig($mappingConfig, $mappingName);
  79. $this->setMappingDriverAlias($mappingConfig, $mappingName);
  80. }
  81. }
  82. /**
  83. * Register the alias for this mapping driver.
  84. *
  85. * Aliases can be used in the Query languages of all the Doctrine object managers to simplify writing tasks.
  86. *
  87. * @param array $mappingConfig
  88. * @param string $mappingName
  89. * @return void
  90. */
  91. protected function setMappingDriverAlias($mappingConfig, $mappingName)
  92. {
  93. if (isset($mappingConfig['alias'])) {
  94. $this->aliasMap[$mappingConfig['alias']] = $mappingConfig['prefix'];
  95. } else {
  96. $this->aliasMap[$mappingName] = $mappingConfig['prefix'];
  97. }
  98. }
  99. /**
  100. * Register the mapping driver configuration for later use with the object managers metadata driver chain.
  101. *
  102. * @param array $mappingConfig
  103. * @param string $mappingName
  104. * @return void
  105. */
  106. protected function setMappingDriverConfig(array $mappingConfig, $mappingName)
  107. {
  108. if (is_dir($mappingConfig['dir'])) {
  109. $this->drivers[$mappingConfig['type']][$mappingConfig['prefix']] = realpath($mappingConfig['dir']);
  110. } else {
  111. throw new \InvalidArgumentException(sprintf('Invalid Doctrine mapping path given. Cannot load Doctrine mapping/bundle named "%s".', $mappingName));
  112. }
  113. }
  114. /**
  115. * If this is a bundle controlled mapping all the missing information can be autodetected by this method.
  116. *
  117. * Returns false when autodetection failed, an array of the completed information otherwise.
  118. *
  119. * @param array $bundleConfig
  120. * @param \ReflectionClass $bundle
  121. * @param ContainerBuilder $container A ContainerBuilder instance
  122. *
  123. * @return array|false
  124. */
  125. protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \ReflectionClass $bundle, ContainerBuilder $container)
  126. {
  127. $bundleDir = dirname($bundle->getFilename());
  128. if (!$bundleConfig['type']) {
  129. $bundleConfig['type'] = $this->detectMetadataDriver($bundleDir, $container);
  130. }
  131. if (!$bundleConfig['type']) {
  132. // skip this bundle, no mapping information was found.
  133. return false;
  134. }
  135. if (!$bundleConfig['dir']) {
  136. if (in_array($bundleConfig['type'], array('annotation', 'staticphp'))) {
  137. $bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingObjectDefaultName();
  138. } else {
  139. $bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingResourceConfigDirectory();
  140. }
  141. } else {
  142. $bundleConfig['dir'] = $bundleDir.'/'.$bundleConfig['dir'];
  143. }
  144. if (!$bundleConfig['prefix']) {
  145. $bundleConfig['prefix'] = $bundle->getNamespaceName().'\\'.$this->getMappingObjectDefaultName();
  146. }
  147. return $bundleConfig;
  148. }
  149. /**
  150. * Register all the collected mapping information with the object manager by registering the appropriate mapping drivers.
  151. *
  152. * @param array $objectManager
  153. * @param ContainerBuilder $container A ContainerBuilder instance
  154. */
  155. protected function registerMappingDrivers($objectManager, ContainerBuilder $container)
  156. {
  157. // configure metadata driver for each bundle based on the type of mapping files found
  158. if ($container->hasDefinition($this->getObjectManagerElementName($objectManager['name'].'_metadata_driver'))) {
  159. $chainDriverDef = $container->getDefinition($this->getObjectManagerElementName($objectManager['name'].'_metadata_driver'));
  160. } else {
  161. $chainDriverDef = new Definition('%'.$this->getObjectManagerElementName('metadata.driver_chain.class%'));
  162. $chainDriverDef->setPublic(false);
  163. }
  164. foreach ($this->drivers as $driverType => $driverPaths) {
  165. $mappingService = $this->getObjectManagerElementName($objectManager['name'].'_'.$driverType.'_metadata_driver');
  166. if ($container->hasDefinition($mappingService)) {
  167. $mappingDriverDef = $container->getDefinition($mappingService);
  168. $args = $mappingDriverDef->getArguments();
  169. if ($driverType == 'annotation') {
  170. $args[1] = array_merge(array_values($driverPaths), $args[1]);
  171. } else {
  172. $args[0] = array_merge(array_values($driverPaths), $args[0]);
  173. }
  174. $mappingDriverDef->setArguments($args);
  175. } elseif ($driverType == 'annotation') {
  176. $mappingDriverDef = new Definition('%'.$this->getObjectManagerElementName('metadata.'.$driverType.'.class%'), array(
  177. new Reference($this->getObjectManagerElementName('metadata.annotation_reader')),
  178. array_values($driverPaths)
  179. ));
  180. } else {
  181. $mappingDriverDef = new Definition('%'.$this->getObjectManagerElementName('metadata.'.$driverType.'.class%'), array(
  182. array_values($driverPaths)
  183. ));
  184. }
  185. $mappingDriverDef->setPublic(false);
  186. if (false !== strpos($mappingDriverDef->getClass(), 'yml') || false !== strpos($mappingDriverDef->getClass(), 'xml')) {
  187. $mappingDriverDef->addMethodCall('setNamespacePrefixes', array(array_flip($driverPaths)));
  188. $mappingDriverDef->addMethodCall('setGlobalBasename', array('mapping'));
  189. }
  190. $container->setDefinition($mappingService, $mappingDriverDef);
  191. foreach ($driverPaths as $prefix => $driverPath) {
  192. $chainDriverDef->addMethodCall('addDriver', array(new Reference($mappingService), $prefix));
  193. }
  194. }
  195. $container->setDefinition($this->getObjectManagerElementName($objectManager['name'].'_metadata_driver'), $chainDriverDef);
  196. }
  197. /**
  198. * Assertion if the specified mapping information is valid.
  199. *
  200. * @param array $mappingConfig
  201. * @param string $objectManagerName
  202. */
  203. protected function assertValidMappingConfiguration(array $mappingConfig, $objectManagerName)
  204. {
  205. if (!$mappingConfig['type'] || !$mappingConfig['dir'] || !$mappingConfig['prefix']) {
  206. throw new \InvalidArgumentException(sprintf('Mapping definitions for Doctrine manager "%s" require at least the "type", "dir" and "prefix" options.', $objectManagerName));
  207. }
  208. if (!file_exists($mappingConfig['dir'])) {
  209. throw new \InvalidArgumentException(sprintf('Specified non-existing directory "%s" as Doctrine mapping source.', $mappingConfig['dir']));
  210. }
  211. if (!in_array($mappingConfig['type'], array('xml', 'yml', 'annotation', 'php', 'staticphp'))) {
  212. throw new \InvalidArgumentException(sprintf('Can only configure "xml", "yml", "annotation", "php" or '.
  213. '"staticphp" through the DoctrineBundle. Use your own bundle to configure other metadata drivers. '.
  214. 'You can register them by adding a a new driver to the '.
  215. '"%s" service definition.', $this->getObjectManagerElementName($objectManagerName.'.metadata_driver')
  216. ));
  217. }
  218. }
  219. /**
  220. * Detects what metadata driver to use for the supplied directory.
  221. *
  222. * @param string $dir A directory path
  223. * @param ContainerBuilder $container A ContainerBuilder instance
  224. *
  225. * @return string|null A metadata driver short name, if one can be detected
  226. */
  227. protected function detectMetadataDriver($dir, ContainerBuilder $container)
  228. {
  229. // add the closest existing directory as a resource
  230. $configPath = $this->getMappingResourceConfigDirectory();
  231. $resource = $dir.'/'.$configPath;
  232. while (!is_dir($resource)) {
  233. $resource = dirname($resource);
  234. }
  235. $container->addResource(new FileResource($resource));
  236. $extension = $this->getMappingResourceExtension();
  237. if (($files = glob($dir.'/'.$configPath.'/*.'.$extension.'.xml')) && count($files)) {
  238. return 'xml';
  239. } elseif (($files = glob($dir.'/'.$configPath.'/*.'.$extension.'.yml')) && count($files)) {
  240. return 'yml';
  241. } elseif (($files = glob($dir.'/'.$configPath.'/*.'.$extension.'.php')) && count($files)) {
  242. return 'php';
  243. }
  244. // add the directory itself as a resource
  245. $container->addResource(new FileResource($dir));
  246. if (is_dir($dir.'/'.$this->getMappingObjectDefaultName())) {
  247. return 'annotation';
  248. }
  249. return null;
  250. }
  251. /**
  252. * Prefixes the relative dependency injection container path with the object manager prefix.
  253. *
  254. * @example $name is 'entity_manager' then the result would be 'doctrine.orm.entity_manager'
  255. *
  256. * @param string $name
  257. * @return string
  258. */
  259. abstract protected function getObjectManagerElementName($name);
  260. /**
  261. * Noun that describes the mapped objects such as Entity or Document.
  262. *
  263. * Will be used for autodetection of persistent objects directory.
  264. *
  265. * @return string
  266. */
  267. abstract protected function getMappingObjectDefaultName();
  268. /**
  269. * Relative path from the bundle root to the directory where mapping files reside.
  270. *
  271. * @return string
  272. */
  273. abstract protected function getMappingResourceConfigDirectory();
  274. /**
  275. * Extension used by the mapping files.
  276. *
  277. * @return string
  278. */
  279. abstract protected function getMappingResourceExtension();
  280. }