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
 * (c) Jonathan H. Wage <jonwage@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
/**
13
 * A symfony database driver for Doctrine.
14
 *
15
 * @package    symfony
16
 * @subpackage doctrine
17
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18
 * @author     Jonathan H. Wage <jonwage@gmail.com>
19
 * @version    SVN: $Id: sfDoctrineDatabase.class.php 28902 2010-03-30 20:57:27Z Jonathan.Wage $
20
 */
21
class sfDoctrineDatabase extends sfDatabase
22
{
23
  /**
24
   * Instance of the Doctrine_Connection for this instance of sfDoctrineDatabase.
25
   * Connection can be accessed by the getDoctrineConnection() accessor method.
26
   *
27
   * @var Doctrine_Connection $_doctrineConnection
28
   */
29
  protected $_doctrineConnection = null;
30
 
31
  /**
32
   * @var sfDoctrineConnectionProfiler
33
   **/
34
  protected $profiler = null;
35
 
36
  /**
37
   * Initialize a sfDoctrineDatabase connection with the given parameters.
38
   *
39
   * <code>
40
   * $parameters = array(
41
   *    'name'       => 'doctrine',
42
   *    'dsn'        => 'sqlite:////path/to/sqlite/db');
43
   *
44
   * $p = new sfDoctrineDatabase($parameters);
45
   * </code>
46
   *
47
   * @param array $parameters  Array of parameters used to initialize the database connection
48
   * @return void
49
   */
50
  public function initialize($parameters = array())
51
  {
52
    parent::initialize($parameters);
53
 
54
    if (null !== $this->_doctrineConnection)
55
    {
56
      return;
57
    }
58
 
59
    $dsn = $this->getParameter('dsn');
60
    $name = $this->getParameter('name');
61
 
62
    // Make sure we pass non-PEAR style DSNs as an array
63
    if ( !strpos($dsn, '://'))
64
    {
65
      $dsn = array($dsn, $this->getParameter('username'), $this->getParameter('password'));
66
    }
67
 
68
    // Make the Doctrine connection for $dsn and $name
69
    $configuration = sfProjectConfiguration::getActive();
70
    $dispatcher = $configuration->getEventDispatcher();
71
    $manager = Doctrine_Manager::getInstance();
72
 
73
    $this->_doctrineConnection = $manager->openConnection($dsn, $name);
74
 
75
    $attributes = $this->getParameter('attributes', array());
76
    foreach ($attributes as $name => $value)
77
    {
78
      if (is_string($name))
79
      {
80
        $stringName = $name;
81
        $name = constant('Doctrine_Core::ATTR_'.strtoupper($name));
82
      }
83
 
84
      if (is_string($value))
85
      {
86
        $valueConstantName = 'Doctrine_Core::'.strtoupper($stringName).'_'.strtoupper($value);
87
        $value = defined($valueConstantName) ? constant($valueConstantName) : $value;
88
      }
89
 
90
      $this->_doctrineConnection->setAttribute($name, $value);
91
    }
92
 
93
    $encoding = $this->getParameter('encoding', 'UTF8');
94
    $eventListener = new sfDoctrineConnectionListener($this->_doctrineConnection, $encoding);
95
    $this->_doctrineConnection->addListener($eventListener);
96
 
97
    // Load Query Profiler
98
    if ($this->getParameter('profiler', sfConfig::get('sf_debug')))
99
    {
100
      $this->profiler = new sfDoctrineConnectionProfiler($dispatcher, array(
101
        'logging' => $this->getParameter('logging', sfConfig::get('sf_logging_enabled')),
102
      ));
103
      $this->_doctrineConnection->addListener($this->profiler, 'symfony_profiler');
104
    }
105
 
106
    // Invoke the configuration methods for the connection if they exist (deprecated in favor of the "doctrine.configure_connection" event)
107
    $method = sprintf('configureDoctrineConnection%s', ucwords($this->_doctrineConnection->getName()));
108
 
109
    if (method_exists($configuration, 'configureDoctrineConnection') && ! method_exists($configuration, $method))
110
    {
111
      $configuration->configureDoctrineConnection($this->_doctrineConnection);
112
    }
113
 
114
    if (method_exists($configuration, $method))
115
    {
116
      $configuration->$method($this->_doctrineConnection);
117
    }
118
 
119
    $dispatcher->notify(new sfEvent($manager, 'doctrine.configure_connection', array('connection' => $this->_doctrineConnection, 'database' => $this)));
120
  }
121
 
122
  /**
123
   * Get the Doctrine_Connection instance.
124
   *
125
   * @return Doctrine_Connection $conn
126
   */
127
  public function getDoctrineConnection()
128
  {
129
    return $this->_doctrineConnection;
130
  }
131
 
132
  /**
133
   * Returns the connection profiler.
134
   *
135
   * @return sfDoctrineConnectionProfiler|null
136
   */
137
  public function getProfiler()
138
  {
139
    return $this->profiler;
140
  }
141
 
142
  /**
143
   * Initializes the connection and sets it to object.
144
   *
145
   * @return void
146
   */
147
  public function connect()
148
  {
149
    $this->connection = $this->_doctrineConnection->getDbh();
150
  }
151
 
152
  /**
153
   * Execute the shutdown procedure.
154
   *
155
   * @return void
156
   */
157
  public function shutdown()
158
  {
159
    if ($this->connection !== null)
160
    {
161
      $this->connection = null;
162
    }
163
    if ($this->_doctrineConnection !== null)
164
    {
165
      $this->_doctrineConnection->getManager()->closeConnection($this->_doctrineConnection);
166
    }
167
  }
168
}