/** * 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 mixed */ public function getBody($link = false) { if ($link === true) { $result = $this->getLinkedBody(); } else { $this->_doRequest(); $result = $this->_ro->getBody(); } return $result; }
/** * データ送信 * * @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> * リソース取得の結果に対しての後処理を行います。 * 後処理にはコールバック関数の適用や、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(); } }