menu introduction
[pimpmyv.git] / .svn / text-base / kernel.php.svn-base
blob6e86a1ecf40b7cf8c655b98674590b9793ea56d8
1 <?php
3 # debug function
4 function __($obj, $ret=false) {
5         if ($ret) {
6                 ob_end_clean();
7                 ob_start();
8         }
9     if (!extension_loaded("xdebug")) {
10         Zend_Debug::dump($obj);
11     } else {
12         var_dump($obj);
13     }
14     if ($ret) {
15         return ob_get_contents();
16     }
19 class Kernel {
21         public $db = null;
22         public $debug = null;
23         public $config = null;
24         
25         public $VARS = array();
27         public function __constructor() {
28                 # nothing to do for the moment
29         }
30         
31         public function boot($bootorder = array()) {
32                 $this->initVars();
33                 
34                 if (empty($bootorder)) {
35                         $bootOrder = array('Domain', 'PHPConfig', 'IncludePath', 'Zend', 'Config', 'DB');
36                 }
37                 foreach ($bootOrder as $fn) {
38                         $fn = 'init' . $fn;
39                         $this->$fn();
40                 }
41         }
42         
43         private function initDomain() {
44                 if (!empty($this->VARS['domain'])) {
45                         $_SERVER['REQUEST_URI'] = str_replace('/'.$this->VARS['domain'], '/', $_SERVER['REQUEST_URI']);
46                 } else {
47                         $url = '/';
48                         $defaultDomain = 'en';
50                         if (empty($this->VARS['controller']) || (false === strstr($_SERVER['REQUEST_URI'], $this->VARS['controller']))) {
51                                 $url = $_SERVER['REQUEST_URI'] . $defaultDomain . '/';
52                         } else {
53                                 $url = str_replace('/'.$this->VARS['controller'].'/', '/'.$defaultDomain.'/'.$this->VARS['controller'].'/', $_SERVER['REQUEST_URI']);
54                         }
55                         header('Location: ' . $url);
56                         die();
57                 }
58         }
59         
60         private function initPHPConfig() {
61                 # Be sure that you're showing all errors
62                 ini_set('display_errors', true);
63                 ini_set('error_reporting', E_ALL | E_STRICT);
64                 
65                 # timezone is always a problem when not set
66                 ini_set('date.timezone', 'Europe/Paris');
67                 
68         }
69         
70         private function initIncludePath() {
71             $includePath = array('.', './site/models/');
72             
73             $libDir = './lib/';
74             foreach(new DirectoryIterator($libDir) as $value) {
75                 $path = $libDir . $value;
76                 if (is_dir($path) && (!in_array((string)$value,array('.','..')))) {
77                     $includePath[] = $path;
78                 }
79             }
81             set_include_path(get_include_path() . PATH_SEPARATOR . implode(PATH_SEPARATOR, $includePath));
82         }
83         
84         private function initZend() {
85                 # load the loader
86                 require_once('Zend/Loader.php');
87                 Zend_Loader::registerAutoload();
89                 # session start
90                 Zend_Session::start();
92                 # controller configuration
93                 $controller = Zend_Controller_Front::getInstance();
94                 $controller->setControllerDirectory('./site/controllers/');
95                 $controller->setDefaultControllerName('Index');
96                 $controller->throwExceptions(true);
97          
98                 # views param
99                 $view = new Zend_View(array('encoding' => 'UTF-8'));
100                 $view->setScriptPath('./site/views/Pages/');
101                 $view->setHelperPath('./site/views/Helpers/');
103                 # modify the viewRenderer default behaviour
104                 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
105                 $viewRenderer->setView($view);
106                 $viewRenderer->setViewSuffix('php');
107         }
108         
109         private function initVars() {
110                 $this->VARS = array();
111                 if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
112                         $this->VARS['_ajax'] = true;
113         }
114                 if (isset($_SESSION)) {
115                         $this->VARS = array_merge($this->VARS, $_SESSION);
116                 } 
117                 $this->VARS = array_merge($this->VARS, $_COOKIE, $_GET, $_POST);
118         }
119         
120         private function initConfig() {
121                 $this->config = new Zend_Config_Ini('config.ini', 'live');
122         }
123         
124         private function initDB() {
125                 $dbtype = $this->config->db->type;
126                 $params = array(
127                         'host' => $this->config->db->host,
128                         'username' => $this->config->db->user,
129                         'password' => $this->config->db->password,
130                         'dbname' => $this->config->db->name
131                 );
132                 
133                 $this->db = Zend_Db::factory($dbtype, $params);
134         }
136         public static function displayError(Exception $e) {
137                 echo '<!-- ERROR -->';
138                 echo '<style type="text/css">';
139                 echo '#mainPanelError {';
140                 echo '  text-align:left;';
141                 echo '  padding:10px;';
142                 echo '  background:#ffea50;';
143                 echo '  border:4px double red;';
144                 echo '}';
145                 echo '#stackTrace {';
146                 echo '  text-align:left;';
147                 echo '  background:white;';
148                 echo '  border:1px solid black;';
149                 echo '  margin:30px;';
150                 echo '}';
151                 echo '.stackTraceElement {';
152                 echo '  text-align:left;';
153                 echo '  background:white;';
154                 echo '  margin:0 0 5px 0;';
155                 echo '}';
156                 echo '.fileInfo {';
157                 echo '  font-size:0.8em;';
158                 echo '  color:#999999;';
159                 echo '  margin:0 0 0 15px;';
160                 echo '}';
161                 echo '</style>';
162                 echo '<div id="mainPanelError"><strong>';
163                 echo get_class($e);
164                 echo '<br />';
165                 echo $e->getMessage();
166                 echo '</strong>';
167                 $trace = $e->getTrace();
168                 if ($trace) {
169                         function getInfo($traceElt, $param = false) {
170                                 $info = array_key_exists('class', $traceElt) ? $traceElt['class'] . $traceElt['type'] : '';
171                                 $info .= $traceElt['function'];
172                                 $info .= '(';
173                                 if ($traceElt['args']) {
174                                         $count = count($traceElt['args']);
175                                         for ($i = 0; $i < $count; $i++) {
176                                                 $arg = $traceElt['args'][$i];
177                                                 $info .= is_object($arg) ? get_class($arg) : gettype($arg);                                             
178                                                 if ($i < $count - 1) {
179                                                         $info .= ', ';
180                                                 }
181                                         }
182                                 }
183                                 $info .= ')';
184                                 if ($traceElt['args'] && $param) {
185                                         $info .= __($traceElt['args'], 'func_get_args(): ', false);
186                                 } 
187                                 return $info;
188                         }
189                         echo '<br /><em>in '.getInfo($trace[0], true).'</em>';
190                         echo '<div id="stackTrace">';
191                         echo '<b>StackTrace :</b><br /><br />';
192                         foreach ($trace as $k => $line) {
193                                 echo '<div class="stackTraceElement">';
194                                 $callInfo = ($k < count($trace) - 1) ? getInfo($trace[$k + 1]) : "";
195                                 echo $callInfo;
196                                 if (array_key_exists('file', $line)) {
197                                         echo ' (' .basename($line['file']) .':'.$line['line'].')<br />';
198                                         echo '<span class="fileInfo">in file '.$line['file'].'</span>';
199                                 }
200                                 echo '</div>';
201                         }
202                         echo '<div>';
203                 }
204                 echo '</div>';
205         }
206