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 Zend_Registry
::get('view')->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']);
79 $this->locale
= new Zend_Locale(Zend_Locale
::BROWSER
);
80 $defaultDomain = $this->locale
->toString();
82 if (empty($this->VARS
['controller']) ||
(false === strstr($_SERVER['REQUEST_URI'], $this->VARS
['controller']))) {
83 $url = $_SERVER['REQUEST_URI'] . $defaultDomain . '/';
85 $url = str_replace('/'.$this->VARS
['controller'].'/', '/'.$defaultDomain.'/'.$this->VARS
['controller'].'/', $_SERVER['REQUEST_URI']);
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
);
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/');
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() {
129 require_once('Zend/Loader.php');
130 Zend_Loader
::registerAutoload();
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);
142 $view = new Zend_View();
143 $view->setEncoding('UTF-8');
144 $view->setScriptPath('./site/views/Pages/');
145 $view->setHelperPath('./site/views/Helpers/');
146 Zend_Registry
::set('view', $view);
148 # modify the viewRenderer default behaviour
149 $viewRenderer = Zend_Controller_Action_HelperBroker
::getStaticHelper('viewRenderer');
150 $viewRenderer->setView($view);
151 $viewRenderer->setViewSuffix('tpl');
153 # use the laout object
154 $layout = Zend_Layout
::startMvc();
155 $layout->setLayout('default');
156 $layout->setView($view);
157 $layout->setLayoutPath('./site/views/Layouts/');
158 $layout->setViewSuffix('tpl');
159 # todo follow if Zend_Layout use inflector from the view renderer in next relases
162 private function initVars() {
163 $this->VARS
= array();
164 if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
165 $this->VARS
['_ajax'] = true;
167 if (isset($_SESSION)) {
168 $this->VARS
= array_merge($this->VARS
, $_SESSION);
170 $this->VARS
= array_merge($this->VARS
, $_COOKIE, $_GET, $_POST);
173 private function initConfig() {
174 $this->config
= new Zend_Config_Ini('config.ini', $this->infos
['dest']);
176 $this->infos
['contactmail'] = $this->config
->contactmail
;
179 private function initDB() {
180 $dbtype = $this->config
->db
->type
;
182 'host' => $this->config
->db
->host
,
183 'username' => $this->config
->db
->user
,
184 'password' => $this->config
->db
->password
,
185 'dbname' => $this->config
->db
->name
188 $this->db
= Zend_Db
::factory($dbtype, $params);
191 private function initSite() {
192 $this->site
= new Site();
195 public static function displayError(Exception
$e) {
196 error_log($e->getMessage());
198 echo '<!-- ERROR -->';
199 echo '<style type="text/css">';
200 echo '#mainPanelError {';
201 echo ' text-align:left;';
202 echo ' padding:10px;';
203 echo ' background:#ffea50;';
204 echo ' border:4px double red;';
206 echo '#stackTrace {';
207 echo ' text-align:left;';
208 echo ' background:white;';
209 echo ' border:1px solid black;';
210 echo ' margin:30px;';
212 echo '.stackTraceElement {';
213 echo ' text-align:left;';
214 echo ' background:white;';
215 echo ' margin:0 0 5px 0;';
218 echo ' font-size:0.8em;';
219 echo ' color:#999999;';
220 echo ' margin:0 0 0 15px;';
223 echo '<div id="mainPanelError"><strong>';
226 echo $e->getMessage();
228 $trace = $e->getTrace();
230 function getInfo($traceElt, $param = false) {
231 $info = array_key_exists('class', $traceElt) ?
$traceElt['class'] . $traceElt['type'] : '';
232 $info .= $traceElt['function'];
234 if ($traceElt['args']) {
235 $count = count($traceElt['args']);
236 for ($i = 0; $i < $count; $i++
) {
237 $arg = $traceElt['args'][$i];
238 $info .= is_object($arg) ?
get_class($arg) : gettype($arg);
239 if ($i < $count - 1) {
245 if ($traceElt['args'] && $param) {
246 $info .= __($traceElt['args'], 'func_get_args(): ', false);
250 echo '<br /><em>in '.getInfo($trace[0], true).'</em>';
251 echo '<div id="stackTrace">';
252 echo '<b>StackTrace :</b><br /><br />';
253 foreach ($trace as $k => $line) {
254 echo '<div class="stackTraceElement">';
255 $callInfo = ($k < count($trace) - 1) ?
getInfo($trace[$k +
1]) : "";
257 if (array_key_exists('file', $line)) {
258 echo ' (' .basename($line['file']) .':'.$line['line'].')<br />';
259 echo '<span class="fileInfo">in file '.$line['file'].'</span>';