/** * テンプレート名の取得 * * @param string $tplName テンプレート名(省略可) * @param array $role エージェントロール * @param $ext $ext 拡張子 * * @return array */ protected function _getViewInfo($tplName = null, array $role = array(), $ext = 'tpl') { $result = array(); $pagePath = $this->_getTemplateNameByPageClass($tplName); // ex) user/create // エージェントロール対応ページテンプレート if ($role && !(count($role) === 1 && $role[0] === BEAR_Agent::UA_DEFAULT)) { foreach ($role as $agent) { $agentExtention = '.' . strtolower($agent); $pagePathFull = _BEAR_APP_HOME . $this->_config['path'] . "pages/{$pagePath}{$agentExtention}.tpl"; if (file_exists($pagePathFull)) { break; } $agentExtention = ''; } } else { $agentExtention = ''; } $matches = array(); preg_match('/(.+?)[\\.]/', $pagePath, $matches); if (is_array($matches)) { // $firstWordForConfig = index. $firstWordForConfig = isset($matches[0]) && $matches[0] ? $matches[0] : $pagePath . '.'; } else { $firstWordForConfig = ''; } if (substr($pagePath, 0, 1) == '/') { $templatePath = "{$pagePath}{$agentExtention}.{$ext}"; } else { $templatePath = "pages/{$pagePath}{$agentExtention}.{$ext}"; } $configFileHead = _BEAR_APP_HOME . $this->_config['path'] . 'pages/' . $firstWordForConfig; // 設定ファイル if (file_exists($configFileHead . 'yml')) { $configFilePath = $configFileHead . 'yml'; } elseif (file_exists($configFileHead . 'ini')) { $configFilePath = $configFileHead . 'ini'; } else { $configFilePath = false; } $yml = $configFilePath ? BEAR::loadValues($configFilePath) : array(); $layoutValue = $yml !== array() ? $this->_getRoleLayoutValue($role, $yml) : array(); $result['page_template'] = $templatePath; $result['layout_value'] = $layoutValue; if (isset($yml['layout'])) { $layoutFile = $this->_getRoleFile($role, _BEAR_APP_HOME . $this->_config['path'] . 'layouts/', $yml['layout']); $result['layout_file'] = 'layouts/' . $layoutFile; } return $result; }
/** * Pullリソース * * <pre> * * Example * </pre> * <code> * {resource uri='Entry' params=$entryParams tepmplate="resource/entry"} * </code> * * @category BEAR * @package BEAR_Smarty * @subpackage Plugin * @author Akihito Koriyama <*****@*****.**> * @copyright 2008-2011 Akihito Koriyama All rights reserved. * @license http://opensource.org/licenses/bsd-license.php BSD * @version SVN: Release: @package_version@ $Id:$ * @link http://www.bear-project.net/ * * @param mixed $params * @param Smarty &$smarty * * @return mixed $resource */ function smarty_function_resource($params, &$smarty) { $config = (array) BEAR::loadValues($params['params']) + array('method' => 'read', 'uri' => $params['uri'], 'values' => array(), 'options' => array()); if (isset($params['template'])) { $config['options']['template'] = $params['template']; } if (isset($params['cache_life'])) { $config['options']['cache']['life'] = $params['cache_life']; } if (isset($params['cache_key'])) { $config['options']['cache']['key'] = $params['cache_key']; } $app = BEAR::get('app'); $string = BEAR::factory('BEAR_Ro_Prototype', array('request' => $config, 'path' => $app['BEAR_View']['path']))->request()->toString(); return $string; }
/** * リソースリクエスト実行 * * スタティクファイルをリソースとして扱います。 * XML, YAML, CSV, INIファイルをサポートしています。 * * @return mixed * @throws BEAR_Resource_Execute_Exception */ public function request() { if ($this->_config['method'] === BEAR_Resource::METHOD_READ) { $result = BEAR::loadValues($this->_config['file']); } else { $config = array('info' => compact('method'), 'code' => 400); throw new BEAR_Resource_Execute_Exception('Method "read" is only allowed with static file', $config); } // valuesでheader=trueが指定されてると一行目はキーに $noHeader = is_array($result) || isset($this->_config['values']['header']) && (bool) $this->_config['values']['header'] === true; if (!$noHeader && is_array($result)) { $index = array_shift($result); $array = array(); foreach ($result as $value) { $array[] = array_combine($index, array_values($value)); } } return $result; }
/** * ページ引数へ連想配列でインジェクト * * @param array $args 引数全部 * * @return void */ public function injectArgs(array $args) { $args = (array) BEAR::loadValues($args); $this->_args = array_merge($this->_args, $args); }
/** * コマンドの実行 * * @return void */ public function execute() { $argv = $this->_config['argv']; if (!isset($argv[1])) { return; } // parse $cli = $this->_config['cli']; $parser = new Console_CommandLine(array('name' => 'bear', 'description' => 'BEAR command line interface', 'version' => BEAR::VERSION, 'add_help_option' => true, 'add_version_option' => true)); // create resource $subCmd = $parser->addCommand(self::CMD_CREATE, array('description' => 'create resource.')); $subCmd->addOption('file', array('short_name' => '-g', 'long_name' => '--file', 'action' => 'StoreString', 'description' => 'load arguments file.')); $subCmd->addOption('app', array('short_name' => '-a', 'long_name' => '--app', 'action' => 'StoreString', 'description' => 'specify application path. *Notice* use this on the end of line.')); $subCmd->addArgument('uri', array('description' => 'resource URI')); // read resource $subCmd = $parser->addCommand(self::CMD_READ, array('description' => 'show resource.')); $subCmd->addOption('file', array('short_name' => '-g', 'long_name' => '--file', 'action' => 'StoreString', 'description' => 'load arguments file.')); $subCmd->addOption('length', array('short_name' => '-l', 'long_name' => '--len', 'action' => 'StoreInt', 'description' => 'filter specific lenght each data.')); $subCmd->addOption('format', array('short_name' => '-f', 'long_name' => '--format', 'action' => 'StoreString', 'description' => 'default | table | php | json | csv | printa ')); $subCmd->addOption('app', array('short_name' => '-a', 'long_name' => '--app', 'action' => 'StoreString', 'description' => 'specify application path. *Notice* use this on the end of line.')); $subCmd->addArgument('uri', array('description' => 'resource URI')); // update resource $subCmd = $parser->addCommand(self::CMD_UPDATE, array('description' => 'update resource.')); $subCmd->addOption('file', array('short_name' => '-g', 'long_name' => '--file', 'action' => 'StoreString', 'description' => 'load arguments file.')); $subCmd->addOption('app', array('short_name' => '-a', 'long_name' => '--app', 'action' => 'StoreString', 'description' => 'specify application path. *Notice* use this on the end of line.')); $subCmd->addArgument('uri', array('description' => 'resource URI')); // delete resource $subCmd = $parser->addCommand(self::CMD_DELETE, array('description' => 'delete resource.')); $subCmd->addOption('file', array('short_name' => '-a', 'long_name' => '--file', 'action' => 'StoreString', 'description' => 'load arguments file.')); $subCmd->addOption('app', array('short_name' => '-a', 'long_name' => '--app', 'action' => 'StoreString', 'description' => 'specify application path. *Notice* use this on the end of line.')); $subCmd->addArgument('uri', array('description' => 'resource URI')); // clear-cache $parser->addCommand('clear-cache', array('description' => 'clear all cache.')); // clear-log $parser->addCommand('clear-log', array('description' => 'clear all log.')); // clear-all $parser->addCommand('clear-all', array('description' => 'clear cache and log.')); if ($cli) { // create app $subCmd = $parser->addCommand(self::CMD_INIT_APP, array('description' => 'create new application.')); $subCmd->addArgument('path', array('description' => 'destination path. ex) /var/www/bear.test')); $subCmd->addOption('pearrc', array('short_name' => '-c', 'long_name' => '--pearrc', 'action' => 'StoreString', 'description' => 'find user configuration in `file`')); // set app $subCmd = $parser->addCommand(self::CMD_SET_APP, array('description' => 'set application path.')); $subCmd->addArgument('path', array('description' => 'application path. ex) /var/www/bear.test')); // show app $subCmd = $parser->addCommand(self::CMD_SHOW_APP, array('description' => 'show application path.')); } //exec try { ob_start(); $this->_command = $parser->parse(count($argv), $argv); $buff = ob_get_clean(); $commandName = $this->_command->command_name; switch ($this->_command->command_name) { case self::CMD_INIT_APP: $path = $this->_command->command->args['path']; $path = $this->_makeFullPath($path); $pearrc = $this->_command->command->options['pearrc']; $this->_initApp($path, $pearrc); $this->_setApp($path); break; case self::CMD_SET_APP: $path = $this->_command->command->args['path']; $path = $this->_makeFullPath($path); $this->_setApp($path); break; case self::CMD_SHOW_APP: $this->_checkAppExists(); $this->_showApp(); break; case self::CMD_CLEAR_CACHE: $this->_checkAppExists(); $this->clearCache(); break; case self::CMD_CLEAR_LOG: $this->_checkAppExists(); $this->clearLog(); break; case self::CMD_CLEAR_ALL: $this->_checkAppExists(); $this->clearCache(); $this->clearLog(); break; case self::CMD_CREATE: case self::CMD_READ: case self::CMD_UPDATE: case self::CMD_DELETE: $this->_checkAppExists(); $uri = $this->_command->command->args['uri']; $values = $this->_command->command->options['file'] ? BEAR::loadValues($this->_command->command->options['file']) : array(); $this->_result = $this->_request($commandName, $uri, $values)->getRo(); $this->_config['debug'] = true; break; default: if ($this->_config['cli']) { $this->_result = "BEAR: {$argv[1]}: command not found, try 'bear --help'"; } else { $this->_result = "BEAR: {$argv[1]}: command not found, try 'help'"; } return; } } catch (Exception $e) { $parser->displayError($e->getMessage()); } }