remove registry, css linked to layout, rewrites ok for resources, test controller...
[pimpmyv.git] / kernel.php
blob97ff393d97e70a69679ad1026bbfa7bac27acb6e
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 Zend_Layout::getMvcInstance()->getView()->infos = $this->infos;
71 private function initDomain() {
73 # determine domain or redirect
74 if (!empty($this->VARS['domain'])) {
75 $_SERVER['REQUEST_URI'] = str_replace('/'.$this->VARS['domain'], '/', $_SERVER['REQUEST_URI']);
76 } else {
77 $url = '/';
79 $this->locale = new Zend_Locale(Zend_Locale::BROWSER);
80 $defaultDomain = $this->locale->getLanguage();
82 if (empty($this->VARS['controller']) || (false === strstr($_SERVER['REQUEST_URI'], $this->VARS['controller']))) {
83 $url = $_SERVER['REQUEST_URI'] . $defaultDomain . '/';
84 } else {
85 $url = str_replace('/'.$this->VARS['controller'].'/', '/'.$defaultDomain.'/'.$this->VARS['controller'].'/', $_SERVER['REQUEST_URI']);
88 redirect($url);
91 # locale and i18n
92 $this->locale = new Zend_Locale(Maps::$langlocale[$this->VARS['domain']]);
93 $moPath = 'resources/locale/'. $this->locale->getLanguage() . '_' . $this->locale->getRegion() .'/LC_MESSAGES/common.mo';
94 $this->i18n = new Zend_Translate('gettext', $moPath, $this->locale);
96 # fill info
97 $this->infos['domain'] = $this->VARS['domain'];
98 $this->infos['httphosti18n'] = "http://".$this->infos['httphost'].$this->VARS['domain']."/";
99 $this->infos['locale'] = $this->locale->getLanguage() . '_' . $this->locale->getRegion();
103 private function initPHPConfig() {
104 # Be sure that you're showing all errors
105 ini_set('display_errors', true);
106 ini_set('error_reporting', E_ALL | E_STRICT);
108 # timezone is always a problem when not set
109 ini_set('date.timezone', 'Europe/Paris');
113 private function initIncludePath() {
114 $includePath = array('.', './site/models/');
116 $libDir = './lib/';
117 foreach(new DirectoryIterator($libDir) as $value) {
118 $path = $libDir . $value;
119 if (is_dir($path) && (!in_array((string)$value,array('.','..')))) {
120 $includePath[] = $path;
124 set_include_path(get_include_path() . PATH_SEPARATOR . implode(PATH_SEPARATOR, $includePath));
127 private function initZend() {
128 # load the loader
129 require_once('Zend/Loader.php');
130 Zend_Loader::registerAutoload();
132 # session start
133 Zend_Session::start();
135 # controller configuration
136 $controller = Zend_Controller_Front::getInstance();
137 $controller->setControllerDirectory('./site/controllers/');
138 $controller->setDefaultControllerName('Index');
139 $controller->throwExceptions(true);
141 # views param
142 $view = new Zend_View();
143 $view->setEncoding('UTF-8');
144 $view->setScriptPath('./site/views/Pages/');
145 $view->setHelperPath('./site/views/Helpers/');
147 # modify the viewRenderer default behaviour
148 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
149 $viewRenderer->setView($view);
150 $viewRenderer->setViewSuffix('tpl');
152 # use the laout object
153 $layout = Zend_Layout::startMvc();
154 $layout->setLayout('default');
155 $layout->setView($view);
156 $layout->setLayoutPath('./site/views/Layouts/');
157 $layout->setViewSuffix('tpl');
158 # todo follow if Zend_Layout use inflector from the view renderer in next relases
161 private function initVars() {
162 $this->VARS = array();
163 if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
164 $this->VARS['_ajax'] = true;
166 if (isset($_SESSION)) {
167 $this->VARS = array_merge($this->VARS, $_SESSION);
169 $this->VARS = array_merge($this->VARS, $_COOKIE, $_GET, $_POST);
172 private function initConfig() {
173 $this->config = new Zend_Config_Ini('config.ini', $this->infos['dest']);
175 $this->infos['contactmail'] = $this->config->contactmail;
178 private function initDB() {
179 $dbtype = $this->config->db->type;
180 $params = array(
181 'host' => $this->config->db->host,
182 'username' => $this->config->db->user,
183 'password' => $this->config->db->password,
184 'dbname' => $this->config->db->name
187 $this->db = Zend_Db::factory($dbtype, $params);
190 private function initSite() {
191 $this->site = new Site();
194 public static function displayError(Exception $e) {
195 error_log($e->getMessage());
197 echo '<!-- ERROR -->';
198 echo '<style type="text/css">';
199 echo '#mainPanelError {';
200 echo ' text-align:left;';
201 echo ' padding:10px;';
202 echo ' background:#ffea50;';
203 echo ' border:4px double red;';
204 echo '}';
205 echo '#stackTrace {';
206 echo ' text-align:left;';
207 echo ' background:white;';
208 echo ' border:1px solid black;';
209 echo ' margin:30px;';
210 echo '}';
211 echo '.stackTraceElement {';
212 echo ' text-align:left;';
213 echo ' background:white;';
214 echo ' margin:0 0 5px 0;';
215 echo '}';
216 echo '.fileInfo {';
217 echo ' font-size:0.8em;';
218 echo ' color:#999999;';
219 echo ' margin:0 0 0 15px;';
220 echo '}';
221 echo '</style>';
222 echo '<div id="mainPanelError"><strong>';
223 echo get_class($e);
224 echo '<br />';
225 echo $e->getMessage();
226 echo '</strong>';
227 $trace = $e->getTrace();
228 if ($trace) {
229 function getInfo($traceElt, $param = false) {
230 $info = array_key_exists('class', $traceElt) ? $traceElt['class'] . $traceElt['type'] : '';
231 $info .= $traceElt['function'];
232 $info .= '(';
233 if ($traceElt['args']) {
234 $count = count($traceElt['args']);
235 for ($i = 0; $i < $count; $i++) {
236 $arg = $traceElt['args'][$i];
237 $info .= is_object($arg) ? get_class($arg) : gettype($arg);
238 if ($i < $count - 1) {
239 $info .= ', ';
243 $info .= ')';
244 if ($traceElt['args'] && $param) {
245 $info .= __($traceElt['args'], 'func_get_args(): ', false);
247 return $info;
249 echo '<br /><em>in '.getInfo($trace[0], true).'</em>';
250 echo '<div id="stackTrace">';
251 echo '<b>StackTrace :</b><br /><br />';
252 foreach ($trace as $k => $line) {
253 echo '<div class="stackTraceElement">';
254 $callInfo = ($k < count($trace) - 1) ? getInfo($trace[$k + 1]) : "";
255 echo $callInfo;
256 if (array_key_exists('file', $line)) {
257 echo ' (' .basename($line['file']) .':'.$line['line'].')<br />';
258 echo '<span class="fileInfo">in file '.$line['file'].'</span>';
260 echo '</div>';
262 echo '<div>';
264 echo '</div>';