PropelExtension.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Symfony\Framework\PropelBundle\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\Definition;
  7. use Symfony\Components\DependencyInjection\Reference;
  8. class PropelExtension extends LoaderExtension
  9. {
  10. protected $resources = array(
  11. 'propel' => 'propel.xml',
  12. );
  13. /**
  14. * Loads the Propel configuration.
  15. *
  16. * @param array $config A configuration array
  17. *
  18. * @return BuilderConfiguration A BuilderConfiguration instance
  19. */
  20. public function configLoad($config)
  21. {
  22. if (!isset($config['path'])) {
  23. throw new \InvalidArgumentException('The "path" parameter is mandatory.');
  24. }
  25. $configuration = new BuilderConfiguration();
  26. $configuration->setParameter('propel.path', $config['path']);
  27. if (isset($config['phing_path']))
  28. {
  29. $configuration->setParameter('propel.phing_path', $config['phing_path']);
  30. }
  31. return $configuration;
  32. }
  33. /**
  34. * Loads the DBAL configuration.
  35. *
  36. * @param array $config A configuration array
  37. *
  38. * @return BuilderConfiguration A BuilderConfiguration instance
  39. */
  40. public function dbalLoad($config)
  41. {
  42. $configuration = new BuilderConfiguration();
  43. $loader = new XmlFileLoader(__DIR__.'/../Resources/config');
  44. $configuration->merge($loader->load($this->resources['propel']));
  45. $defaultConnection = array(
  46. 'driver' => 'mysql',
  47. 'user' => 'root',
  48. 'password' => null,
  49. 'dsn' => null,
  50. // FIXME: should be automatically changed based on %kernel.debug%
  51. 'classname' => 'DebugPDO', //'PropelPDO',
  52. 'options' => array(),
  53. 'attributes' => array(),
  54. // FIXME: Mysql wants UTF8, not UTF-8 (%kernel.charset%)
  55. 'settings' => array('charset' => array('value' => 'UTF8')),
  56. );
  57. $config['default_connection'] = isset($config['default_connection']) ? $config['default_connection'] : 'default';
  58. $connections = array();
  59. if (isset($config['connections'])) {
  60. foreach ($config['connections'] as $name => $connection) {
  61. $connections[isset($connection['id']) ? $connection['id'] : $name] = $connection;
  62. }
  63. } else {
  64. $connections = array($config['default_connection'] => $config);
  65. }
  66. $c = array(
  67. // FIXME: should be the same value as %zend.logger.priority%
  68. 'log' => array('level' => 7),
  69. 'datasources' => array(),
  70. );
  71. foreach ($connections as $name => $connection) {
  72. $connection = array_replace($defaultConnection, $connection);
  73. $c['datasources'][$name] = array(
  74. 'adapter' => $connection['driver'],
  75. 'connection' => array(
  76. 'dsn' => $connection['dsn'],
  77. 'user' => $connection['user'],
  78. 'password' => $connection['password'],
  79. 'classname' => $connection['classname'],
  80. 'options' => $connection['options'],
  81. 'attributes' => $connection['attributes'],
  82. 'settings' => $connection['settings'],
  83. ),
  84. );
  85. }
  86. $configuration->getDefinition('propel.configuration')->setArguments(array($c));
  87. return $configuration;
  88. }
  89. /**
  90. * Returns the base path for the XSD files.
  91. *
  92. * @return string The XSD base path
  93. */
  94. public function getXsdValidationBasePath()
  95. {
  96. return __DIR__.'/../Resources/config/';
  97. }
  98. /**
  99. * Returns the namespace to be used for this extension (XML namespace).
  100. *
  101. * @return string The XML namespace
  102. */
  103. public function getNamespace()
  104. {
  105. return 'http://www.symfony-project.org/schema/dic/propel';
  106. }
  107. /**
  108. * Returns the recommended alias to use in XML.
  109. *
  110. * This alias is also the mandatory prefix to use when using YAML.
  111. *
  112. * @return string The alias
  113. */
  114. public function getAlias()
  115. {
  116. return 'propel';
  117. }
  118. }