Пример #1
0
 /**
  * STAGE 7: Render results in response to request.
  */
 public static function stage7($response)
 {
     /////////////////////////////
     // ==> SECTION: mvc <==
     $response->renderExceptions(true);
     // show any excpetions in the visible output (i.e. debug mode)
     // OR: Handle exceptions thrown in the dispatch loop.
     // Examine the exception type, and then redirect to an error page.
     if (isset(self::$registry['ajax'])) {
         #$response->clearBody("any segments you don't want");
     } else {
         // Use UTF-8.  See "1. Content-type, Charset, DOCTYPE" in NOTES.txt
         $response->setHeader('Content-type', 'text/html; charset=utf-8', true);
         ksort($_SERVER);
         self::$view->SERVER = $_SERVER;
         self::$view->log = ZFDemo_Log::get();
         /////////////////////////////
         // ==> SECTION: mvc <==
         try {
             $doctype = self::$view->render('header.php');
             $title = $response->getBody('body');
             if (empty($title) || stripos($response->getBody('body'), '<title>') === false) {
                 // view did not include a title, so add a default title now
                 $doctype .= self::$view->render('title.php');
             }
             $response->prepend('doctype', $doctype);
             $response->appendBody(self::$view->render('footer.php'), 'footer');
         } catch (Exception $exception) {
             $msg = 'ERROR corrupt installation, can not find site template file(s) header.php or footer.php.';
             echo $msg;
             self::doExit(500, null, $msg);
             // reader excercise: smarter error handling for Zend_View exceptions
         }
     }
     //echo __FILE__,__LINE__;Zend_Debug::dump($response);exit; // debug only the response object
     $response->sendResponse();
     // send final results to browser, including headers
     return true;
 }
