Updated the Error class from a static class to an instantiatable one.
git-svn-id: http://svn.cleancode.org/svn/pickles@68 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
parent
ecf86b9818
commit
20fd236cb0
9 changed files with 177 additions and 255 deletions
|
@ -27,135 +27,78 @@
|
|||
/**
|
||||
* Error handling class
|
||||
*
|
||||
* Handles (for the most part) all the errors and warnings that are encountered
|
||||
* by the PICKLES core classes. Unfortunately this was written, but never really
|
||||
* utilized correctly, so I'm thinking it may be better to remove it entirely or
|
||||
* add logic to appropriately report the errors, perhaps as a variable in the
|
||||
* model data array.
|
||||
*
|
||||
* @todo Internally document the functions better.
|
||||
* @todo Convert the class to extend Singleton.
|
||||
* @todo Quite possibly revamp the class entirely as it is still left over
|
||||
* from the previous iteration of PICKLES (the system received a kick
|
||||
* in the ass Q2 of 2008 to move in a more object-oriented direction,
|
||||
* previously models were procedural files and not instantiated
|
||||
* classes. I want to thank Joe Stump for the direction I took.
|
||||
* Handles (for the most part) all the errors and warnings that
|
||||
* are encountered by the PICKLES core classes. Usage is optional
|
||||
* for site level code. Errors are logged but it's up to the
|
||||
* developer to interact with and/or display the errors to their
|
||||
* end-users.
|
||||
*/
|
||||
class Error {
|
||||
class Error extends Object {
|
||||
|
||||
/**
|
||||
* Private message arrays
|
||||
*/
|
||||
private static $errors;
|
||||
private static $warnings;
|
||||
|
||||
private $errors = null;
|
||||
private $warnings = null;
|
||||
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* Gets an instance of the object
|
||||
*
|
||||
* Determines if the Error object has been instantiated, and if not, it will
|
||||
* go ahead and instantiate it.
|
||||
*
|
||||
* @return object An instance of the Error class
|
||||
* Constructor
|
||||
*/
|
||||
public static function instance() {
|
||||
static $object;
|
||||
|
||||
if (!is_object($object)) {
|
||||
$object = new Error();
|
||||
}
|
||||
|
||||
return $object;
|
||||
public function __construct(Config $config, Logger $logger = null) {
|
||||
$this->logger = isset($logger) ? $logger : new Logger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error message
|
||||
*
|
||||
* Takes the passed error message and loads it into the private array of
|
||||
* error messages.
|
||||
*
|
||||
* @param string Error message
|
||||
* @return boolean true
|
||||
*/
|
||||
public static function addError($message) {
|
||||
self::$errors[] = $message;
|
||||
public function addError($message) {
|
||||
$this->errors[] = $message;
|
||||
$this->logger->write('error', '[error] ' . $message);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a warning message
|
||||
*
|
||||
* Takes the passed warning message and loads it into the private array of
|
||||
* warning messages.
|
||||
*
|
||||
* @param string Warning message
|
||||
* @return boolean true
|
||||
*/
|
||||
public static function addWarning($message) {
|
||||
self::$warnings[] = $message;
|
||||
public function addWarning($message) {
|
||||
$this->warnings[] = $message;
|
||||
$this->logger->write('error', '[warning] ' . $message);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the stored errors
|
||||
*
|
||||
* Returns the errors that have been stored in the private array of error
|
||||
* messages.
|
||||
*
|
||||
* @return array Error messages indexed by the order they were stored
|
||||
* @return mixed Messages in sequential order or false
|
||||
*/
|
||||
public static function getError() {
|
||||
return self::$errors;
|
||||
public function getErrors() {
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the stored warnings
|
||||
*
|
||||
* Returns the warnings that have been stored in the private array of warning
|
||||
* messages.
|
||||
*
|
||||
* @return array Warning messages indexed by the order they were stored
|
||||
*/
|
||||
public static function getWarning() {
|
||||
return self::$warnings;
|
||||
public function getWarnings() {
|
||||
return $this->warnings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if there are any stored errors or warnings
|
||||
*
|
||||
* Checks the private error and warning arrays and returns the status.
|
||||
*
|
||||
* @return boolean Whether or not there are any errors or warnings.
|
||||
*/
|
||||
public static function isError() {
|
||||
if (is_array(self::getError()) || is_array(self::getWarning())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display errors and warnings
|
||||
*
|
||||
* If any errors or warnings are set they are echo'd out separated by XHTML
|
||||
* compliant line breaks. Also clears out the private arrays upon displaying
|
||||
* their contents.
|
||||
*
|
||||
* @return boolean Whether or not there are any errors or warnings.
|
||||
*/
|
||||
public function display() {
|
||||
if (self::isError()) {
|
||||
if (self::getError()) {
|
||||
foreach (self::getError() as $error) {
|
||||
echo "{$error}<br />";
|
||||
}
|
||||
}
|
||||
|
||||
if (self::getWarning()) {
|
||||
foreach (self::getWarning() as $error) {
|
||||
echo "{$warning}<br />";
|
||||
}
|
||||
}
|
||||
|
||||
self::$errors = self::$warnings = null;
|
||||
public function isError() {
|
||||
if (isset($this->errors, $this->warnings)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue