/** * リソーステンプレートをRoにセット * * リクエストにテンプレートオプションが指定されているとHTML等文字列化してRoに保持します。 * * @return void * @todo リソースボディのキャッシュはUA共通に */ protected function _setHtml($isLinked) { // キャッシュ? $isLinkCache = isset($this->_config['request']['options']['cache']['link']) && $this->_config['request']['options']['cache']['link']; if ($isLinked && $isLinkCache === true) { $life = $this->_config['request']['options']['cache']['life']; } elseif (!$isLinked && isset($this->_config['request']['options']['cache']['life']) && isset($this->_config['request']['options']['template'])) { $life = $this->_config['request']['options']['cache']['life']; } else { $life = false; } if ($life !== false) { // キャッシュ読み込み $cache = BEAR::dependency('BEAR_Cache')->setLife($life); $pagerKey = isset($_GET['_start']) ? $_GET['_start'] : ''; $ua = BEAR::get('page')->getConfig('ua'); $cacheKey = $ua . md5(serialize($this->_config['request']) . "-{$pagerKey}"); $saved = $cache->get($cacheKey); if ($saved) { $this->_ro = $saved; return; } else { $useCache = true; } } //実リクエスト $body = $isLinked !== true ? $this->_ro->getBody() : $this->getLinkedBody(); $this->_ro->setBody($body); if ($isLinked === true) { $this->_ro->setHeader('_linked', $this->_chainLink); } // テンプレート適用 $html = isset($this->_config['request']['options']['template']) ? $this->_getHtml($body) : false; if ($html !== false) { $this->_ro->setHtml($html); } // キャッシュ書き込み if (isset($useCache)) { $roContainer = new BEAR_Ro_Container($this->_ro); $cache->set($cacheKey, $roContainer); } }
/** * リソース後処理 * * <pre> * リソース取得の結果に対しての後処理を行います。 * 後処理にはコールバック関数の適用や、DBページャーの * 結果取り出しなどがあります。 * </pre> * * @param BEAR_Ro &$ro BEAR_Roオブジェクト * * @return mixed * * @throws BEAR_Resource_Execute_Exception */ private function _actionPostProcess(BEAR_Ro &$ro) { $body = $ro->getBody(); $Info = array(); $info['totalItems'] = count($body); $options = $this->_config['options']; // ページャーリザルト処理 if (PEAR::isError($body) || !$body) { return; } // ページャーオプション if (isset($options[BEAR_Resource::OPTION_PAGER])) { $pager = BEAR::factory('BEAR_Pager'); $pagerOptions['perPage'] = $options[BEAR_Resource::OPTION_PAGER]; $pager->setOptions($pagerOptions); $pager->makePager($body); $body = $pager->getResult(); $info['page_numbers'] = array('current' => $pager->pager->getCurrentPageID(), 'total' => $pager->pager->numPages()); list($info['from'], $info['to']) = $pager->pager->getOffsetByPageId(); $links = $pager->getLinks(); $ro->setLink(BEAR_Resource::LINK_PAGER, $links); $ro->setHeaders($info); $info['page_numbers'] = array('current' => $pager->pager->getCurrentPageID(), 'total' => $pager->pager->numPages()); list($info['from'], $info['to']) = $pager->pager->getOffsetByPageId(); $info['limit'] = $info['to'] - $info['from'] + 1; $pager->setPagerLinks($links, $info); } // コールバックオプション 1 if (isset($options['callback'])) { if (is_callable($options['callback'])) { call_user_func($options['callback'], $body); } else { $msg = 'BEAR_Resource callback failed.'; $info = array('callback' => $options['callback']); throw $this->_exception($msg, array('info' => $info)); } } // コールバックオプション rec if (isset($options['callbackr'])) { if (is_callable($options['callbackr'])) { array_walk_recursive($body, $options['callbackr']); } else { $msg = 'BEAR_Resource callback_r failed.'; $info = array('callbackr' => $options['callback_r']); throw $this->_exception($msg, array('info' => $info)); } } $ro->setBody($body); }