/lib/plugins/sfPropelPlugin/lib/database/sfPropelDatabase.class.php

https://github.com/bheneka/gitta · PHP · 189 lines · 107 code · 20 blank · 62 comment · 13 complexity · 0a2305324acf74902de0de2302b9dc64 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * A symfony database driver for Propel.
  11. *
  12. * @package sfPropelPlugin
  13. * @subpackage database
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id$
  16. */
  17. class sfPropelDatabase extends sfPDODatabase
  18. {
  19. /**
  20. * Returns the current propel configuration.
  21. *
  22. * @return array
  23. *
  24. * @deprecated Use Propel::getConfiguration() instead
  25. */
  26. static public function getConfiguration()
  27. {
  28. return array('propel' => Propel::getConfiguration(PropelConfiguration::TYPE_ARRAY));
  29. }
  30. /**
  31. * Configures a Propel datasource.
  32. *
  33. * @param array $parameters The datasource parameters
  34. * @param string $name The datasource name
  35. */
  36. public function initialize($parameters = null, $name = 'propel')
  37. {
  38. parent::initialize($parameters);
  39. if (!$this->hasParameter('datasource') && $this->hasParameter('name'))
  40. {
  41. $this->setParameter('datasource', $this->getParameter('name'));
  42. }
  43. elseif (!$this->hasParameter('datasource') && !empty($name))
  44. {
  45. $this->setParameter('datasource', $name);
  46. }
  47. $this->addConfig();
  48. // mark the first connection as the default
  49. if (!Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT)->getParameter('datasources.default'))
  50. {
  51. $this->setDefaultConfig();
  52. }
  53. // for BC
  54. if ($this->getParameter('pooling', false))
  55. {
  56. Propel::enableInstancePooling();
  57. }
  58. else
  59. {
  60. Propel::disableInstancePooling();
  61. }
  62. }
  63. /**
  64. * Connect to the database.
  65. *
  66. * Stores the PDO connection in $connection.
  67. *
  68. * @return void
  69. */
  70. public function connect()
  71. {
  72. $this->connection = Propel::getConnection($this->getParameter('datasource'));
  73. }
  74. /**
  75. * Marks the current database as the default.
  76. */
  77. public function setDefaultConfig()
  78. {
  79. Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT)->setParameter('datasources.default', $this->getParameter('datasource'));
  80. }
  81. /**
  82. * Adds configuration for current datasource.
  83. */
  84. public function addConfig()
  85. {
  86. if ($dsn = $this->getParameter('dsn'))
  87. {
  88. $params = $this->parseDsn($dsn);
  89. $options = array('dsn', 'phptype', 'hostspec', 'database', 'username', 'password', 'port', 'protocol', 'encoding', 'persistent', 'socket', 'compat_assoc_lower', 'compat_rtrim_string');
  90. foreach ($options as $option)
  91. {
  92. if (!$this->getParameter($option) && isset($params[$option]))
  93. {
  94. $this->setParameter($option, $params[$option]);
  95. }
  96. }
  97. }
  98. if ($this->hasParameter('persistent'))
  99. {
  100. // for BC
  101. $this->setParameter('options', array_merge(
  102. $this->getParameter('options', array()),
  103. array('ATTR_PERSISTENT' => $this->getParameter('persistent'))
  104. ));
  105. }
  106. $propelConfiguration = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
  107. if ($this->hasParameter('debug'))
  108. {
  109. $propelConfiguration->setParameter('debugpdo.logging', sfToolkit::arrayDeepMerge(
  110. $propelConfiguration->getParameter('debugpdo.logging', array()),
  111. $this->getParameter('debug')
  112. ));
  113. }
  114. $event = new sfEvent($propelConfiguration, 'propel.filter_connection_config', array('name' => $this->getParameter('datasource'), 'database' => $this));
  115. $event = sfProjectConfiguration::getActive()->getEventDispatcher()->filter($event, array(
  116. 'adapter' => $this->getParameter('phptype'),
  117. 'connection' => array(
  118. 'dsn' => $this->getParameter('dsn'),
  119. 'user' => $this->getParameter('username'),
  120. 'password' => $this->getParameter('password'),
  121. 'classname' => $this->getParameter('classname', 'PropelPDO'),
  122. 'options' => $this->getParameter('options', array()),
  123. 'settings' => array(
  124. 'charset' => array('value' => $this->getParameter('encoding', sfConfig::get('sf_charset'))),
  125. 'queries' => $this->getParameter('queries', array()),
  126. ),
  127. ),
  128. ));
  129. $propelConfiguration->setParameter('datasources.'.$this->getParameter('datasource'), $event->getReturnValue());
  130. }
  131. /**
  132. * Sets database configuration parameter
  133. *
  134. * @param string $key
  135. * @param mixed $value
  136. */
  137. public function setConnectionParameter($key, $value)
  138. {
  139. if ('host' == $key)
  140. {
  141. $key = 'hostspec';
  142. }
  143. Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT)->setParameter('datasources.'.$this->getParameter('datasource').'.connection.'.$key, $value);
  144. $this->setParameter($key, $value);
  145. }
  146. /**
  147. * Execute the shutdown procedure.
  148. *
  149. * @return void
  150. */
  151. public function shutdown()
  152. {
  153. if (null !== $this->connection)
  154. {
  155. @$this->connection = null;
  156. }
  157. }
  158. /**
  159. * Parses PDO style DSN.
  160. *
  161. * @param string $dsn
  162. *
  163. * @return array the parsed dsn
  164. */
  165. protected function parseDsn($dsn)
  166. {
  167. return array('phptype' => substr($dsn, 0, strpos($dsn, ':')));
  168. }
  169. }