1 <?php
2 /**
3 * Neevo - Tiny database layer for PHP. (http://neevo.smasty.net)
4 *
5 * This source file is subject to the MIT license that is bundled
6 * with this package in the file license.txt.
7 *
8 * Copyright (c) 2012 Smasty (http://smasty.net)
9 *
10 */
11
12 namespace Neevo;
13
14 use Exception;
15 use SplObjectStorage;
16
17
18 /**
19 * Main Neevo exception.
20 * @author Smasty
21 */
22 class NeevoException extends Exception implements ObservableInterface {
23
24
25 /** @var string */
26 protected $sql;
27
28 /** @var SplObjectStorage */
29 protected static $observers;
30
31
32 /**
33 * Constructs exception.
34 * @param string $message
35 * @param int $code
36 * @param string $sql Optional SQL command
37 * @param Exception $previous
38 */
39 public function __construct($message = '', $code = 0, $sql = null, Exception $previous = null){
40
41 parent::__construct($message, (int) $code, $previous);
42 $this->sql = $sql;
43 if(self::$observers === null)
44 self::$observers = new SplObjectStorage;
45 $this->notifyObservers(ObserverInterface::EXCEPTION);
46 }
47
48
49 /**
50 * Returns string representation of exception.
51 * @return string
52 */
53 public function __toString(){
54 return parent::__toString() . ($this->sql ? "\nSQL: $this->sql" : '');
55 }
56
57
58 /**
59 * Returns given SQL command.
60 * @return string
61 */
62 public function getSql(){
63 return $this->sql;
64 }
65
66
67 /**
68 * Attaches given observer to given event.
69 * @param ObserverInterface $observer
70 * @param int $event
71 */
72 public function attachObserver(ObserverInterface $observer, $event){
73 self::$observers->attach($observer, $event);
74 }
75
76
77 /**
78 * Detaches given observer.
79 * @param ObserverInterface $observer
80 */
81 public function detachObserver(ObserverInterface $observer){
82 self::$observers->detach($observer);
83 }
84
85
86 /**
87 * Notifies all observers attached to given event.
88 * @param int $event
89 */
90 public function notifyObservers($event){
91 foreach(self::$observers as $observer){
92 if($event & self::$observers->getInfo())
93 $observer->updateStatus($this, $event);
94 }
95 }
96
97
98 }
99