* @license http://github.com/basdenooijer/solarium/raw/master/COPYING * @link http://www.solarium-project.org/ * * @package Solarium * @subpackage Client */ /** * Adapter that uses a Zend_Http_Client * * The Zend Framework HTTP client has many great features and has lots of * configuration options. For more info see the manual at * {@link http://framework.zend.com/manual/en/zend.http.html} * * To use this adapter you need to have the Zend Framework available (autoloading) * * @package Solarium * @subpackage Client */ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter { /** * Zend Http instance for communication with Solr * * @var Zend_Http_Client */ protected $_zendHttp; /** * Set options * * Overrides any existing values. * * If the options array has an 'options' entry it is forwarded to the * Zend_Http_Client. See the Zend_Http_Clientdocs for the many config * options available. * * The $options param should be an array or an object that has a toArray * method, like Zend_Config * * @param array|object $options * @param boolean $overwrite * @return Solarium_Client_Adapter_ZendHttp Provides fluent interface */ public function setOptions($options, $overwrite = false) { parent::setOptions($options, $overwrite); // forward options to zendHttp instance if (null !== $this->_zendHttp) { // forward timeout setting $adapterOptions = array('timeout' => $this->getTimeout()); // forward adapter options if available if (isset($this->_options['options'])) { $adapterOptions = array_merge($adapterOptions, $this->_options['options']); } $this->_zendHttp->setConfig($adapterOptions); } return $this; } /** * Set the Zend_Http_Client instance * * This method is optional, if you don't set a client it will be created * upon first use, using default and/or custom options (the most common use * case) * * @param Zend_Http_Client $zendHttp * @return Solarium_Client_Adapter_ZendHttp Provides fluent interface */ public function setZendHttp($zendHttp) { $this->_zendHttp = $zendHttp; return $this; } /** * Get the Zend_Http_Client instance * * If no instance is available yet it will be created automatically based on * options. * * You can use this method to get a reference to the client instance to set * options, get the last response and use many other features offered by the * Zend_Http_Client API. * * @return Zend_Http_Client */ public function getZendHttp() { if (null == $this->_zendHttp) { $options = array('timeout' => $this->getOption('timeout')); // forward zendhttp options if (isset($this->_options['options'])) { $options = array_merge( $options, $this->_options['options'] ); } $this->_zendHttp = new Zend_Http_Client(null, $options); } return $this->_zendHttp; } /** * Execute a Solr request using the Zend_Http_Client instance * * @param Solarium_Client_Request $request * @return Solarium_Client_Response */ public function execute($request) { $client = $this->getZendHttp(); $client->setMethod($request->getMethod()); $client->setUri($this->getBaseUri() . $request->getUri()); $client->setHeaders($request->getHeaders()); $client->setRawData($request->getRawData()); $response = $client->request(); // throw an exception in case of a HTTP error if ($response->isError()) { throw new Solarium_Client_HttpException( $response->getMessage(), $response->getStatus() ); } if ($request->getMethod() == Solarium_Client_Request::METHOD_HEAD) { $data = ''; } else { $data = $response->getBody(); } // this is used because getHeaders doesn't return the HTTP header... $headers = explode("\n", $response->getHeadersAsString()); return new Solarium_Client_Response($data, $headers); } }