DoctrineExtension.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Symfony\Framework\DoctrineBundle\DependencyInjection;
  3. use Symfony\Components\DependencyInjection\Loader\LoaderExtension;
  4. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  5. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  6. /*
  7. * This file is part of the symfony framework.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * This source file is subject to the MIT license that is bundled
  12. * with this source code in the file LICENSE.
  13. */
  14. /**
  15. * DoctrineExtension is an extension for the Doctrine DBAL and ORM library.
  16. *
  17. * @package symfony
  18. * @subpackage dependency_injection
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. class DoctrineExtension extends LoaderExtension
  22. {
  23. protected $resources = array(
  24. 'dbal' => 'dbal.xml',
  25. 'orm' => 'orm.xml',
  26. );
  27. protected $alias;
  28. public function setAlias($alias)
  29. {
  30. $this->alias = $alias;
  31. }
  32. /**
  33. * Loads the DBAL configuration.
  34. *
  35. * Usage example:
  36. *
  37. * <doctrine:dbal dbname="sfweb" user="root" />
  38. *
  39. * @param array $config A configuration array
  40. *
  41. * @return BuilderConfiguration A BuilderConfiguration instance
  42. */
  43. public function dbalLoad($config)
  44. {
  45. $configuration = new BuilderConfiguration();
  46. $loader = new XmlFileLoader(__DIR__.'/../Resources/config');
  47. $configuration->merge($loader->load($this->resources['dbal']));
  48. foreach (array('dbname', 'host', 'user', 'password', 'path', 'port') as $key)
  49. {
  50. if (isset($config[$key]))
  51. {
  52. $configuration->setParameter('doctrine.dbal.'.$key, $config[$key]);
  53. }
  54. }
  55. if (isset($config['options']))
  56. {
  57. $configuration->setParameter('doctrine.dbal.driver.options', $config['options']);
  58. }
  59. if (isset($config['driver']))
  60. {
  61. $class = $config['driver'];
  62. if (in_array($class, array('OCI8', 'PDOMsSql', 'PDOMySql', 'PDOOracle', 'PDOPgSql', 'PDOSqlite')))
  63. {
  64. $class = 'Doctrine\\DBAL\\Driver\\'.$class.'\\Driver';
  65. }
  66. $configuration->setParameter('doctrine.dbal.driver.class', $class);
  67. }
  68. $configuration->setAlias('database_connection', null !== $this->alias ? $this->alias : 'doctrine.dbal.connection');
  69. return $configuration;
  70. }
  71. /**
  72. * Loads the Doctrine ORM configuration.
  73. *
  74. * @param array $config A configuration array
  75. *
  76. * @return BuilderConfiguration A BuilderConfiguration instance
  77. */
  78. public function ormLoad($config)
  79. {
  80. $configuration = new BuilderConfiguration();
  81. $loader = new XmlFileLoader(__DIR__.'/../Resources/config');
  82. $configuration->merge($loader->load($this->resources['orm']));
  83. return $configuration;
  84. }
  85. /**
  86. * Returns the base path for the XSD files.
  87. *
  88. * @return string The XSD base path
  89. */
  90. public function getXsdValidationBasePath()
  91. {
  92. return __DIR__.'/../Resources/config/';
  93. }
  94. /**
  95. * Returns the namespace to be used for this extension (XML namespace).
  96. *
  97. * @return string The XML namespace
  98. */
  99. public function getNamespace()
  100. {
  101. return 'http://www.symfony-project.org/schema/dic/doctrine';
  102. }
  103. /**
  104. * Returns the recommanded alias to use in XML.
  105. *
  106. * This alias is also the mandatory prefix to use when using YAML.
  107. *
  108. * @return string The alias
  109. */
  110. public function getAlias()
  111. {
  112. return 'doctrine';
  113. }
  114. }