source: pro-violet-viettel/sourcecode/api.violet.vn/www/plugins/sfPropel13Plugin/lib/propel/database/sfPropel13Database.class.php @ 289

Last change on this file since 289 was 289, checked in by dungnv, 11 years ago
File size: 5.9 KB
Line 
1<?php
2/*
3 * This file is part of the sfPropel13Plugin package.
4 * (c) 2007 Joshua May <notjosh@gmail.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
10class sfPropel13Database extends sfDatabase
11{
12  static protected $config = array();
13 
14  /**
15   * @see sfDatabase::initialize()
16   */
17  public function initialize($parameters = null, $name = 'propel')
18  {
19    parent::initialize($parameters);
20    if (!$this->hasParameter('datasource'))
21    {
22      $this->setParameter('datasource', $name);
23    }
24
25    $this->addConfig();
26
27    $is_default = $this->getParameter('is_default', false);
28
29    // first defined if none listed as default
30    if ($is_default || count(self::$config['propel']['datasources']) == 1)
31    {
32      $this->setDefaultConfig();
33    }
34       
35    // setup propel
36    if (null !== $this->getParameter('instance-pooling'))
37    {
38      $instancePooling = $this->getParameter('instance-pooling');
39      if (is_bool($instancePooling))
40      {
41        if ($instancePooling)
42        {
43          Propel::enableInstancePooling();
44        }
45        else
46        {
47          Propel::disableInstancePooling();
48        }
49      }
50    }
51
52    Propel::setConfiguration(self::$config);
53    Propel::initialize();
54  }
55 
56  public function setDefaultConfig()
57  {
58    self::$config['propel']['datasources']['default'] = $this->getParameter('datasource');
59  }
60
61  public function addConfig()
62  {
63        if ($dsn = $this->getParameter('dsn'))
64        {
65          $params = array();
66
67                // check for non-pdo dsn
68                if (false !== strpos($dsn, '//'))
69                {
70        // derive pdo dsn (etc) from old style dsn
71        $params = $this->parseOldDsn($dsn);
72
73        $dsn = $params['phptype'] . ':dbname=' . $params['database'] . ';host=' . $params['hostspec'];
74        $this->setParameter('dsn', $dsn);
75      }
76      else
77      {
78        $params = $this->parseDsn($dsn);
79      }
80
81      $options = array('phptype', 'hostspec', 'database', 'username', 'password', 'port', 'protocol', 'encoding', 'persistent');
82      foreach ($options as $option)
83      {
84        if (!$this->getParameter($option) && isset($params[$option]))
85        {
86          $this->setParameter($option, $params[$option]);
87        }
88      }
89        }
90
91    self::$config['propel']['datasources'][$this->getParameter('datasource')] =
92      array(
93        'adapter'      => $this->getParameter('phptype'),
94        'connection'   =>
95        array(
96          'dsn' => $this->getParameter('dsn'),
97          'user' => $this->getParameter('username'),
98          'password' => $this->getParameter('password'),
99
100          // not sure which (if any) of these are still relevant now that we use pdo.. doesn't break if i leave them though..?
101          'phptype'    => $this->getParameter('phptype'),
102          'hostspec'   => $this->getParameter('hostspec'),
103          'port'       => $this->getParameter('port'),
104          'encoding'   => $this->getParameter('encoding'),
105          'persistent' => $this->getParameter('persistent'),
106          'protocol'   => $this->getParameter('protocol'),
107        ),
108      );
109  }
110
111  /**
112   * parse the new styled dsn, really i only want to grab the 'phptype' out
113   *
114   * @param string $dsn
115   * @return array
116   */
117  private function parseDsn($dsn)
118  {
119    // i only really need the phptype, for now!
120
121    return array(
122      'phptype' => substr($dsn, 0, strpos($dsn, ':'))
123    );
124  }
125
126  /**
127   * this is the old Creole::parseDSN method, so i can parse old dsn's and connect via pdo still
128   *
129   * @param string $dsn
130   * @return array
131   */
132  private function parseOldDsn($dsn)
133  {
134    if (is_array($dsn))
135    {
136        return $dsn;
137    }
138
139    $parsed = array(
140        'phptype'  => null,
141        'username' => null,
142        'password' => null,
143        'protocol' => null,
144        'hostspec' => null,
145        'port'     => null,
146        'socket'   => null,
147        'database' => null
148    );
149
150    $info = parse_url($dsn);
151
152    if (count($info) === 1)
153    { // if there's only one element in result, then it must be the phptype
154        $parsed['phptype'] = array_pop($info);
155        return $parsed;
156    }
157
158    // some values can be copied directly
159    $parsed['phptype'] = @$info['scheme'];
160    $parsed['username'] = @$info['user'];
161    $parsed['password'] = @$info['pass'];
162    $parsed['port'] = @$info['port'];
163
164    $host = @$info['host'];
165    if (false !== ($pluspos = strpos($host, '+')))
166    {
167      $parsed['protocol'] = substr($host,0,$pluspos);
168
169      if ($parsed['protocol'] === 'unix')
170      {
171        $parsed['socket'] = substr($host,$pluspos+1);
172      }
173      else
174      {
175          $parsed['hostspec'] = substr($host,$pluspos+1);
176      }
177    }
178    else
179    {
180        $parsed['hostspec'] = $host;
181    }
182
183    if (isset($info['path']))
184    {
185        $parsed['database'] = substr($info['path'], 1); // remove first char, which is '/'
186    }
187
188    if (isset($info['query']))
189    {
190      $opts = explode('&', $info['query']);
191      foreach ($opts as $opt)
192      {
193        list($key, $value) = explode('=', $opt);
194
195        if (!isset($parsed[$key]))
196        { // don't allow params overwrite
197          $parsed[$key] = urldecode($value);
198        }
199      }
200    }
201
202    return $parsed;
203  }
204
205  public static function getConfiguration()
206  {
207    return self::$config;
208  }
209
210  public function setConnectionParameter($key, $value)
211  {
212    if ($key == 'host')
213    {
214      $key = 'hostspec';
215    }
216
217    self::$config['propel']['datasources'][$this->getParameter('datasource')]['connection'][$key] = $value;
218    $this->setParameter($key, $value);
219  }
220       
221  /**
222   * Connect to the database.
223   * Stores the PDO connection in $connection
224   *
225   */
226  public function connect()
227  {
228        $this->connection = Propel::getConnection();
229  }
230
231  /**
232   * Execute the shutdown procedure.
233   *
234   * @return void
235   *
236   * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database.
237   */
238  public function shutdown()
239  {
240    if ($this->connection !== null)
241    {
242      @$this->connection = null;
243    }
244  }
245}
Note: See TracBrowser for help on using the repository browser.