예제 #1
0
// Step 1a: Define absolute environment values
// set so that we can check if PHP pages have been accessed directly
if (!defined('FARI')) {
    define('FARI', 'Fari Framework 2.3.15.0 (August 27, 2012)');
}
// get absolute pathname and define it as a constant (server install path)
if (!defined('BASEPATH')) {
    define('BASEPATH', dirname(__FILE__));
}
// www root dir (path for links in your views)
if (!defined('WWW_DIR')) {
    // now we can have the app running in the root
    dirname($_SERVER['SCRIPT_NAME']) == '/' ? define('WWW_DIR', '') : define('WWW_DIR', dirname($_SERVER['SCRIPT_NAME']));
}
// default file extension (.php)
if (!defined('EXT')) {
    define('EXT', '.' . pathinfo(__FILE__, PATHINFO_EXTENSION));
}
// Step 1b: Include database and application specific settings
require BASEPATH . '/config/config' . EXT;
// Step 2a: Initialize Error & Exceptions handling and check environment
require BASEPATH . '/fari/Application/ApplicationDiagnostics' . EXT;
require BASEPATH . '/fari/Application/ApplicationEnvironment' . EXT;
Fari_ApplicationEnvironment::startupCheck();
// Step 2b: Setup contracts handling
require BASEPATH . '/fari/Application/ApplicationContracts' . EXT;
// Step 3: Define global functions, autoloading and those required for framework start
require BASEPATH . '/fari/Application/ApplicationFunctions' . EXT;
// Step 4: Start the whole shebang
Fari_ApplicationRouter::loadRoute();
 /**
  * Load the Presenter and Action from route requested.
  * @return void
  */
 public static function loadRoute()
 {
     // first make sure we have the Presenter directory right
     $presenterDir = BASEPATH . '/' . APP_DIR . '/presenters/';
     try {
         // throw an error if is not a directory
         if (!is_dir($presenterDir)) {
             throw new Fari_Exception('Not a Presenter directory: ' . $presenterDir . '.');
             // else set the path
         } else {
             self::$presenterDir = $presenterDir;
         }
     } catch (Fari_Exception $exception) {
         $exception->fire();
     }
     // get route as passed by .htaccess
     $route = new Fari_ApplicationRoute();
     // set file requested
     $presenterFile = self::$presenterDir . $route->presenter . EXT;
     // now check if the Presenter file is readable...
     if (!is_readable($presenterFile)) {
         // ... not, so update Presenter path to the error page 404
         $route->setErrorRoute();
         // update the path to file
         $presenterFile = self::$presenterDir . $route->presenter . EXT;
         // throw an exception if the error Presenter doesn't exist either
         try {
             if (!is_readable($presenterFile)) {
                 throw new Fari_Exception('Missing 404 Error Presenter: ' . $presenterFile);
             }
         } catch (Fari_Exception $exception) {
             $exception->fire();
         }
     }
     // now we know the Presenter path exists so include
     include $presenterFile;
     // from variable to instance of an object
     $class = ucwords($route->presenter);
     $presenter = @new $class($route);
     // check that the Presenter object extends Fari_ApplicationPresenter
     try {
         if (!$presenter instanceof Fari_ApplicationPresenter) {
             throw new Fari_Exception('Presenter object ' . $route->presenter . ' does not extend Fari_ApplicationPresenter.');
         }
     } catch (Fari_Exception $exception) {
         $exception->fire();
     }
     // Presenter is set, now check we can call the Action in it with the appropriate prefix
     if (!is_callable(array($presenter, $route->action))) {
         $route->setDefaultAction();
     } else {
         // we have implemented method overloading in the presenter so make a little checkup first
         try {
             // reflect the action we are calling
             $method = new Fari_ApplicationReflection($route->presenter, $route->action);
         } catch (ReflectionException $e) {
             // set default
             $route->setDefaultAction();
         }
     }
     // reflect the action we are calling
     // save us some trouble if we've instantiated already
     if (!$method instanceof Fari_ApplicationReflection) {
         $method = new Fari_ApplicationReflection($route->presenter, $route->action);
     }
     // ...and optionally pass variable number of parametres
     if ($method->hasParameters()) {
         $method->setParameters($route->parameters);
     }
     // call the action (method) in the Presenter
     $method->call($presenter);
 }