PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr2/src/vendor/symfony/src/Symfony/Framework/DoctrineMongoDBBundle/DependencyInjection/MongoDBExtension.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 179 lines | 105 code | 24 blank | 50 comment | 11 complexity | 0bdd5102e7ba5c03f7e7c9c36c87f376 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. namespace Symfony\Framework\DoctrineMongoDBBundle\DependencyInjection;
  3. use Symfony\Components\DependencyInjection\Loader\LoaderExtension;
  4. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  5. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  6. use Symfony\Components\DependencyInjection\Reference;
  7. use Symfony\Components\DependencyInjection\Definition;
  8. use Symfony\Components\DependencyInjection\FileResource;
  9. /**
  10. * Doctrine MongoDB ODM extension.
  11. *
  12. * @author Bulat Shakirzyanov <bulat@theopenskyproject.com>
  13. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  14. * @author Jonathan H. Wage <jonwage@gmail.com>
  15. *
  16. * @todo Add support for multiple document managers
  17. */
  18. class MongoDBExtension extends LoaderExtension
  19. {
  20. protected $bundles;
  21. protected $resources = array(
  22. 'mongodb' => 'mongodb.xml',
  23. );
  24. public function __construct(array $bundles)
  25. {
  26. $this->bundles = $bundles;
  27. }
  28. public function mongodbLoad($config, BuilderConfiguration $configuration)
  29. {
  30. $loader = new XmlFileLoader(__DIR__.'/../Resources/config');
  31. $configuration->merge($loader->load($this->resources['mongodb']));
  32. if (!$configuration->hasDefinition('doctrine.odm.mongodb.document_manager')) {
  33. $configuration->setParameter('doctrine.odm.mongodb.mapping_dirs', $this->findBundleSubpaths('Resources/config/doctrine/metadata', $configuration));
  34. $configuration->setParameter('doctrine.odm.mongodb.document_dirs', $this->findBundleSubpaths('Document', $configuration));
  35. $configuration->setDefinition('doctrine.odm.mongodb.metadata', $this->buildMetadataDefinition($configuration));
  36. }
  37. foreach (array('host', 'port', 'database') as $key) {
  38. if (isset($config[$key])) {
  39. $configuration->setParameter('doctrine.odm.mongodb.default_'.$key, $config[$key]);
  40. }
  41. }
  42. foreach (array('proxy_dir', 'auto_generate_proxy_classes') as $key) {
  43. if (isset($config[$key])) {
  44. $configuration->setParameter('doctrine.odm.mongodb.'.$key, $config[$key]);
  45. }
  46. }
  47. foreach (array('cache', 'metadata') as $key) {
  48. if (isset($config[$key])) {
  49. $configuration->setAlias('doctrine.odm.mongodb.'.$key, 'doctrine.odm.mongodb.'.$key.'.'.$config[$key]);
  50. }
  51. }
  52. return $configuration;
  53. }
  54. /**
  55. * Finds existing bundle subpaths.
  56. *
  57. * @param string $path A subpath to check for
  58. * @param Symfony\Components\DependencyInjection\BuilderConfiguration $configuration A builder configuration
  59. *
  60. * @return array An array of absolute directory paths
  61. */
  62. protected function findBundleSubpaths($path, BuilderConfiguration $configuration)
  63. {
  64. $dirs = array();
  65. foreach ($this->bundles as $bundle) {
  66. $reflection = new \ReflectionClass($bundle);
  67. if (is_dir($dir = dirname($reflection->getFilename()).'/'.$path)) {
  68. $dirs[] = $dir;
  69. $configuration->addResource(new FileResource($dir));
  70. } else {
  71. // add the closest existing parent directory as a file resource
  72. do {
  73. $dir = dirname($dir);
  74. } while (!is_dir($dir));
  75. $configuration->addResource(new FileResource($dir));
  76. }
  77. }
  78. return $dirs;
  79. }
  80. /**
  81. * Detects and builds the appropriate metadata driver for each bundle.
  82. *
  83. * @param Symfony\Components\DependencyInjection\BuilderConfiguration $configuration A builder configuration
  84. *
  85. * @return Symfony\Components\DependencyInjection\Definition A definition for the metadata service
  86. */
  87. protected function buildMetadataDefinition(BuilderConfiguration $configuration)
  88. {
  89. $definition = new Definition('%doctrine.odm.mongodb.metadata.chain_class%');
  90. foreach ($this->bundles as $bundle) {
  91. $reflection = new \ReflectionClass($bundle);
  92. if ($driver = static::detectMetadataDriver(dirname($reflection->getFilename()), $configuration)) {
  93. $definition->addMethodCall('addDriver', array(
  94. new Reference('doctrine.odm.mongodb.metadata.'.$driver),
  95. $reflection->getNamespaceName().'\\Document',
  96. ));
  97. }
  98. }
  99. return $definition;
  100. }
  101. /**
  102. * Detects what metadata driver to use for the supplied directory.
  103. *
  104. * @param string $dir A directory path
  105. * @param Symfony\Components\DependencyInjection\BuilderConfiguration $configuration A builder configuration
  106. *
  107. * @return string|null A metadata driver short name, if one can be detected
  108. */
  109. static protected function detectMetadataDriver($dir, BuilderConfiguration $configuration)
  110. {
  111. // add the closest existing directory as a resource
  112. $resource = $dir.'/Resources/config/doctrine/metadata';
  113. while (!is_dir($resource)) {
  114. $resource = dirname($resource);
  115. }
  116. $configuration->addResource(new FileResource($resource));
  117. if (count(glob($dir.'/Resources/config/doctrine/metadata/*.xml'))) {
  118. return 'xml';
  119. } elseif (count(glob($dir.'/Resources/config/doctrine/metadata/*.yml'))) {
  120. return 'yml';
  121. }
  122. // add the directory itself as a resource
  123. $configuration->addResource(new FileResource($dir));
  124. if (is_dir($dir.'/Document')) {
  125. return 'annotation';
  126. }
  127. }
  128. /**
  129. * Returns the namespace to be used for this extension (XML namespace).
  130. *
  131. * @return string The XML namespace
  132. */
  133. public function getNamespace()
  134. {
  135. return 'http://www.symfony-project.org/schema/dic/doctrine/odm/mongodb';
  136. }
  137. /**
  138. * @return string
  139. */
  140. public function getXsdValidationBasePath()
  141. {
  142. return __DIR__.'/../Resources/config';
  143. }
  144. /**
  145. * Returns the recommended alias to use in XML.
  146. *
  147. * This alias is also the mandatory prefix to use when using YAML.
  148. *
  149. * @return string The alias
  150. */
  151. public function getAlias()
  152. {
  153. return 'doctrine_odm';
  154. }
  155. }