/** * リソースのリンクをつなげる * * リソースをリンクした結果をHEADER_LINK_BODYヘッダーに付加します * * @return array */ private function _chainLink(BEAR_Ro $rootRo, array $chain) { $config = $rootRo->getConfig(); $this->_chain = $chain; $hasMultiLink = $hasMultiLink2 = false; $class = get_class($rootRo); assert(method_exists($class, 'onLink')); // ルートリソース $firstUri = strtolower(str_replace(DIRECTORY_SEPARATOR, '_', $config['uri'])); $link = $body = $rootRo->getBody(); $isCollection = count($body) !== count($body, COUNT_RECURSIVE); if ($isCollection === true) { $sourceBody = $body; self::_makeCollectionChain($sourceBody, $firstUri, $rootRo); $linkFrom = $firstUri; $linked = array($firstUri => $sourceBody); $linked[$firstUri] = $sourceBody; $body = $sourceBody; $isCollectionRoot = true; } else { $body = $sourceBody = $body; $linked = array($firstUri => $sourceBody); $isCollectionRoot = false; } $ro = $rootRo; // foreach ($this->_chain as $links) { foreach ($links as $link) { if ($hasMultiLink2 === true || $isCollectionRoot) { self::_changeRecursive($linked, $linkFrom, $link); } else { $onLinks = $ro->onLink($sourceBody); $config = self::_makeRequestConfig($onLinks, $link); $linkRo = BEAR::factory('BEAR_Resource_Request', $config)->request(); $links = $linkRo->getLinks(); if (isset($links['pager'])) { $this->_links['pager'] = $links['pager']; } $body = $linkRo->getBody(); $isCollection = count($body) != count($body, COUNT_RECURSIVE); if ($isCollection) { self::_makeCollectionChain($body, $link, $linkRo); $hasMultiLink = true; $linkFrom = $link; } $linked[$link] = $body; } } $linkFrom = $link; $sourceBody = $body; $hasMultiLink2 = $hasMultiLink ? true : false; $ro = $linkRo; } $this->_cleanUpLink($linked); return $linked; }
/** * Constructor * * @param BEAR_Ro $ro */ public function __construct(BEAR_Ro $ro) { /* @var $ro BEAR_Ro */ $this->code = $ro->getCode(); $this->header = $ro->getHeaders(); $this->body = $ro->getBody(); $this->links = $ro->getLinks(); $this->html = $ro->getHtml(); }
/** * デバック表示描画 * * @return BEAR_Ro * @ignore */ protected function _p() { $trace = debug_backtrace(); $trace = $trace[1]; $place = " in <span style=\"color: gray;\">{$trace['file']}</span> on line {$trace['line']} "; if ($this->hasChainLink()) { $linkBody = $this->getLinkedBody(); } else { $body = $this->getBody(); } $headers = $this->_ro->getHeaders(); $request = $headers['_request']; foreach ($headers as $key => $header) { if (substr($key, 0, 1) == '_') { unset($headers[$key]); } } $linkLabel = ''; if ($this->_chainLink) { foreach ($this->_chainLink as $links) { $linkLabel .= '->' . (count($links) > 1 ? '(' . implode(',', $links) . ')' : $links[0]); } } $label = $this->_config['uri'] . $linkLabel; $labelField = '<fieldset style="color:#4F5155; border:1px solid black;padding:2px;width:10px;">'; $labelField .= '<legend style="color:black;font-size:9pt;font-weight:bold;font-family:sans-serif;">' . $label . '</legend>'; echo $labelField; if (isset($linkBody)) { print_a($linkBody); } else { $resource = array('code' => $this->getCode(), 'header' => $headers, 'body' => $this->getBody(), 'link' => $this->getLinks()); print_a($resource); } echo '</fieldset>'; $linkLabel = $linkLabel ? ' and link(s)' : ''; echo "by \"{$request}\"{$linkLabel} {$place}<br /><br />"; return $this; }
/** * Constructor */ public function __construct($config) { parent::__construct($config); }
/** * ページリソースをHTTP出力します * * @param BEAR_Ro $ro * * @return void */ protected function _outputHttp(BEAR_Ro $ro) { if ($this->_config['mode'] === self::CONFIG_MODE_HTML) { $ro->outputHttp(); } }
/** * データ送信 * * @param int $clientId クライアントID * @param string $method リクエストメソッド名 * @param BEAR_Ro $ro RO * * @return void */ public function sendData($clientId, $method, BEAR_Ro $ro) { $code = $ro->getCode(); $hearders = $ro->getHeaders(); if (isset($hearders['broadcast'])) { $this->_server->broadcastData($code . PHP_EOL, array($clientId)); $this->_server->broadcastData('Content-Type: text/php' . PHP_EOL, array($clientId)); $type = 'X-Socket-Type: broadcast' . PHP_EOL; $this->_server->broadcastData($type, array($clientId)); $this->_server->broadcastData(PHP_EOL, array($clientId)); $data = serialize($hearders['broadcast']) . PHP_EOL; $this->_server->broadcastData($data, array($clientId)); } $this->_server->sendData($clientId, $code . PHP_EOL); $this->_server->sendData($clientId, 'Content-Type: text/php' . PHP_EOL); $this->_server->sendData($clientId, PHP_EOL); $data = $ro->getBody(); $this->_server->sendData($clientId, serialize($data) . PHP_EOL); }
/** * ページャー情報の取得 * * <pre>ページャーで生成されたナビゲーションHTMLを取得します。 * DBページャーの場合はページャー情報も取得されます * ['links']にナビゲーションリンクが * ['info']のページャー情報がはいります * * DBページャーの場合は結果がリソースオブジェクトになっているので、 * リンクとメタ情報からページャー情報を生成しています</pre> * * @return array */ public function getPager() { $links = $this->_ro->getLinks(); $result = array('links' => $links['pager'], 'info' => $this->_ro->getHeaders()); return $result; }
/** * リソース後処理 * * <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); }
/** * リソースのデバック表示 * * firePHPコンソールにリソースを表示します。 * * @param BEAR_Ro $ro リソースオブジェクト * * @return void */ public function debugShowResource(BEAR_Ro $ro) { $config = $ro->getConfig(); if (!isset($config['method'])) { return; } $labelUri = "[resource] {$config['method']} {$config['uri']}"; $labelUri .= $config['values'] ? '?' . http_build_query($config['values']) : ""; $body = $ro->getBody(); if (is_array($body) && isset($body[0]) && is_array($body[0])) { // bodyが表構造と仮定 $table = array(); $table[] = array_values(array_keys($body[0])); foreach ((array) $body as $key => $val) { $table[] = array_values((array) $val); } FB::table($labelUri, $table); } else { FB::group("{$labelUri}", array('Collapsed' => true)); FB::log($body); FB::groupEnd(); } }