Example #1
0
 /**
  * environment builder
  *
  * @var string $env forced env value
  */
 public static function init($envKey = null)
 {
     if (!is_null($envKey) && !in_array($envKey, array(self::ENV_DEV, self::ENV_STAGE, self::ENV_PROD))) {
         throw new \Exception(sprintf("'%s' is not a recognized environment", $envKey));
     }
     // enable garbage collection
     gc_enable();
     // enable t41 error handler (notices are not catched until we get a proper logger)
     set_error_handler(array('t41\\Core', 'userErrorHandler'), (E_ALL | E_STRICT) ^ E_NOTICE);
     self::preInit();
     // never cached, shall it be ?
     $config = Config\Loader::loadConfig('application.xml');
     self::$_config = $config['application'];
     if (!is_null($envKey)) {
         self::$env = $envKey;
         self::$appId = $envKey;
     } else {
         /* CLI Mode */
         if (php_sapi_name() == 'cli') {
             self::$mode = self::$_config['environments']['mode'] = 'cli';
             $opts = new \Zend_Console_Getopt(array('env=s' => 'Environment value', 'controller=s' => "Controller", 'module=s' => "Module", 'action=s' => "Action", 'params=s' => "Action parameters", 'simulate' => "Simulate execution"));
             try {
                 $opts->parse();
             } catch (\Zend_Console_GetOpt_Exception $e) {
                 exit($e->getUsageMessage());
             }
             $match = trim($opts->env);
             /* temporary */
             define('CLI_CONTROLLER', trim($opts->controller));
             define('CLI_MODULE', trim($opts->module));
             define('CLI_ACTION', trim($opts->action));
             define('CLI_PARAMS', $opts->params);
             define('CLI_SIMULATE', (bool) $opts->simulate);
         } else {
             View::setDisplay(View\Adapter\WebAdapter::ID);
             /* array of mode / $_SERVER data key value */
             $envMapper = array('hostname' => 'SERVER_NAME');
             $match = isset($_SERVER[$envMapper[self::$_config['environments']['mode']]]) ? $_SERVER[$envMapper[self::$_config['environments']['mode']]] : null;
         }
         /* define which environment matches current mode value */
         if (is_null($match)) {
             throw new Config\Exception("environment value not detected");
         }
         self::$appId = str_replace(array('.', '-'), '_', $match);
         $envKey = null;
         switch (self::$_config['environments']['mode']) {
             case 'cli':
                 foreach (self::$_config['environments'] as $key => $value) {
                     if (!is_array($value)) {
                         continue;
                     }
                     if ($key == $match) {
                         $envKey = self::$env = $key;
                         break;
                     }
                 }
                 break;
             case 'hostname':
             default:
                 foreach (self::$_config['environments'] as $key => $value) {
                     if (!is_array($value)) {
                         continue;
                     }
                     if (isset($value['hostname']) && in_array($match, (array) $value['hostname'])) {
                         $envKey = self::$env = $key;
                         break;
                     }
                 }
                 break;
         }
         if (is_null($envKey)) {
             throw new Config\Exception("No matching environment found");
         }
     }
     self::$_env += self::$_config['environments'][$envKey];
     if (self::getEnvData('cache_backend')) {
         self::$cache = self::getEnvData('cache_backend');
     }
     self::$_env['version'] = self::$_config['versions'][self::$_config['versions']['default']];
     /* define app name */
     self::$name = isset(self::$_config['name']) ? self::$_config['name'] : 'Untitled t41-based application';
     /* set PHP env */
     setlocale(E_ALL, self::$_env['version']['locale']);
     setlocale(LC_MONETARY, self::$_env['version']['currency']);
     date_default_timezone_set(isset(self::$_env['version']['timezone']) ? self::$_env['version']['timezone'] : 'Europe/Paris');
     if (isset(self::$_env['php'])) {
         foreach (self::$_env['php'] as $directive => $value) {
             ini_set($directive, $value);
         }
     }
     // define logger
     if (isset(self::$_env['log']) && self::$_env['log'] != false) {
         self::enableLogger(self::$_env['log']);
     }
     /* define lang - can be overwritten anywhere */
     self::$lang = self::$_config['versions']['default'];
     // load modules
     Core\Module::init(self::$basePath);
     // load ACL
     Core\Acl::init(self::$basePath);
     /* load configuration files if lazy mode is off */
     if (self::$lazy !== true) {
         // get backends configuration
         Backend::loadConfig();
         // get mappers configuration
         Mapper::loadConfig();
         // get object model configuration
         ObjectModel::loadConfig();
     }
     // configure error reporting according to env
     if (in_array(self::$env, array(self::ENV_STAGE, self::ENV_PROD))) {
         error_reporting(E_ERROR);
         //(E_ALL | E_STRICT) ^ E_NOTICE);
         ini_set('display_errors', 1);
     } else {
         error_reporting(E_ERROR);
         // E_ALL & ~E_STRICT);
         ini_set('display_errors', 1);
     }
     // define some basic view data
     View::setEnvData('t41.version', self::VERSION);
     if (class_exists('Zend_Version')) {
         View::setEnvData('zf.version', \Zend_Version::VERSION);
     }
     View::setEnvData('app.name', self::$_config['name']);
     View::setEnvData('app.version', self::getVersion());
     // set a cache adapter
     if (!isset(self::$_adapters['registry'])) {
         self::$_adapters['registry'] = new \Zend_Registry();
     }
     // (re-)init data session
     if (!isset(self::$_adapters['session'])) {
         self::$_adapters['session'] = new \Zend_Session_Namespace('data');
     }
     // to be done at the very end to avoid empty stack on exception
     if (self::$_fancyExceptions === true) {
         //set_exception_handler(array('t41\Core', 'exceptionHandler'));
     }
 }