Hoa central
Dal.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Database;
38 
39 use Hoa\Core;
40 
50 {
56  const DBA = 'Dba';
57 
63  const DBX = 'Dbx';
64 
70  const ODBC = 'Odbc';
71 
77  const PDO = 'Pdo';
78 
84  private static $_instance = [];
85 
91  private static $_id = null;
92 
98  protected $__id = null;
99 
105  protected $_layer = null;
106 
112  protected static $_parameters = null;
113 
114 
115 
128  private function __construct(
129  $dalName,
130  $dsn,
131  $username,
132  $password,
133  Array $driverOptions = []
134  ) {
135  // Please see https://bugs.php.net/55154.
136  if (0 !== preg_match('#^sqlite:(.+)$#i', $dsn, $matches)) {
137  $dsn = 'sqlite:' . resolve($matches[1]);
138  }
139 
140  $id = $this->__id = self::$_id;
141  $event = 'hoa://Event/Database/' . $id;
142 
143  Core\Event::register($event . ':opened', $this);
144  Core\Event::register($event . ':closed', $this);
145 
146  $this->setDal(dnew(
147  '\Hoa\Database\Layer\\' . $dalName,
148  [$dsn, $username, $password, $driverOptions]
149  ));
150 
151  Core\Event::notify(
152  $event . ':opened',
153  $this,
154  new Core\Event\Bucket([
155  'id' => $id,
156  'dsn' => $dsn,
157  'username' => $username,
158  'driverOptions' => $driverOptions
159  ])
160  );
161 
162  return;
163  }
164 
171  public static function initializeParameters(Array $parameters = [])
172  {
173  self::$_parameters = new Core\Parameter(
174  __CLASS__,
175  [],
176  [
186  'connection.autoload' => null // or connection ID, e.g. 'default'.
187  ]
188  );
189  self::$_parameters->setParameters($parameters);
190 
191  return;
192  }
193 
206  public static function getInstance(
207  $id,
208  $dalName = null,
209  $dsn = null,
210  $username = null,
211  $password = null,
212  Array $driverOptions = []
213  ) {
214  if (null === self::$_parameters) {
215  self::initializeParameters();
216  }
217 
218  self::$_id = $id;
219 
220  if (isset(self::$_instance[$id])) {
221  return self::$_instance[$id];
222  }
223 
224  if (null === $dalName &&
225  null === $dsn &&
226  null === $username &&
227  null === $password &&
228  empty($driverOptions)) {
229  $list = self::$_parameters->unlinearizeBranche('connection.list');
230 
231  if (!isset($list[$id])) {
232  throw new Exception(
233  'Connection ID %s does not exist in the connection list.',
234  0,
235  $id
236  );
237  }
238 
239  $handle = $list[$id];
240  $dalName = @$handle['dal'] ?: 'Undefined';
241  $dsn = @$handle['dsn'] ?: '';
242  $username = @$handle['username'] ?: '';
243  $password = @$handle['password'] ?: '';
244  $driverOptions = @$handle['options'] ?: [];
245  }
246 
247  return self::$_instance[$id] = new self(
248  $dalName,
249  $dsn,
250  $username,
251  $password,
252  $driverOptions
253  );
254  }
255 
264  public static function getLastInstance()
265  {
266  if (null === self::$_parameters) {
267  self::initializeParameters();
268  }
269 
270  if (null === self::$_id) {
271  $autoload = self::$_parameters->getFormattedParameter(
272  'connection.autoload'
273  );
274 
275  if (null !== $autoload) {
276  self::getInstance($autoload);
277  }
278  }
279 
280  if (null === self::$_id) {
281  throw new Exception(
282  'No instance was set, cannot return the last instance.',
283  1
284  );
285  }
286 
287  return self::$_instance[self::$_id];
288  }
289 
295  public function getParameters()
296  {
297  return self::$_parameters;
298  }
299 
305  public function close()
306  {
307  $id = $this->getId();
308  $event = 'hoa://Event/Database/' . $id;
309 
310  $this->_layer = null;
311  self::$_id = null;
312  unset(self::$_instance[$id]);
313 
314  Core\Event::notify(
315  $event . ':closed',
316  $this,
317  new Core\Event\Bucket(['id' => $id])
318  );
319 
320  Core\Event::unregister($event . ':opened');
321  Core\Event::unregister($event . ':closed');
322 
323  return true;
324  }
325 
332  protected function setDal(IDal\Wrapper $dal)
333  {
334  $old = $this->_layer;
335  $this->_layer = $dal;
336 
337  return $old;
338  }
339 
345  protected function getDal()
346  {
347  return $this->_layer;
348  }
349 
356  public function beginTransaction()
357  {
358  return $this->getDal()->beginTransaction();
359  }
360 
367  public function commit()
368  {
369  return $this->getDal()->commit();
370  }
371 
378  public function rollBack()
379  {
380  return $this->getDal()->rollBack();
381  }
382 
391  public function lastInsertId($name = null)
392  {
393  if (null === $name) {
394  return $this->getDal()->lastInsertId();
395  }
396 
397  return $this->getDal()->lastInsertId($name);
398  }
399 
410  public function prepare($statement, Array $options = [])
411  {
412  return new DalStatement(
413  $this->getDal()->prepare(
414  $statement, $options
415  )
416  );
417  }
418 
428  public function quote($string = null, $type = -1)
429  {
430  if ($type < 0) {
431  return $this->getDal()->quote($string);
432  }
433 
434  return $this->getDal()->quote($string, $type);
435  }
436 
445  public function query($statement)
446  {
447  return new DalStatement(
448  $this->getDal()->query($statement)
449  );
450  }
451 
459  public function errorCode()
460  {
461  return $this->getDal()->errorCode();
462  }
463 
471  public function errorInfo()
472  {
473  return $this->getDal()->errorInfo();
474  }
475 
482  public function getAvailableDrivers()
483  {
484  return $this->getDal()->getAvailableDrivers();
485  }
486 
494  public function setAttributes(Array $attributes)
495  {
496  return $this->getDal()->setAttributes($attributes);
497  }
498 
507  public function setAttribute($attribute, $value)
508  {
509  return $this->getDal()->setAtribute($attribute, $value);
510  }
511 
518  public function getAttributes()
519  {
520  return $this->getDal()->getAttributes();
521  }
522 
530  public function getAttribute($attribute)
531  {
532  return $this->getDal()->getAttribute($attribute);
533  }
534 
540  public function getId()
541  {
542  return $this->__id;
543  }
544 }
static getLastInstance()
Definition: Dal.php:264
lastInsertId($name=null)
Definition: Dal.php:391
query($statement)
Definition: Dal.php:445
getAttribute($attribute)
Definition: Dal.php:530
prepare($statement, Array $options=[])
Definition: Dal.php:410
const ODBC
Definition: Dal.php:70
const PDO
Definition: Dal.php:77
static getInstance($id, $dalName=null, $dsn=null, $username=null, $password=null, Array $driverOptions=[])
Definition: Dal.php:206
static $_instance
Definition: Dal.php:84
static getInstance()
Definition: Core.php:193
static $_parameters
Definition: Dal.php:112
static initializeParameters(Array $parameters=[])
Definition: Dal.php:171
const DBX
Definition: Dal.php:63
setDal(IDal\Wrapper $dal)
Definition: Dal.php:332
static $_id
Definition: Dal.php:91
setAttribute($attribute, $value)
Definition: Dal.php:507
setAttributes(Array $attributes)
Definition: Dal.php:494
getAvailableDrivers()
Definition: Dal.php:482
const DBA
Definition: Dal.php:56
__construct($dalName, $dsn, $username, $password, Array $driverOptions=[])
Definition: Dal.php:128
beginTransaction()
Definition: Dal.php:356
quote($string=null, $type=-1)
Definition: Dal.php:428