summaryrefslogtreecommitdiff
path: root/lib/FrontController.php
blob: e424c6714f74e7a4fd16c07a385d05eea14210d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?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 noyau du framework, elle lance l'application
 * Elle est appelée en général dans le fichier index.php à la racine du serveur
 */
class FrontController {
	protected $dispatcher;
	protected $router;
	
	/**
	 * Constructeur
	 * Initialise le router et le dispatcher
	 */
	public function __construct () {
		$this->loadLib ();
		
		try {
			Configuration::init ();
			Request::init ();
			
			$this->router = new Router ();
			$this->router->init ();
		} catch (RouteNotFoundException $e) {
			Log::record ($e->getMessage (), Log::ERROR);
			Error::error (
				404,
				array ('error' => array ($e->getMessage ()))
			);
		} catch (MinzException $e) {
			Log::record ($e->getMessage (), Log::ERROR);
			$this->killApp ();
		}
		
		$this->dispatcher = Dispatcher::getInstance ($this->router);
	}
	
	/**
	 * Inclue les fichiers de la librairie
	 */
	private function loadLib () {
		require ('ActionController.php');
		require ('Cache.php');
		require ('Configuration.php');
		require ('Dispatcher.php');
		require ('Error.php');
		require ('Helper.php');
		require ('Log.php');
		require ('Model.php');
		require ('Paginator.php');
		require ('Request.php');
		require ('Response.php');
		require ('Router.php');
		require ('Session.php');
		require ('Translate.php');
		require ('Url.php');
		require ('View.php');
		
		require ('dao/Model_pdo.php');
		require ('dao/Model_txt.php');
		require ('dao/Model_array.php');
		
		require ('exceptions/MinzException.php');
	}
	
	/**
	 * Démarre l'application (lance le dispatcher et renvoie la réponse
	 */
	public function run () {
		try {
			$this->dispatcher->run ();
			Response::send ();
		} catch (MinzException $e) {
			Log::record ($e->getMessage (), Log::ERROR);
			$this->killApp ();
		}
	}
	
	/**
	* Permet d'arrêter le programme en urgence
	*/
	private function killApp () {
		exit ('### Application problem ###'."\n".
		      'See logs files');
	}
}