Orion PHP  0.11.12
The PHP5.3 framework
orion.php
Go to the documentation of this file.
00001 <?php
00002 
00003 define( 'FS', '.' );
00004 if( !defined( 'DS' ) ) define( 'DS', DIRECTORY_SEPARATOR );
00005 
00006 /**
00007  * \Orion
00008  * 
00009  * Orion main class.
00010  * Makes everything work together
00011  * 
00012  * <p>Usage : $o = new Orion(); $o->configure('main'); $o->run();
00013  * 
00014  * <p>Copyright (c) 2010-2012, Thibaut Despoulain
00015  * All rights reserved.
00016  * http://orion.bkcore.com/</p>
00017  *
00018  * <p>Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions are met:
00020  * 1. Redistributions of source code must retain the above copyright
00021  *    notice, this list of conditions and the following disclaimer.
00022  * 2. Redistributions in binary form must reproduce the above copyright
00023  *    notice, this list of conditions and the following disclaimer in the
00024  *    documentation and/or other materials provided with the distribution.
00025  * 3. All advertising materials mentioning features or use of this software
00026  *    must display the following acknowledgement:
00027  *    This product includes software developed by the <organization>.
00028  * 4. Neither the name of the <organization> nor the
00029  *    names of its contributors may be used to endorse or promote products
00030  *    derived from this software without specific prior written permission.</p>
00031  *
00032  * <p>THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY
00033  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00034  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00035  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00036  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00041  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
00042  * 
00043  * 
00044  * @author Thibaut Despoulain
00045  * @copyright 2010-2012, Thibaut Despoulain
00046  * @link http://orion.bkcore.com/
00047  * @version 0.11.12
00048  *
00049  * @license BSD 4-clauses
00050  */
00051 class Orion
00052 {
00053     const BASE_NS = '\\Orion\\';
00054     const CONF_NS = '\\Orion\\Configs\\';
00055     const MODULE_NS = '\\Orion\\Modules\\';
00056     const PLUGIN_NS = '\\Orion\\Plugins\\';
00057     const MODEL_NS = '\\Orion\\Models\\';
00058     /**
00059      * Class base
00060      */
00061     const CLASS_BASE = 'Orion';
00062     /**
00063      * Relative path to Orion's core classes
00064      */
00065     const CORE_PATH = 'core/';
00066     /**
00067      * Relative path to Orion's configuration files
00068      */
00069     const CONF_PATH = 'configs/';
00070     /**
00071      * Reslative path to Orion's third-party libs
00072      */
00073     const LIBS_PATH = 'libs/';
00074     /**
00075      * Relative path to Orion's global models
00076      */
00077     const MODEL_PATH = 'models/';
00078     /**
00079      * Relative path to Orion's modules
00080      */
00081     const MODULE_PATH = 'modules/';
00082     /**
00083      * Relative path to Orion's plugins
00084      */
00085     const PLUGIN_PATH = 'plugins/';
00086     /**
00087      * Relative path to Orion's renderers
00088      */
00089     const RENDERER_PATH = 'renderers/';
00090     /**
00091      * Orion's local model extension
00092      */
00093     const MODEL_EXT = '.model';
00094     /**
00095      * Orion's template extension
00096      */
00097     const TEMPLATE_EXT = '.tpl';
00098     /**
00099      * Orion's view extension
00100      */
00101     const VIEW_EXT = '.view';
00102     /**
00103      * Default mode
00104      */
00105     const MODE_DEFAULT = 'default';
00106 
00107     /**
00108      * Set Orion's debug mode
00109      * @var boolean
00110      */
00111     private static $DEBUG = false;
00112 
00113     /**
00114      * OrionConfig accessor variable, use Orion::config() or Orion::o->getConfig() to access it.
00115      * @var Orion\Core\Config
00116      */
00117     private static $CONFIG = null;
00118 
00119     /**
00120      * Module controller accessor variable
00121      * @var Orion\Core\Controller
00122      */
00123     private static $MODULE = null;
00124 
00125     /**
00126      * Path to orion's base directory ('orion/' by default)
00127      * @var string
00128      */
00129     private static $BASE;
00130 
00131     /**
00132      * Orion's mode 
00133      * @var string
00134      */
00135     private static $MODE = 'default';
00136 
00137     /**
00138      * Start the Orion instance.<br />
00139      * Register Orion's spl_autoload.
00140      * @param string $path Path to Orion's main directory. ('orion/' by default)<br/><b>With the trailing slash but without the first.</b>
00141      */
00142     public function __construct( $path='orion/' )
00143     {
00144         self::$BASE = $path;
00145         date_default_timezone_set('Europe/Berlin');
00146         spl_autoload_register('Orion::autoload');
00147     }
00148 
00149     /**
00150      * Load and init a new OrionConfig instance, linking it to Orion.
00151      * @param string $filename The configuration file to use (No path, no extension, just the name).
00152      */
00153     public function configure( $filename )
00154     {
00155         if ( self::$CONFIG == null )
00156         {
00157             $class = self::CONF_NS . ucfirst( $filename );
00158             try
00159             {
00160                 self::$CONFIG = new $class();
00161                 self::$CONFIG->load();
00162             } catch ( Exception $e )
00163             {
00164                 throw new Orion\Core\Exception( 'Configuration file does not exist.', E_USER_ERROR, get_class( $this ) );
00165             }
00166         }
00167         else
00168             throw new Orion\Core\Exception( 'Cannot load more than one config file.', E_USER_WARNING, get_class( $this ) );
00169     }
00170 
00171     /**
00172      * Run everything, launch module, etc.
00173      */
00174     public function run()
00175     {
00176         if ( self::$MODULE != null )
00177             throw new Orion\Core\Exception( 'Only one Orion instance is allowed at a time.', E_USER_ERROR, get_class( $this ) );
00178 
00179         Orion\Core\Context::init( self::$BASE );
00180 
00181         $module = Orion\Core\Context::$MODULE_NAME;
00182         $modulefile = self::$BASE . self::MODULE_PATH . $module . DS . $module . FS . self::$MODE . '.php';
00183         $moduleclass = self::MODULE_NS . ucfirst( $module ) . '\\' . ucfirst( $module ) . ucfirst( self::$MODE );
00184 
00185         if ( !in_array( $module, self::$CONFIG->get( 'OPEN_MODULES' ) ) )
00186             Orion\Core\Context::redirect( 404 );
00187         //throw new Orion\Core\Exception('Module ['.$module.'] is not a trusted module (see OPEN_MODULES in configuration).', E_USER_ERROR, get_class($this));
00188 
00189         if ( !file_exists( $modulefile ) )
00190         //Orion\Core\Context::redirect (404);
00191             throw new Orion\Core\Exception( 'Module class file (' . $modulefile . ') does not exist.', E_USER_ERROR, get_class( $this ) );
00192 
00193         require_once($modulefile);
00194         self::$MODULE = new $moduleclass();
00195         self::$MODULE->load();
00196     }
00197 
00198     /**
00199      * Autoloader for Orion's core classes
00200      * @param string $classname
00201      */
00202     public static function autoload( $classname )
00203     {
00204         try
00205         {
00206             $file = self::parseClassName( $classname );
00207         } catch ( Exception $e )
00208         {
00209             return false;
00210         }
00211 
00212         if ( file_exists( $file ) )
00213             require_once($file);
00214         else
00215             return false;
00216         //throw new Exception('Class file does not exist.', E_USER_ERROR);
00217     }
00218 
00219     /**
00220      * Parse a class name and transform it into its corresponding path
00221      * @param string $name of the class
00222      */
00223     public static function parseClassName( $name )
00224     {
00225         // replace NS separator by DS separator and add extension
00226         $name = str_replace( '\\', '/', strtolower( $name ) ) . '.php';
00227 
00228         // remove any heading slash
00229         if ( $name{0} == '/' )
00230             $name = substr( $name, 1 );
00231 
00232         // change root NS directory if Orion is not in its default directory
00233         if ( substr( $name, 0, 6 ) == 'orion/' && self::$BASE != 'orion/' )
00234             $name = self::$BASE . substr( $name, 6 );
00235 
00236         return $name;
00237     }
00238 
00239     /**
00240      * @return string orion's base dir with the trailing slash. ex: orion/
00241      */
00242     public static function base()
00243     {
00244         return self::$BASE;
00245     }
00246 
00247     /**
00248      * Config class accessor
00249      * @return Orion\Core\Config
00250      */
00251     public static function &config()
00252     {
00253         return self::$CONFIG;
00254     }
00255 
00256     /**
00257      * Current module accessor
00258      * @return Orion\Core\Controller
00259      */
00260     public static function &module()
00261     {
00262         return self::$MODULE;
00263     }
00264 
00265     /**
00266      * Set Orion's mode ('main'|'admin'). You can use Orion::MODE_DEFAULT or Orion::MODE_ADMIN constants.
00267      * <p>Modes are used to determinate which menu and context to use</p>
00268      * Default mode is 'default'
00269      * @param string Mode
00270      */
00271     public static function setMode( $mode )
00272     {
00273         self::$MODE = strtolower( $mode );
00274     }
00275 
00276     /**
00277      * Get Orion's mode ('default'|'admin'). You can use Orion::MODE_DEFAULT or Orion::MODE_ADMIN constants.
00278      * @return string Mode
00279      */
00280     public static function getMode()
00281     {
00282         return self::$MODE;
00283     }
00284 
00285     public static function debug()
00286     {
00287         self::$DEBUG = true;
00288     }
00289 
00290     public static function isDebug()
00291     {
00292         return self::$DEBUG;
00293     }
00294 
00295 }
00296 
00297 ?>