Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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