aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/FrontController.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-10-09 15:53:10 +0200
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-10-09 15:53:10 +0200
commitf97d4b3b6cca4a55636bbd50158f3c57666b0f08 (patch)
tree3ca9dd42155228292f0842d65b9b6d90e9140639 /lib/Minz/FrontController.php
parente51ceb6812e3736aa9b9ce1f2d5181f5b4b6aaa3 (diff)
parent444b1552364b39761c3278c7da5152fd3998f216 (diff)
Merge branch 'master' into hotfixes
Diffstat (limited to 'lib/Minz/FrontController.php')
-rw-r--r--lib/Minz/FrontController.php117
1 files changed, 117 insertions, 0 deletions
diff --git a/lib/Minz/FrontController.php b/lib/Minz/FrontController.php
new file mode 100644
index 000000000..f13882801
--- /dev/null
+++ b/lib/Minz/FrontController.php
@@ -0,0 +1,117 @@
+<?php
+# ***** BEGIN LICENSE BLOCK *****
+# MINZ - a free PHP Framework like Zend Framework
+# Copyright (C) 2011 Marien Fressinaud
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# ***** END LICENSE BLOCK *****
+
+/**
+ * La classe FrontController est le Dispatcher du framework, elle lance l'application
+ * Elle est appelée en général dans le fichier index.php à la racine du serveur
+ */
+class Minz_FrontController {
+ protected $dispatcher;
+
+ /**
+ * Constructeur
+ * Initialise le dispatcher, met à jour la Request
+ */
+ public function __construct () {
+ if (LOG_PATH === false) {
+ $this->killApp ('Path not found: LOG_PATH');
+ }
+
+ try {
+ Minz_Configuration::init ();
+
+ Minz_Request::init ();
+
+ $url = $this->buildUrl();
+ $url['params'] = array_merge (
+ $url['params'],
+ Minz_Request::fetchPOST ()
+ );
+ Minz_Request::forward ($url);
+ } catch (Minz_Exception $e) {
+ Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
+ $this->killApp ($e->getMessage ());
+ }
+
+ $this->dispatcher = Minz_Dispatcher::getInstance();
+ }
+
+ /**
+ * Retourne un tableau représentant l'url passée par la barre d'adresses
+ * @return tableau représentant l'url
+ */
+ private function buildUrl() {
+ $url = array ();
+
+ $url['c'] = Minz_Request::fetchGET (
+ 'c',
+ Minz_Request::defaultControllerName ()
+ );
+ $url['a'] = Minz_Request::fetchGET (
+ 'a',
+ Minz_Request::defaultActionName ()
+ );
+ $url['params'] = Minz_Request::fetchGET ();
+
+ // post-traitement
+ unset ($url['params']['c']);
+ unset ($url['params']['a']);
+
+ return $url;
+ }
+
+ /**
+ * Démarre l'application (lance le dispatcher et renvoie la réponse)
+ */
+ public function run () {
+ try {
+ $this->dispatcher->run();
+ } catch (Minz_Exception $e) {
+ try {
+ Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
+ } catch (Minz_PermissionDeniedException $e) {
+ $this->killApp ($e->getMessage ());
+ }
+
+ if ($e instanceof Minz_FileNotExistException ||
+ $e instanceof Minz_ControllerNotExistException ||
+ $e instanceof Minz_ControllerNotActionControllerException ||
+ $e instanceof Minz_ActionException) {
+ Minz_Error::error (
+ 404,
+ array ('error' => array ($e->getMessage ())),
+ true
+ );
+ } else {
+ $this->killApp ();
+ }
+ }
+ }
+
+ /**
+ * Permet d'arrêter le programme en urgence
+ */
+ private function killApp ($txt = '') {
+ if ($txt == '') {
+ $txt = 'See logs files';
+ }
+ exit ('### Application problem ###<br />'."\n".$txt);
+ }
+}