/** * Execute the action and returns a result * * @return array */ public function execute($params = null) { if ((!isset($params[$this->queryfield]) || empty($params[$this->queryfield])) && (!isset($params[$this->queryidfield]) || empty($params[$this->queryidfield]))) { return false; } if (isset($params['_offset'])) { $this->setParameter('offset', (int) $params['_offset']); } $extra = isset($params['extra']) ? (array) $params['extra'] : array(); if (Core::getEnvData('cache_datasets') === true) { $hash = serialize($extra); $hash .= isset($params[$this->queryfield]) ? $params[$this->queryfield] : $params[$this->queryidfield]; $md5 = md5($hash); //@todo check unicity, especially with hard-coded conditions having() $ckey = 'ds_ac_' . $this->_cachePrefix . '_' . $md5 . '_' . $this->getParameter('offset') . '_' . $this->getParameter('batch'); if (($data = Core::cacheGet($ckey)) === false) { if (isset($params[$this->queryfield])) { $data = $this->_getSuggestions(trim($params[$this->queryfield]), $extra); } else { $data = $this->_getFromIdentifier(trim($params[$this->queryidfield])); } $data['cache-key'] = $ckey; Core::cacheSet($data, $ckey, true, array('tags' => array('view', 'ac', str_replace('\\', '_', $this->_obj->getClass())))); } } else { if (isset($params[$this->queryfield])) { $data = $this->_getSuggestions(trim($params[$this->queryfield]), $extra); } else { $data = $this->_getFromIdentifier(trim($params[$this->queryidfield])); } } return $data; }
/** * Load a file into a Configuration Array * * @param string $file * @param integer $realm * @param array $params * * @return array|false Configuration Array or false if the file don't exists */ public static function loadConfig($file, $realm = Config::REALM_CONFIGS, array $params = null) { // try to get cached version if configs caching is enabled if (Core::getEnvData('cache_configs') === true) { $ckey = self::getCacheKey($file, $realm); if (($cached = Core::cacheGet($ckey)) !== false) { Core::log(sprintf('[Config] Retrieved %s config file from cache', $file)); return $cached; } } if (($filePath = self::findFile($file, $realm)) == null) { /* no matching file name in paths */ Core::log(sprintf('[Config] Failed loading %s config file', $file), \Zend_Log::ERR); return false; } $type = substr($file, strrpos($file, '.') + 1); /* use existing adapter instance or create it */ if (!isset(self::$_adapters[$type])) { $className = sprintf('\\t41\\Config\\Adapter\\%sAdapter', ucfirst(strtolower($type))); try { self::$_adapters[$type] = new $className($filePath, $params); if (!self::$_adapters[$type] instanceof Adapter\AbstractAdapter) { throw new Exception("{$className} is not implementing AbstractAdapter."); } } catch (\Exception $e) { throw new Exception($e->getMessage()); } } else { self::$_adapters[$type]->setPath($filePath); } $config = self::$_adapters[$type]->load(); Core::log(sprintf('[Config] Successfully loaded %s config file', $file)); /* if ckey is set, cache is activated but empty */ if (isset($ckey)) { Core::cacheSet($config, $ckey, true, array('tags' => array('config'))); } return $config; }
/** * 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'))); } }
/** * (non-PHPdoc) * @see \t41\View\Adapter\AbstractAdapter::_renderComponents() */ protected function _renderComponents($type, $params = null) { if (!isset($this->_component[$type])) { return ''; } $components = $this->_component[$type]; if (count($components) == 0) { return null; } if ($params) { // params come as a pseudo json string $params = \Zend_Json::decode('{' . $params . '}'); $baseUrl = sprintf('http%s://%s', $_SERVER['SERVER_PORT'] == 443 ? 's' : null, $_SERVER['SERVER_NAME']); } $html = ''; $cdn = Core::getEnvData('cdn'); foreach ($components as $component) { if (isset($params['fullUrl']) && $params['fullUrl'] == true && substr($component, 0, 4) != 'http') { $component = $baseUrl . $component; } // use CDN url if provided if (!is_null($cdn)) { $component = '//' . $cdn . $component; } switch ($type) { case 'css': $html .= sprintf('<link rel="stylesheet" href="%s" type="text/css" />' . "\n", $component); break; case 'js': $html .= sprintf('<script src="%s" type="text/javascript"></script>' . "\n", $component); break; } } return $html; }
protected function _getConfigValue($var) { if (is_string($var)) { return $var; } else { return isset($var[\t41\Core::getEnvData('webEnv')]) ? $var[\t41\Core::getEnvData('webEnv')] : null; } }
/** * Recupérer une instance de Backend à partir de son Uri, alias ou encore id dans la liste. * * @param string|t41_Backend_Uri alias or uri of desired backend * @return t41\Backend\Adapter\AbstractAdapter Backend Adapter */ public static function getInstance($id) { if (!is_array(self::$_config)) { self::loadConfig(); } if ($id instanceof Backend\BackendUri) { if ($id->getAlias()) { return self::getInstance($id->getAlias()); } else { if ($id->getHost() && $id->getType()) { // si uri n'est pas un alias mais contient au moins host + type return self::factory($id); } } } else { if (substr($id, 0, 1) == self::PREFIX) { $id = substr($id, 1); } /* return already instanciated backend */ if (isset(self::$_backendsObj[$id])) { return self::$_backendsObj[$id]; } if (isset(self::$_config[$id])) { $config = self::$_config[$id]; $uri = array(); /* temp fix - some backends require adapters (PDO), most don't */ if (!isset($config['uri']['adapter'])) { $uri['adapter'] = $config['type']; } /* reduce possible arrays into proper env-based value */ /* @todo use parent value if backend extends some other backend */ foreach ($config['uri'] as $key => $val) { if (is_array($val)) { $uri[$key] = isset($val[Core::$env]) ? $val[Core::$env] : null; } else { $uri[$key] = $val; } } $uri = new Backend\BackendUri($uri); $backend = self::factory($uri, $id); /* @todo refactor this */ if (isset($config['mapper'])) { if (is_array($config['mapper'])) { if (isset($config['mapper'][Core::getEnvData('webEnv')])) { $mapper = $config['mapper'][Core::getEnvData('webEnv')]; } else { throw new Backend\Exception("NO_MAPPER_VALUE"); } } else { $mapper = $config['mapper']; } $mapper = Mapper::getInstance($mapper); $backend->setMapper($mapper); } return $backend; } } throw new Backend\Exception('Unknown backend alias: ' . $id . '.'); }
protected function _setResponse($status = 'OK', $data, $context = null, $format = 'json') { $response = array('status' => $status, 'data' => $data, 'context' => $context); switch ($format) { case 'json': default: $this->getResponse()->setHeader('Content-type', 'application/json')->setBody(\Zend_Json::encode($response)); if (Core::getEnvData('cache_datasets') !== false) { $this->getResponse()->setHeader('Expires', gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 3600)); $this->getResponse()->clearHeader('Cache-Control'); $this->getResponse()->clearHeader('Pragma'); } break; } $this->getResponse()->sendResponse(); exit; }
/** * Detect all modules directories and try to get config for them * @param string $path */ public static function init($path) { self::$_path = $path . 'application/modules' . DIRECTORY_SEPARATOR; if (!is_dir(self::$_path)) { return false; } // get config from cache if (Core::getEnvData('cache_configs') == "bogus") { $ckey = 'configs_modules'; if (($cached = Core::cacheGet($ckey)) !== false) { self::$_config = $cached; } } if (true) { //! self::$_config) { self::$_config = array(); self::$_modules = array('enabled' => array(), 'disabled' => array()); foreach (scandir(self::$_path) as $vendor) { if (is_dir(self::$_path . $vendor) && substr($vendor, 0, 1) != '.') { foreach (scandir(self::$_path . $vendor) as $entry) { $fpath = self::$_path . $vendor . DIRECTORY_SEPARATOR . $entry . DIRECTORY_SEPARATOR; if (is_dir($fpath) && substr($entry, 0, 1) != '.') { if (is_dir($fpath . 'configs')) { // register path with $vendor as prefix Config::addPath($fpath . 'configs', Config::REALM_MODULES, null, $vendor); } } } } } // load all detected modules configuration file $config = Config\Loader::loadConfig('module.xml', Config::REALM_MODULES); // remove useless "modules" key foreach ($config as $key => $val) { self::$_config[$key] = $val['modules']; } // cache config if cache is enabled if (isset($ckey)) { Core::cacheSet(self::$_config, $ckey); } } unset($ckey); // build menu if (Core::getEnvData('cache_configs') == "bogus") { $ckey = 'configs_menu_main'; if (($cached = Core::cacheGet($ckey)) !== false) { $menu = $cached; } } if (true) { //! isset($menu)) { $menu = new Layout\Menu('main'); $menu->setLabel('Main Menu'); foreach (self::$_config as $prefix => $modules) { foreach ($modules as $key => $module) { if ($module['enabled'] != 'true') { continue; } $path = Core::$basePath . 'application/modules' . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR . $key; self::bind($module, $path); // if module has controllers, declare them to front controller // then add them to main menu if (isset($module['controller']) || isset($module['controllers_extends'])) { Config::addPath($path . '/controllers/', Config::REALM_CONTROLLERS, null, $module['controller']['base']); } if (isset($module['controller'])) { $menu->addItem($module['controller']['base'], $module['controller']); } // if module extends existing controllers, declare them for inclusion at the end of the process if (isset($module['controllers_extends']) && !empty($module['controllers_extends'])) { $menu->registerExtends($module['controller']['base'], $module['controllers_extends']); } } } // process extends declaration when menu is complete $menu->proceedExtends(); if (isset($ckey)) { Core::cacheSet($menu, $ckey); } } Layout::addMenu('main', $menu); }