4 function __($obj, $ret=false) {
9 if (!extension_loaded("xdebug")) {
10 Zend_Debug
::dump($obj);
15 return ob_get_contents();
20 function redirect($url) {
21 header('Location: ' . $url);
28 private static $_instance = null;
32 public $config = null;
34 public $VARS = array();
35 public $infos = array();
39 public function __construct() {
40 $h = $_SERVER['HTTP_HOST'];
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()) {
59 if (empty($bootOrder)) {
60 $bootOrder = array('PHPConfig', 'IncludePath', 'Zend', 'Domain', 'Config', 'DB', 'Site');
62 foreach ($bootOrder as $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']);
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 . '/';
89 $url = str_replace('/'.$this->VARS
['controller'].'/', '/'.$defaultDomain.'/'.$this->VARS
['controller'].'/', $_SERVER['REQUEST_URI']);
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
);
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/');
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() {
133 require_once('Zend/Loader.php');
134 Zend_Loader
::registerAutoload();
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);
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
;
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;';
209 echo '#stackTrace {';
210 echo ' text-align:left;';
211 echo ' background:white;';
212 echo ' border:1px solid black;';
213 echo ' margin:30px;';
215 echo '.stackTraceElement {';
216 echo ' text-align:left;';
217 echo ' background:white;';
218 echo ' margin:0 0 5px 0;';
221 echo ' font-size:0.8em;';
222 echo ' color:#999999;';
223 echo ' margin:0 0 0 15px;';
226 echo '<div id="mainPanelError"><strong>';
229 echo $e->getMessage();
231 $trace = $e->getTrace();
233 function getInfo($traceElt, $param = false) {
234 $info = array_key_exists('class', $traceElt) ?
$traceElt['class'] . $traceElt['type'] : '';
235 $info .= $traceElt['function'];
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) {
248 if ($traceElt['args'] && $param) {
249 $info .= __($traceElt['args'], 'func_get_args(): ', false);
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]) : "";
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>';