Example #1
0
 public function preDispatch()
 {
     $this->cr = Core::$mode == 'cli' ? "\n" : '<br/>';
     // get active modules list
     // detect existing CronController()
     foreach (Module::getConfig() as $vendor => $modules) {
         foreach ($modules as $key => $module) {
             // ignore disabled module
             if ($module['enabled'] != true) {
                 continue;
             }
             // ignore module without controller (rare though)
             if (!isset($module['controller'])) {
                 continue;
             }
             $path = Core::$basePath . "application/modules/{$vendor}/{$key}/controllers/CronController.php";
             if (file_exists($path)) {
                 $this->_modules[$vendor . '/' . $key] = $module;
             }
         }
     }
 }
Example #2
0
 public function init()
 {
     // Just in cas something goes wrong before the end
     // @todo replace with a setTemplate() in t41\Exception
     View::setTemplate('default.html');
     // get page identifiers (module, controller and action)
     Layout::$module = $this->_getParam('module');
     Layout::$controller = $this->_getParam('controller');
     Layout::$action = $this->_getParam('action');
     // provide controller with basic information about the current module
     foreach (Module::getConfig() as $vendor => $modules) {
         foreach ($modules as $key => $module) {
             if (isset($module['controller']) && Layout::$module == $module['controller']['base']) {
                 $this->_module = 'app/' . $vendor . '/' . $key;
                 Layout::$vendor = $vendor;
                 Layout::$moduleKey = $key;
                 $resource = Layout::$controller;
                 if (Layout::$action) {
                     $resource .= '/' . Layout::$action;
                 }
                 if (isset($module['controller']['items'])) {
                     foreach ($module['controller']['items'] as $controller) {
                         if ($this->_getCurrentItem($resource, $controller) == true) {
                             break;
                         }
                     }
                 }
                 if (isset($module['controllers_extends'])) {
                     foreach ($module['controllers_extends'] as $controller) {
                         if ($this->_getCurrentItem($resource, $controller['items']) == true) {
                             break;
                         }
                     }
                 }
                 break;
             }
         }
     }
 }
Example #3
0
 /**
  * Detect all modules directories and try to get config for them
  * @param string $path
  */
 public static function init($path)
 {
     if (Core::getEnvData('cache_configs') !== false) {
         $ckey = 'configs_acl';
         if (($cached = Core::cacheGet($ckey)) !== false) {
             self::$_config = $cached;
             return;
         }
     }
     // load application acl configuration file
     $config = Config\Loader::loadConfig('acl.xml', Config::REALM_CONFIGS);
     $resources = array();
     // add all fragments coming from modules
     foreach (Core\Module::getConfig() as $vendorId => $vendorModules) {
         foreach ($vendorModules as $key => $module) {
             // module menus
             if (isset($module['controller']) && isset($module['controller']['items'])) {
                 // walk recursively through all module's items (menu elements)
                 $resources += self::_getAcl($module['controller']['base'], $module['controller']['items']);
             }
             // and optional menus extensions
             if (isset($module['controllers_extends'])) {
                 foreach ($module['controllers_extends'] as $controller => $data) {
                     $resources += self::_getAcl($module['controller']['base'], $data['items']);
                 }
             }
         }
     }
     if (!isset($config['acl']['resources'])) {
         $config['acl']['resources'] = array();
     }
     $config['acl']['resources'] += $resources;
     self::$_config = $config['acl'];
     if (isset($ckey)) {
         Core::cacheSet($config['acl'], $ckey, true, array('tags' => array('config', 'acl')));
     }
 }
Example #4
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'));
     }
 }