Пример #2
0
 public static function bootstrap($installDir)
 {
     if (self::testEnvironment() === false) {
         echo 'Environment fails to meet minimum requirements for this demo tutorial.';
         return;
     }
     // where to find this application's configuration (using Conventional Modular Layout)
     $ds = DIRECTORY_SEPARATOR;
     // too much typing ;)
     if ($installDir[0] === '/') {
         $tmp = $installDir;
     } else {
         $tmp = dirname(__FILE__) . $ds . '..' . $ds . '..' . $ds . 'zfdemo' . $ds . $installDir;
     }
     // STAGE 0: Initializations / Loading Configuration
     ZFDemo_Log::log("looking for application directory in: realpath({$tmp}" . $ds . ')');
     $appDir = realpath($tmp) . $ds;
     ZFDemo_Log::log('$appDir =' . $appDir);
     self::$registry = Zend_Registry::getInstance();
     self::$registry['appDir'] = $appDir;
     if (!is_readable($appDir)) {
         ZFDemo_Log::log("ERROR: Application directory is not readable (path problem).\n", true);
         return false;
     }
     // this application's configuration information
     $configDir = realpath($appDir . $ds . 'config' . $ds) . $ds;
     self::$registry['configDir'] = $configDir;
     ZFDemo_Log::log('$configDir =' . $configDir);
     if (!is_readable($configDir)) {
         ZFDemo_Log::log("ERROR: Application configuration directory 'config' is not readable (path problem).\n", true);
         return false;
     }
     // persistent dynamic data, like log files or SQLite files
     $dataDir = realpath($appDir . $ds . 'data' . $ds) . $ds;
     self::$registry['dataDir'] = $dataDir;
     ZFDemo_Log::log('$dataDir =' . $dataDir);
     if (!is_readable("{$dataDir}")) {
         ZFDemo_Log::log("ERROR: Application 'data' directory is not readable (path problem).\n", true);
         return false;
     }
     // temporary data, like PHP session state files
     $temporaryDir = realpath($appDir . $ds . 'temporary' . $ds) . $ds;
     self::$registry['temporaryDir'] = $temporaryDir;
     ZFDemo_Log::log('$temporaryDir =' . $temporaryDir);
     if (!is_readable("{$temporaryDir}")) {
         ZFDemo_Log::log("ERROR: Application 'temporary' directory is not readable (path problem).\n", true);
         return false;
     }
     // add the application-specific source file path to PHP's include path for the Conventional Modular Layout
     set_include_path($appDir . PATH_SEPARATOR . get_include_path());
     ZFDemo_Log::log("PHP Include Path = \n    " . str_replace(':', "\n    ", ini_get('include_path')));
     self::$environment = 'sandbox';
     // after this point, all defaults come from config files
     require 'Zend/Config/Ini.php';
     $config = new Zend_Config_Ini($configDir . 'config.ini', self::$environment, true);
     ZFDemo_Log::log("config.ini=" . print_r($config->asArray(), true));
     if (strpos($config->log, '/') !== 0) {
         $config->log = $dataDir . $config->log;
     }
     self::$registry['config'] = $config;
     // application configuration array
     date_default_timezone_set($config->timezone);
     $sessionConfig = new Zend_Config_Ini($configDir . 'Zend_Session.ini', self::$environment, true);
     $sessionConfig->save_path = $temporaryDir . $sessionConfig->save_path;
     ZFDemo_Log::log("Zend_Session.ini=" . print_r($sessionConfig->asArray(), true));
     require 'Zend/Session.php';
     Zend_Session::setOptions($sessionConfig->asArray());
     Zend_Session::start();
     /*
      * The zfdemo will not work unless the following code results creates a session file
      * in your save_path folder, * with file contents like:
      *    foo|a:2:{s:3:"bar";s:5:"apple";s:4:"time";s:19:"2007-02-20 21:30:36";}
      */
     $testSpace = new Zend_Session_Namespace('spaceFoo');
     $testSpace->keyBar = 'valueBar';
     $testSpace->time = time();
     $testSpace->date = date('Y-m-d H:i:s');
     // preemptively write session file now
     Zend_Session::writeClose();
     self::testPdo($config);
     // sanity check connection and zfdemo tables using PDO
     // Now test using ZF's MySQL PDO DB adapter:
     require 'Zend/Db.php';
     require 'Zend/Db/Adapter/Pdo/Mysql.php';
     // setup our DB adapter
     $db = new Zend_Db_Adapter_Pdo_Mysql($config->db->asArray());
     self::$registry['db'] = $db;
     self::testDb($db);
     // sanity check connection and zfdemo tables using Zend Db Adapter
     // STAGE 1: Prepare the front (primary) controller.
     require 'Zend/Controller/Front.php';
     $frontController = Zend_Controller_Front::getInstance();
     // manages the overall workflow
     $baseUrl = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], '/index.php'));
     ZFDemo_Log::log("baseUrl={$baseUrl}");
     //$frontController->setBaseUrl($baseUrl);
     $frontController->setControllerDirectory(array('default' => $appDir . 'default' . $ds . 'controllers', 'forum' => $appDir . 'forum' . $ds . 'controllers'));
     // Initialize views
     require 'Zend/View.php';
     self::$view = new Zend_View();
     self::$view->sectionName = basename($installDir);
     // e.g. "section1_install"
     self::$view->setScriptPath($appDir . 'default' . $ds . 'views');
     self::$view->showLog = true;
     ZFDemo_Log::log("scriptPaths=\n    " . implode("\n    ", self::$view->getScriptPaths()));
     $frontController->setParam('view', self::$view);
     // STAGE 2: Find the right action and execute it.
     // Use routes to calculate controllers and actions to execute
     // Dispatch calculated actions of the selected controllers
     $frontController->returnResponse(true);
     // return the response (do not echo it to the browser)
     // Use UTF-8.  See "1. Content-type, Charset, DOCTYPE" in NOTES.txt
     require_once 'Zend/Controller/Response/Http.php';
     $response = new Zend_Controller_Response_Http();
     $response->setHeader('Content-type', 'text/html; charset=utf-8', true);
     $response->setBody(self::$view->render('header.php'));
     try {
         require_once 'Zend/Controller/Request/Http.php';
         $request = new Zend_Controller_Request_Http();
         // show exceptions immediately, instead of adding to the response
         $frontController->throwExceptions(true);
         // without this no exceptions are thrown
         // similar to "running" the configured MVC "program"
         $frontController->dispatch($request, $response);
     } catch (Zend_Controller_Dispatcher_Exception $exception) {
         self::analyzeError($frontController->getDispatcher(), $exception, $request, $response);
         return false;
     }
     // STAGES 3 to 5 occur in an action controller: /zfdemo/forum/*/controllers/*Controller.php
     // STAGE 6 occurs in a view template: /zfdemo/forum/*/views/*.phtml
     /**
      * STAGE 7: Render results in response to request.
      */
     $response->renderExceptions(true);
     // show any excpetions in the visible output (i.e. debug mode)
     // OR: Handle exceptions thrown in the dispatch loop.
     // Examine the exception type, and then redirect to an error page.
     ksort($_SERVER);
     self::$view->SERVER = $_SERVER;
     self::$view->log = ZFDemo_Log::get();
     $response->appendBody(self::$view->render('footer.php'), 'footer');
     //Zend::debug($frontController->getRequest());exit // debug the request object
     //Zend_Debug::dump($response);exit;  // examine the inner details of the response object
     $response->sendResponse();
     // send final results to browser, including headers
 }