I am currently developing a Content Management System, and I have just completed the Error Handling system, which handles errors according to the user's settings.
I want to know every single thing I could improve to make it more flexible, robust, light weight and fast processing. If there are better ways of doing things, I am ready to re-do stuff. I just want comments on ErrorHandling
System, in this global.php you can see how I instantiate them. It would also be helpful if you can suggest renames to variables, etc to make more sense. This project is going to be up on GitHub soon. Please be as strict as possible, and point out even a slight flaw in it.
Global.php
<?php
/**
* @author Hassan Althaf
* @link www.HassanTech.org
* @package IDK CMS
*/
require_once("config.php");
if($configuration['errors']['show_errors'] == 0) {
error_reporting(0);
}
require_once("ErrorHandling/interface.ErrorHandlerMapper.php");
require_once("ErrorHandling/class.ErrorHandlerController.php");
require_once("ErrorHandling/class.ErrorHandlerLocalMapper.php");
require_once("ErrorHandling/class.ErrorHandlerDatabaseMapper.php");
$database = new mysqli(
$configuration['database']['host'],
$configuration['database']['username'],
$configuration['database']['password'],
$configuration['database']['database']
);
if($configuration['errors']['error_log_method'] == 'local') {
$errorHandlerMapper = new CMS\Core\ErrorHandling\ErrorHandlerLocalMapper();
} elseif($configuration['errors']['error_log_method'] == 'database') {
$errorHandlerMapper = new CMS\Core\ErrorHandling\ErrorHandlerDatabaseMapper($database);
}
$errorHandlerController = new CMS\Core\ErrorHandling\ErrorHandlerController($errorHandlerMapper);
if($configuration['errors']['enable_error_logs'] == 1) {
if($database->connect_errno) {
echo $database->connect_error;
}
}
I have 3 classes and 1 interface.
class.ErrorHandlerController.php:
<?php
namespace CMS\Core\ErrorHandling;
class ErrorHandlerController {
private $errorHandlerMapper;
public function __construct(ErrorHandlerMapper $errorHandlerMapper) {
$this->errorHandlerMapper = $errorHandlerMapper;
}
public function handleErrorLogging($error) {
return $this->errorHandlerMapper->log($error);
}
public function handleReturningErrorList() {
return $this->errorHandlerMapper->returnErrorList();
}
public function handleErrorListTruncating() {
return $this->errorHandlerMapper->truncateErrorList();
}
}
class.ErrorHandlerDatabaseMapper.php:
<?php
namespace CMS\Core\ErrorHandling;
use mysqli;
class ErrorHandlerDatabaseMapper implements ErrorHandlerMapper {
private $database;
public function __construct(mysqli $database) {
$this->database = $database;
}
public function log($error) {
$query = $this->database->prepare("INSERT INTO errors VALUES('', ?);");
$query->bind_param('s', $error);
$query->execute();
$query->close();
return true;
}
public function returnErrorList() {
$query = $this->database->prepare("SELECT error FROM errors");
$query->execute();
$query->bind_result($error);
while($query->fetch()) {
$errors[] = $error;
}
$query->close();
return $errors;
}
public function truncateErrorList() {
$query = $this->database->prepare("TRUNCATE TABLE errors");
$query->execute();
$query->close();
return true;
}
}
class.ErrorHandlerLocalMapper.php:
<?php
namespace CMS\Core\ErrorHandling;
class ErrorHandlerLocalMapper implements ErrorHandlerMapper {
public function log($error) {
return file_put_contents('errorLog.txt', file_get_contents('errorLog.txt') . $error . ";");
}
public function returnErrorList() {
return explode(';', file_get_contents('errorLog.txt'));
}
public function truncateErrorList() {
return file_put_contents('errorLog.txt', '');
}
}
interface.ErrorHandlerMapper.php:
<?php
namespace CMS\Core\ErrorHandling;
interface ErrorHandlerMapper {
public function log($error);
public function returnErrorList();
public function truncateErrorList();
}