- <?php
- /**
- * PHLY - PHp LibrarY
- *
- * PHLY is a library of PHP classes designed with the following intentions:
- * - Loosely coupled; dependencies should be few, and no base class should be
- * necessary.
- * - Extendible; all classes should be easily extendible. This may be via
- * observers, interfaces, adapters, etc.. The base class should solve 80% of
- * usage, and allow extensions to the class to fill in the remainder.
- * - Designed for PHP5 and up; all classes should make use of PHP5's features.
- * - Documented; all classes should minimally have excellent API-level
- * documentation, with use cases in the class docblock.
- * - Tested; all classes should have (passing) unit tests accompanying them.
- * - Open source and commercial friendly; all classes should use a
- * commercial-friendly open source license. The BSD license is one such
- * example.
- *
- * @license New BSD, http://www.opensource.org/licenses/bsd-license.php
- * @package Phly
- * @copyright 2006 - Present, Matthew Weier O'Phinney
- */
-
- /**
- * Phly Exception Handling
- *
- * Phly_Exception implements an Observer pattern. You may build observer
- * classes that receive notifications when Phly_Exceptions (or classes
- * derived from it) are raised. Such classes need only have an event() method
- * that accepts a Phly_Exception object, and have to register their class or
- * an instance with Phly_Exception using {@link attach()}.
- *
- * @category Phly
- * @subpackage Phly_Exception
- * @copyright 2006 - Present, Matthew Weier O'Phinney
- * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
- * @version @release-version@
- */
- class Phly_Exception extends Exception
- {
- /**
- * Array of observers
- * @var array
- * @static
- * @access protected
- */
- private static $observers = array();
-
- /**
- * Constructor
- *
- * @access public
- * @param string
- * @param int
- * @param int
- */
- public function __construct($message, $code = 0)
- {
- parent::__construct($message, $code);
-
- $this->notify();
- }
-
- /**
- * String representation of exception
- *
- * @access public
- * @return void
- */
- public function __toString()
- {
- return __CLASS__ . ': [' . $this->code . ']: ' . $this->message . "\n";
- }
-
- /**
- * Attach an observer
- *
- * @static
- * @access public
- * @param string|object $class Classname or object
- * @return void
- */
- final public static function attach($class)
- {
- if (is_object($class) || class_exists($class)) {
- array_push(self::$observers, $class);
- }
- }
-
- /**
- * Notify observers of an error event
- *
- * @access public
- * @return void
- */
- final public function notify()
- {
- foreach (self::$observers as $class) {
- if (is_callable(array($class, 'event'))) {
- call_user_func(array($class, 'event'), $this);
- }
- }
- }
- }