示例#1
0
 /**
  * リソースリクエスト実行
  *
  * <pre>
  * htdocs/のページをリソースとして扱うクラスです。
  * readがページクラスのonInit()を呼び出しonInit内でsetされたものが結果になって帰ります。フォーマットはROです。
  * create, update, deleteはonAction()を呼び出します。
  *
  * $this->_config['options']に応じて次のどちらをpageリソースにするか決定されます。
  *
  *   1)pageが出力するHTML
  *   2)set()でセットされたリソース結果の集合
  * </pre>
  *
  * @return mixed
  * @throws BEAR_Exception
  */
 public function request()
 {
     $this->_setGetPost($this->_config['options']);
     $pageRawPath = substr($this->_config['uri'], 7);
     $url = parse_url($this->_config['uri']);
     $pageRawPath = $url['path'];
     $pageClass = 'page' . str_replace('/', '_', $pageRawPath);
     if (!class_exists($pageClass, false)) {
         $pageFile = str_replace('/', DIRECTORY_SEPARATOR, $pageRawPath) . '.php';
         BEAR_Main::includePage($pageFile);
     }
     if (!class_exists($pageClass, false)) {
         throw new BEAR_Exception("Page class[{$pageClass}] is not exist.");
     }
     $pageConfig = array('resource_id' => $pageClass, 'mode' => BEAR_Page::CONFIG_MODE_RESOURCE);
     $pageOptions = $this->_config['options'];
     if (isset($this->_config['options']['page'])) {
         $pageConfig = array_merge($pageConfig, (array) $this->_config['options']['page']);
     }
     if (isset($pageConfig['ua'])) {
         $pageConfig['enable_ua_sniffing'] = true;
     }
     $page = BEAR::factory($pageClass, $pageConfig, $pageOptions);
     $method = $this->_config['method'] === 'read' ? 'onInit' : 'onAction';
     $args = array_merge($page->getArgs(), $this->_config['values']);
     $cnt = $this->_roPrototye->countStack();
     $page->{$method}($args);
     $cnt = $this->_roPrototye->countStack() - $cnt;
     // リソースモード
     switch (true) {
         // resource
         case !isset($this->_config['options']['output']) || $this->_config['options']['output'] === 'resource':
             $result = $this->_outputResource($page, $cnt);
             break;
             // html
         // html
         case $this->_config['options']['output'] === 'html':
             $result = $this->_outputHtml($page);
             break;
         default:
             $info = array('output option' => $this->_config['options']['output']);
             throw $this->_exception('Unknown page resource options', compact('info'));
             break;
     }
     if (!$result instanceof BEAR_Ro) {
         $result = BEAR::factory('BEAR_Ro', array())->setBody($result);
     }
     return $result;
 }