root/trunk/lib/SGL/FrontController.php

Revision 3942, 12.8 kB (checked in by demian, 1 month ago)

removed lib cache feature

Line 
1 <?php
2 /* Reminder: always indent with 4 spaces (no tabs). */
3 // +---------------------------------------------------------------------------+
4 // | Copyright (c) 2008, Demian Turner                                         |
5 // | All rights reserved.                                                      |
6 // |                                                                           |
7 // | Redistribution and use in source and binary forms, with or without        |
8 // | modification, are permitted provided that the following conditions        |
9 // | are met:                                                                  |
10 // |                                                                           |
11 // | o Redistributions of source code must retain the above copyright          |
12 // |   notice, this list of conditions and the following disclaimer.           |
13 // | o Redistributions in binary form must reproduce the above copyright       |
14 // |   notice, this list of conditions and the following disclaimer in the     |
15 // |   documentation and/or other materials provided with the distribution.    |
16 // | o The names of the authors may not be used to endorse or promote          |
17 // |   products derived from this software without specific prior written      |
18 // |   permission.                                                             |
19 // |                                                                           |
20 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       |
21 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         |
22 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR     |
23 // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      |
24 // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     |
25 // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT          |
26 // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     |
27 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
28 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
29 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE     |
30 // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.      |
31 // |                                                                           |
32 // +---------------------------------------------------------------------------+
33 // | Seagull 0.9                                                               |
34 // +---------------------------------------------------------------------------+
35 // | FrontController.php                                                       |
36 // +---------------------------------------------------------------------------+
37 // | Author:   Demian Turner <demian@phpkitchen.com>                           |
38 // +---------------------------------------------------------------------------+
39 // $Id: FrontController.php,v 1.49 2005/06/23 19:15:25 demian Exp $
40
41 require_once dirname(__FILE__)  . '/../SGL.php';
42
43 /**
44  * Application controller.
45  *
46  * @package SGL
47  * @author  Demian Turner <demian@phpkitchen.com>
48  * @version $Revision: 1.49 $
49  */
50 class SGL_FrontController
51 {
52     /**
53      * Allow SGL_Output with its template methods to be extended.
54      *
55      * Remember to add your custom include path to the global config, ie a class
56      * called FOO_Output will be discovered if it exists in seagull/lib/FOO/Output.php.
57      * This means '/path/to/seagull/lib' must be added to
58      * $conf['path']['additionalIncludePath'].  The class definition would be:
59      *
60      *  class FOO_Output extends SGL_Output {}
61      *
62      */
63     function getOutputClass()
64     {
65         if (SGL_Config::get('site.customOutputClassName')) {
66             $className = SGL_Config::get('site.customOutputClassName');
67         } else {
68             $className = 'SGL_Output';
69         }
70         return $className;
71     }
72
73     /**
74      * Main invocation, init tasks plus main process.
75      *
76      */
77     public static function run()
78     {
79         if (!defined('SGL_INITIALISED')) {
80             self::init();
81         }
82         //  assign request to registry
83         $input = SGL_Registry::singleton();
84         $req   = SGL_Request::singleton();
85
86         if (PEAR::isError($req)) {
87             //  stop with error page
88             SGL::displayStaticPage($req->getMessage());
89         }
90         $input->setRequest($req);
91
92         //  ensure local config loaded and merged
93         $c = SGL_Config::singleton();
94         $c->ensureModuleConfigLoaded($req->getModuleName());
95
96         $outputClass = self::getOutputClass();
97         $output = new $outputClass();
98
99         // test db connection
100 //SGL_FrontController::testDbConnection($output);
101
102         // run module init tasks
103 //SGL_Task_InitialiseModules::run();
104
105         // see http://trac.seagullproject.org/wiki/Howto/PragmaticPatterns/InterceptingFilter
106         if (!self::customFilterChain($input)) {
107
108
109             $aFilters = array(
110                 //  pre-process (order: top down)
111                 //'SGL_Task_Init',
112                 'SGL_Filter_StripMagicQuotes',
113                 'SGL_Filter_DiscoverClientOs',
114                 'SGL_Filter_ResolveManager',
115                 'SGL_Filter_CreateSession',
116                 'SGL_Filter_SetupLangSupport',
117                 'SGL_Filter_SetupLocale',
118                 'SGL_Filter_AuthenticateRequest',
119                 'SGL_Filter_DetectAdminMode',
120 //                //new SGL_Filter_MaintenanceModeIntercept(
121 //                //new SGL_Filter_DetectSessionDebug(
122 //                //new SGL_Filter_SetupPerms(
123
124                 //  post-process (order: bottom up)
125                 'SGL_Filter_BuildHeaders',
126                 'SGL_Filter_BuildView',
127 //                //new SGL_Filter_BuildDebugBlock(
128 //                //new SGL_Filter_SetupBlocks(
129 //                //new SGL_Filter_SetupNavigation(
130                 'SGL_Filter_SetupGui',
131                 'SGL_Filter_BuildOutputData',
132
133                 //  target
134                 'SGL_MainProcess',
135                 );
136             $input->setFilters($aFilters);
137         }
138         $chain = new SGL_FilterChain($input->getFilters());
139         $chain->doFilter($input, $output);
140         if (SGL_Config::get('site.outputBuffering')) {
141             ob_end_flush();
142         }
143         echo $output->data;
144     }
145
146     function customFilterChain(&$input)
147     {
148         $req = $input->getRequest();
149
150         switch ($req->getType()) {
151
152         case SGL_REQUEST_BROWSER:
153         case SGL_REQUEST_CLI:
154             $mgr = SGL_Inflector::getManagerNameFromSimplifiedName(
155                 $req->getManagerName());
156             //  load filters defined by specific manager
157             if (SGL_Config::get("$mgr.filterChain")) {
158                 $aFilters = explode(',', SGL_Config::get("$mgr.filterChain"));
159                 $input->setFilters($aFilters);
160                 $ret = true;
161
162             //  load sitewide custom filters
163             } elseif (SGL_Config::get('site.filterChain')) {
164                 $aFilters = explode(',', SGL_Config::get('site.filterChain'));
165                 $input->setFilters($aFilters);
166                 $ret = true;
167             } else {
168                 $ret = false;
169             }
170             break;
171
172         case SGL_REQUEST_AJAX:
173             $moduleName = ucfirst($req->getModuleName());
174             $providerName = $moduleName . 'AjaxProvider';
175             if (SGL_Config::get("$providerName.filterChain")) {
176                 $aFilters = explode(',', SGL_Config::get("$providerName.filterChain"));
177             } else {
178                 $aFilters = array(
179                     'SGL_Task_Init',
180                     'SGL_Task_SetupORM',
181                     'SGL_Task_CreateSession',
182                     'SGL_Task_SetupLangSupport',
183                     'SGL_Task_AuthenticateAjaxRequest',
184                     'SGL_Task_BuildAjaxHeaders',
185                     'SGL_Task_CustomBuildOutputData',
186                     'SGL_Task_ExecuteAjaxAction',
187                 );
188             }
189             $input->setFilters($aFilters);
190             $ret = true;
191             break;
192
193         case SGL_REQUEST_AMF:
194             $moduleName = ucfirst($req->getModuleName());
195             $providerName = $moduleName . 'AmfProvider';
196             if (SGL_Config::get("$providerName.filterChain")) {
197                 $aFilters = explode(',', SGL_Config::get("$providerName.filterChain"));
198             } else {
199                 $aFilters = array(
200                     'SGL_Task_Init',
201                     'SGL_Task_SetupORM',
202                     'SGL_Task_CreateSession',
203                     'SGL_Task_SetupLangSupport',
204                     'SGL_Task_ExecuteAmfAction',
205                 );
206             }
207             $input->setFilters($aFilters);
208             $ret = true;
209             break;
210         }
211         return $ret;
212     }
213
214     function testDbConnection($output)
215     {
216         $originalErrorLevel = error_reporting(0);
217
218         // test db connection
219         if (defined('SGL_INSTALLED')) {
220             $dbh = SGL_DB::singleton();
221             if (PEAR::isError($dbh)) {
222                 // stop with error page
223                 SGL::displayErrorPage($output);
224             }
225         }
226         error_reporting($originalErrorLevel);
227     }
228
229
230     public static function init()
231     {
232         self::setupMinimumEnv();
233         self::loadRequiredFiles();
234
235         $autoLoad = (is_file(SGL_VAR_DIR  . '/INSTALL_COMPLETE.php'))
236             ? true
237             : false;
238         $c = SGL_Config::singleton($autoLoad);
239
240         $init = new SGL_TaskRunner();
241         $init->addData($c->getAll());
242         $init->addTask(new SGL_Task_SetupConstantsFinish());
243         //$init->addTask(new SGL_Task_EnsurePlaceholderDbPrefixIsNull());
244         $init->addTask(new SGL_Task_SetGlobals());
245         $init->addTask(new SGL_Task_ModifyIniSettings());
246         //$init->addTask(new SGL_Task_SetupPearErrorCallback());
247         //$init->addTask(new SGL_Task_SetupCustomErrorHandler());
248         $init->addTask(new SGL_Task_SetBaseUrl());
249         //$init->addTask(new SGL_Task_RegisterTrustedIPs());
250         $init->addTask(new SGL_Task_LoadCustomConfig());
251         $init->main();
252         define('SGL_INITIALISED', true);
253     }
254
255     public static function loadRequiredFiles()
256     {
257         $coreLibs = dirname(__FILE__);
258         $aRequiredFiles = array(
259             $coreLibs  . '/Url.php',
260             $coreLibs  . '/HTTP.php',
261             $coreLibs  . '/Manager.php',
262             $coreLibs  . '/Output.php',
263             $coreLibs  . '/String.php',
264             $coreLibs  . '/Session.php',
265             $coreLibs  . '/Util.php',
266             $coreLibs  . '/Config.php',
267             $coreLibs  . '/ParamHandler.php',
268             $coreLibs  . '/Registry.php',
269             $coreLibs  . '/Request.php',
270             $coreLibs  . '/Inflector.php',
271             $coreLibs  . '/Date.php',
272             $coreLibs  . '/Array.php',
273             $coreLibs  . '/Error.php',
274             $coreLibs  . '/Cache.php',
275             //$coreLibs  . '/DB.php',
276             //$coreLibs  . '/BlockLoader.php',
277             $coreLibs  . '/Translation.php',
278             $coreLibs  . '/../data/ary.languages.php',
279         );
280         foreach ($aRequiredFiles as $file) {
281             require_once $file;
282         }
283     }
284
285     public static function setupMinimumEnv()
286     {
287         $init = new SGL_TaskRunner();
288         $init->addTask(new SGL_Task_SetupPaths());
289         $init->addTask(new SGL_Task_SetupConstantsStart());
290         $init->main();
291     }
292 }
293
294 /**
295  * Abstract request processor.
296  *
297  * @abstract
298  * @package SGL
299  *
300  */
301 class SGL_ProcessRequest
302 {
303     function process(SGL_Registry $input, SGL_Output $output) {}
304 }
305
306 /**
307  * Decorator.
308  *
309  * @abstract
310  * @package SGL
311  */
312 class SGL_DecorateProcess extends SGL_ProcessRequest
313 {
314     var $processRequest;
315
316     function SGL_DecorateProcess(/* SGL_ProcessRequest */ $pr)
317     {
318         $this->processRequest = $pr;
319     }
320 }
321
322 /**
323  * Core data processing routine.
324  *
325  * @package SGL
326  * @author  Demian Turner <demian@phpkitchen.com>
327  */
328 class SGL_MainProcess extends SGL_ProcessRequest
329 {
330     function process($input, $output)
331     {
332         SGL::logMessage(null, PEAR_LOG_DEBUG);
333
334         $req  = $input->getRequest();
335         $mgr  = $input->get('manager');
336
337         $mgr->validate($req, $input);
338         $input->aggregate($output);
339
340         //  process data if valid
341         if ($mgr->isValid()) {
342             $ok = $mgr->process($input, $output);
343             if (SGL_Error::count() && SGL_Session::getRoleId() != SGL_ADMIN
344                     && SGL_Config::get('debug.production')) {
345                 $mgr->handleError(SGL_Error::getLast(), $output);
346             }
347         }
348         SGL_Manager::display($output);
349         $mgr->display($output);
350     }
351 }
352
353 /**
354  * Abstract renderer strategy
355  *
356  * @abstract
357  * @package SGL
358  */
359 class SGL_OutputRendererStrategy
360 {
361     /**
362      * Prepare renderer options.
363      *
364      */
365     function initEngine() {}
366
367     /**
368      * Abstract render method.
369      *
370      * @param SGL_View $view
371      */
372     function render($view) {}
373 }
374 ?>
Note: See TracBrowser for help on using the browser.