File indexing completed on 2024-12-22 05:33:09

0001 <?php
0002 
0003 /**
0004  * Flooer Framework
0005  *
0006  * LICENSE: BSD License (2 Clause)
0007  *
0008  * @category    Flooer
0009  * @package     Flooer_Application
0010  * @subpackage  Bootstrap
0011  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0012  * @copyright   Akira Ohgaki
0013  * @license     https://opensource.org/licenses/BSD-2-Clause  BSD License (2 Clause)
0014  * @link        https://github.com/akiraohgaki/flooer
0015  */
0016 
0017 /**
0018  * Usage
0019  *
0020  * $bootstrap = new Flooer_Application_Bootstrap($application);
0021  * $bootstrap->bootstrap();
0022  */
0023 
0024 /**
0025  * Bootstrapper class of application
0026  *
0027  * @category    Flooer
0028  * @package     Flooer_Application
0029  * @subpackage  Bootstrap
0030  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0031  */
0032 class Flooer_Application_Bootstrap
0033 {
0034 
0035     /**
0036      * Application environment object
0037      *
0038      * @var     Flooer_Application
0039      */
0040     protected $_application = null;
0041 
0042     /**
0043      * Configuration options
0044      *
0045      * @var     array
0046      */
0047     protected $_config = array(
0048         'methodPrefix' => 'init'
0049     );
0050 
0051     /**
0052      * Bootstrap status
0053      *
0054      * @var     bool
0055      */
0056     protected $_status = false;
0057 
0058     /**
0059      * Constructor
0060      *
0061      * @param   Flooer_Application &$application
0062      * @param   array $config
0063      * @return  void
0064      */
0065     public function __construct(Flooer_Application &$application, array $config = null)
0066     {
0067         $this->_application =& $application;
0068         if ($config) {
0069             $this->_config = $config + $this->_config;
0070         }
0071     }
0072 
0073     /**
0074      * Bootstrap application
0075      *
0076      * @return  void
0077      */
0078     public function bootstrap()
0079     {
0080         if ($this->_status) {
0081             trigger_error(
0082                 'Bootstrap is already running',
0083                 E_USER_NOTICE
0084             );
0085         }
0086         $this->_status = true;
0087         $methods = get_class_methods($this);
0088         foreach ($methods as $method) {
0089             if (strpos($method, $this->_config['methodPrefix']) === 0
0090                 && $method != $this->_config['methodPrefix']
0091             ) {
0092                 $this->$method();
0093             }
0094         }
0095     }
0096 
0097     /**
0098      * Predefined method to initialize a dispatcher object
0099      *
0100      * @return  void
0101      */
0102     public function initDispatch()
0103     {
0104         require_once 'Flooer/Application/Dispatch.php';
0105         $this->_application->setResource(
0106             'dispatch',
0107             new Flooer_Application_Dispatch(
0108                 $this->_application,
0109                 array(
0110                     'baseDir' => $this->_application->getConfig('baseDir')
0111                         . '/controllers'
0112                 )
0113             )
0114         );
0115     }
0116 
0117     /**
0118      * Predefined method to initialize an HTTP request object
0119      *
0120      * @return  void
0121      */
0122     public function initRequest()
0123     {
0124         require_once 'Flooer/Http/Request.php';
0125         $this->_application->setResource(
0126             'request',
0127             new Flooer_Http_Request(array(
0128                 'uriMapRules' => array(
0129                     array(
0130                         '^([^/]+)/?([^/]*)/?(.*)$',
0131                         'controller=$1&action=$2&params=$3',
0132                         'params'
0133                     )
0134                 )
0135             ))
0136         );
0137     }
0138 
0139     /**
0140      * Predefined method to initialize an HTTP response object
0141      *
0142      * @return  void
0143      */
0144     public function initResponse()
0145     {
0146         require_once 'Flooer/Http/Response.php';
0147         $this->_application->setResource(
0148             'response',
0149             new Flooer_Http_Response(array(
0150                 'headers' => array(
0151                     'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT',
0152                     'X-Powered-By' => ''
0153                 )
0154             ))
0155         );
0156     }
0157 
0158     /**
0159      * Predefined method to initialize an HTTP cookie object
0160      *
0161      * @return  void
0162      */
0163     public function initCookie()
0164     {
0165         require_once 'Flooer/Http/Cookie.php';
0166         $this->_application->setResource(
0167             'cookie',
0168             new Flooer_Http_Cookie(array(
0169                 'expire' => time() + 60 * 60 * 24 * 7,
0170                 'path' => '/'
0171             ))
0172         );
0173     }
0174 
0175     /**
0176      * Predefined method to initialize a session object
0177      *
0178      * @return  void
0179      */
0180     public function initSession()
0181     {
0182         require_once 'Flooer/Http/Session.php';
0183         $this->_application->setResource(
0184             'session',
0185             new Flooer_Http_Session(array(
0186                 'cacheParamsLimiter' => 'nocache',
0187                 'cacheParamsExpire' => 0,
0188                 'name' => 'Flooer_Http_Session'
0189             ))
0190         );
0191     }
0192 
0193     /**
0194      * Predefined method to initialize a server and execution environment information object
0195      *
0196      * @return  void
0197      */
0198     public function initServer()
0199     {
0200         require_once 'Flooer/Http/Server.php';
0201         $this->_application->setResource(
0202             'server',
0203             new Flooer_Http_Server()
0204         );
0205     }
0206 
0207     /**
0208      * Predefined method to initialize a view object
0209      *
0210      * @return  void
0211      */
0212     public function initView()
0213     {
0214         require_once 'Flooer/View.php';
0215         $this->_application->setResource(
0216             'view',
0217             new Flooer_View(array(
0218                 'baseDir' => $this->_application->getConfig('baseDir')
0219                     . '/views'
0220             ))
0221         );
0222     }
0223 
0224     /**
0225      * Predefined method to initialize a gettext and locale information setting object
0226      *
0227      * @return  void
0228      */
0229     public function initGettext()
0230     {
0231         require_once 'Flooer/Gettext.php';
0232         $this->_application->setResource(
0233             'gettext',
0234             new Flooer_Gettext(array(
0235                 'baseDir' => $this->_application->getConfig('baseDir')
0236                     . '/locales',
0237                 'encoding' => $this->_application->getConfig('encoding')
0238             ))
0239         );
0240     }
0241 
0242     /**
0243      * Predefined method to initialize an error logger object
0244      *
0245      * @return  void
0246      */
0247     public function initLog()
0248     {
0249         require_once 'Flooer/Log.php';
0250         $this->_application->setResource(
0251             'log',
0252             new Flooer_Log(array(
0253                 'file' => $this->_application->getConfig('baseDir')
0254                     . '/logs/error.log',
0255             ))
0256         );
0257     }
0258 
0259     /**
0260      * Get an application environment object
0261      *
0262      * @return  Flooer_Application
0263      */
0264     public function getApplication()
0265     {
0266         return $this->_application;
0267     }
0268 
0269     /**
0270      * Set a method prefix
0271      *
0272      * @param   string $prefix
0273      * @return  void
0274      */
0275     public function setMethodPrefix($prefix)
0276     {
0277         $this->_config['methodPrefix'] = $prefix;
0278     }
0279 
0280     /**
0281      * Get a method prefix
0282      *
0283      * @return  string
0284      */
0285     public function getMethodPrefix()
0286     {
0287         return $this->_config['methodPrefix'];
0288     }
0289 
0290 }