test commit
[pimpmyv.git] / kernel.php
bloba6189230bbff8d74d0bdd90b99e75bc1c37f0e93
1 <?php
3 # debug function
4 function __($obj, $ret=false) {
5 if ($ret) {
6 ob_end_clean();
7 ob_start();
9 if (!extension_loaded("xdebug")) {
10 Zend_Debug::dump($obj);
11 } else {
12 var_dump($obj);
14 if ($ret) {
15 return ob_get_contents();
19 # redirect function
20 function redirect($url) {
21 header('Location: ' . $url);
22 die();
25 # kernel class
26 class Kernel {
28 private static $_instance = null;
30 public $db = null;
31 public $debug = null;
32 public $config = null;
34 public $VARS = array();
35 public $infos = array();
36 public $site = null;
37 public $i18n = null;
39 public function __construct() {
40 $h = $_SERVER['HTTP_HOST'];
42 $this->infos = array(
43 "host" => $h,
44 "dest" => 'live',
45 "httphost" => "http://".$h."/"
49 public static function getInstance() {
50 if (self::$_instance == null) {
51 self::$_instance = new Kernel();
53 return self::$_instance;
56 public function boot($bootOrder = array()) {
57 $this->initVars();
59 if (empty($bootOrder)) {
60 $bootOrder = array('PHPConfig', 'IncludePath', 'Zend', 'Domain', 'Config', 'DB', 'Site');
62 foreach ($bootOrder as $fn) {
63 $fn = 'init' . $fn;
64 $this->$fn();
67 # register infos in the view
68 self::getView()->infos = $this->infos;
71 public static function getView() {
72 return Zend_Layout::getMvcInstance()->getView();
75 private function initDomain() {
77 # determine domain or redirect
78 if (!empty($this->VARS['domain'])) {
79 $_SERVER['REQUEST_URI'] = str_replace('/'.$this->VARS['domain'], '/', $_SERVER['REQUEST_URI']);
80 } else {
81 $url = '/';
83 $this->locale = new Zend_Locale(Zend_Locale::BROWSER);
84 $defaultDomain = $this->locale->getLanguage();
86 if (empty($this->VARS['controller']) || (false === strstr($_SERVER['REQUEST_URI'], $this->VARS['controller']))) {
87 $url = $_SERVER['REQUEST_URI'] . $defaultDomain . '/';
88 } else {
89 $url = str_replace('/'.$this->VARS['controller'].'/', '/'.$defaultDomain.'/'.$this->VARS['controller'].'/', $_SERVER['REQUEST_URI']);
92 redirect($url);
95 # locale and i18n
96 $this->locale = new Zend_Locale(Maps::$langlocale[$this->VARS['domain']]);
97 $moPath = 'resources/locale/'. $this->locale->getLanguage() . '_' . $this->locale->getRegion() .'/LC_MESSAGES/common.mo';
98 $this->i18n = new Zend_Translate('gettext', $moPath, $this->locale);
100 # fill info
101 $this->infos['domain'] = $this->VARS['domain'];
102 $this->infos['httphosti18n'] = "http://".$this->infos['httphost'].$this->VARS['domain']."/";
103 $this->infos['locale'] = $this->locale->getLanguage() . '_' . $this->locale->getRegion();
107 private function initPHPConfig() {
108 # Be sure that you're showing all errors
109 ini_set('display_errors', true);
110 ini_set('error_reporting', E_ALL | E_STRICT);
112 # timezone is always a problem when not set
113 ini_set('date.timezone', 'Europe/Paris');
117 private function initIncludePath() {
118 $includePath = array('.', './site/models/');
120 $libDir = './lib/';
121 foreach(new DirectoryIterator($libDir) as $value) {
122 $path = $libDir . $value;
123 if (is_dir($path) && (!in_array((string)$value,array('.','..')))) {
124 $includePath[] = $path;
128 set_include_path(get_include_path() . PATH_SEPARATOR . implode(PATH_SEPARATOR, $includePath));
131 private function initZend() {
132 # load the loader
133 require_once('Zend/Loader.php');
134 Zend_Loader::registerAutoload();
136 # session start
137 Zend_Session::start();
139 # controller configuration
140 $controller = Zend_Controller_Front::getInstance();
141 $controller->setControllerDirectory('./site/controllers/');
142 $controller->setDefaultControllerName('Index');
143 $controller->throwExceptions(true);
145 # views param
146 $view = new Zend_View();
147 $view->setEncoding('UTF-8');
148 $view->setScriptPath('./site/views/Pages/');
149 $view->setHelperPath('./site/views/Helpers/');
151 # modify the viewRenderer default behaviour
152 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
153 $viewRenderer->setView($view);
154 $viewRenderer->setViewSuffix('tpl');
156 # use the laout object
157 $layout = Zend_Layout::startMvc();
158 $layout->setLayout('default');
159 $layout->setView($view);
160 $layout->setLayoutPath('./site/views/Layouts/');
161 $layout->setViewSuffix('tpl');
162 # todo follow if Zend_Layout use inflector from the view renderer in next relases
165 private function initVars() {
166 $this->VARS = array();
167 if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
168 $this->VARS['_ajax'] = true;
170 if (isset($_SESSION)) {
171 $this->VARS = array_merge($this->VARS, $_SESSION);
173 $this->VARS = array_merge($this->VARS, $_COOKIE, $_GET, $_POST);
176 private function initConfig() {
177 $this->config = new Zend_Config_Ini('config.ini', $this->infos['dest']);
179 $this->infos['contactmail'] = $this->config->contactmail;
182 private function initDB() {
183 $dbtype = $this->config->db->type;
184 $params = array(
185 'host' => $this->config->db->host,
186 'username' => $this->config->db->user,
187 'password' => $this->config->db->password,
188 'dbname' => $this->config->db->name
191 $this->db = Zend_Db::factory($dbtype, $params);
194 private function initSite() {
195 $this->site = new Site();
198 public static function displayError(Exception $e) {
199 error_log($e->getMessage());
201 echo '<!-- ERROR -->';
202 echo '<style type="text/css">';
203 echo '#mainPanelError {';
204 echo ' text-align:left;';
205 echo ' padding:10px;';
206 echo ' background:#ffea50;';
207 echo ' border:4px double red;';
208 echo '}';
209 echo '#stackTrace {';
210 echo ' text-align:left;';
211 echo ' background:white;';
212 echo ' border:1px solid black;';
213 echo ' margin:30px;';
214 echo '}';
215 echo '.stackTraceElement {';
216 echo ' text-align:left;';
217 echo ' background:white;';
218 echo ' margin:0 0 5px 0;';
219 echo '}';
220 echo '.fileInfo {';
221 echo ' font-size:0.8em;';
222 echo ' color:#999999;';
223 echo ' margin:0 0 0 15px;';
224 echo '}';
225 echo '</style>';
226 echo '<div id="mainPanelError"><strong>';
227 echo get_class($e);
228 echo '<br />';
229 echo $e->getMessage();
230 echo '</strong>';
231 $trace = $e->getTrace();
232 if ($trace) {
233 function getInfo($traceElt, $param = false) {
234 $info = array_key_exists('class', $traceElt) ? $traceElt['class'] . $traceElt['type'] : '';
235 $info .= $traceElt['function'];
236 $info .= '(';
237 if ($traceElt['args']) {
238 $count = count($traceElt['args']);
239 for ($i = 0; $i < $count; $i++) {
240 $arg = $traceElt['args'][$i];
241 $info .= is_object($arg) ? get_class($arg) : gettype($arg);
242 if ($i < $count - 1) {
243 $info .= ', ';
247 $info .= ')';
248 if ($traceElt['args'] && $param) {
249 $info .= __($traceElt['args'], 'func_get_args(): ', false);
251 return $info;
253 echo '<br /><em>in '.getInfo($trace[0], true).'</em>';
254 echo '<div id="stackTrace">';
255 echo '<b>StackTrace :</b><br /><br />';
256 foreach ($trace as $k => $line) {
257 echo '<div class="stackTraceElement">';
258 $callInfo = ($k < count($trace) - 1) ? getInfo($trace[$k + 1]) : "";
259 echo $callInfo;
260 if (array_key_exists('file', $line)) {
261 echo ' (' .basename($line['file']) .':'.$line['line'].')<br />';
262 echo '<span class="fileInfo">in file '.$line['file'].'</span>';
264 echo '</div>';
266 echo '<div>';
268 echo '</div>';