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