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