summaryrefslogtreecommitdiff
path: root/lib/Minz/Error.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-01-29 21:26:19 +0100
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-01-29 21:26:19 +0100
commit165eb57459a152b3cc6aa3fd15ca990c3d908829 (patch)
tree640ddacbc6eabc985646dd65b0578f369ea92321 /lib/Minz/Error.php
parent75096e6a39fe5d34d3951991f296f616e62a9fd8 (diff)
parentc053825ff8f9792e692c101585481129b006937b (diff)
Sortie de la version 0.70.7.0
Diffstat (limited to 'lib/Minz/Error.php')
-rw-r--r--lib/Minz/Error.php94
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/Minz/Error.php b/lib/Minz/Error.php
new file mode 100644
index 000000000..337ab6c0a
--- /dev/null
+++ b/lib/Minz/Error.php
@@ -0,0 +1,94 @@
+<?php
+/**
+ * MINZ - Copyright 2011 Marien Fressinaud
+ * Sous licence AGPL3 <http://www.gnu.org/licenses/>
+*/
+
+/**
+ * La classe Error permet de lancer des erreurs HTTP
+ */
+class Minz_Error {
+ public function __construct () { }
+
+ /**
+ * Permet de lancer une erreur
+ * @param $code le type de l'erreur, par défaut 404 (page not found)
+ * @param $logs logs d'erreurs découpés de la forme
+ * > $logs['error']
+ * > $logs['warning']
+ * > $logs['notice']
+ * @param $redirect indique s'il faut forcer la redirection (les logs ne seront pas transmis)
+ */
+ public static function error ($code = 404, $logs = array (), $redirect = false) {
+ $logs = self::processLogs ($logs);
+ $error_filename = APP_PATH . '/Controllers/errorController.php';
+
+ if (file_exists ($error_filename)) {
+ $params = array (
+ 'code' => $code,
+ 'logs' => $logs
+ );
+
+ Minz_Response::setHeader ($code);
+ if ($redirect) {
+ Minz_Request::forward (array (
+ 'c' => 'error'
+ ), true);
+ } else {
+ Minz_Request::forward (array (
+ 'c' => 'error',
+ 'params' => $params
+ ), false);
+ }
+ } else {
+ $text = '<h1>An error occured</h1>'."\n";
+
+ if (!empty ($logs)) {
+ $text .= '<ul>'."\n";
+ foreach ($logs as $log) {
+ $text .= '<li>' . $log . '</li>'."\n";
+ }
+ $text .= '</ul>'."\n";
+ }
+
+ Minz_Response::setHeader ($code);
+ Minz_Response::setBody ($text);
+ Minz_Response::send ();
+ exit ();
+ }
+ }
+
+ /**
+ * Permet de retourner les logs de façon à n'avoir que
+ * ceux que l'on veut réellement
+ * @param $logs les logs rangés par catégories (error, warning, notice)
+ * @return la liste des logs, sans catégorie,
+ * > en fonction de l'environment
+ */
+ private static function processLogs ($logs) {
+ $env = Minz_Configuration::environment ();
+ $logs_ok = array ();
+ $error = array ();
+ $warning = array ();
+ $notice = array ();
+
+ if (isset ($logs['error'])) {
+ $error = $logs['error'];
+ }
+ if (isset ($logs['warning'])) {
+ $warning = $logs['warning'];
+ }
+ if (isset ($logs['notice'])) {
+ $notice = $logs['notice'];
+ }
+
+ if ($env == Minz_Configuration::PRODUCTION) {
+ $logs_ok = $error;
+ }
+ if ($env == Minz_Configuration::DEVELOPMENT) {
+ $logs_ok = array_merge ($error, $warning, $notice);
+ }
+
+ return $logs_ok;
+ }
+}