source: sandbox/expresso-solr/expressoMail1_2/inc/solrclient/library/Solarium/Client/Adapter/ZendHttp.php @ 7576

Revision 7576, 6.0 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
1<?php
2/**
3 * Copyright 2011 Bas de Nooijer. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 *    this listof conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * The views and conclusions contained in the software and documentation are
28 * those of the authors and should not be interpreted as representing official
29 * policies, either expressed or implied, of the copyright holder.
30 *
31 * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
32 * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
33 * @link http://www.solarium-project.org/
34 *
35 * @package Solarium
36 * @subpackage Client
37 */
38
39/**
40 * Adapter that uses a Zend_Http_Client
41 *
42 * The Zend Framework HTTP client has many great features and has lots of
43 * configuration options. For more info see the manual at
44 * {@link http://framework.zend.com/manual/en/zend.http.html}
45 *
46 * To use this adapter you need to have the Zend Framework available (autoloading)
47 *
48 * @package Solarium
49 * @subpackage Client
50 */
51class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter
52{
53
54    /**
55     * Zend Http instance for communication with Solr
56     *
57     * @var Zend_Http_Client
58     */
59    protected $_zendHttp;
60
61    /**
62     * Set options
63     *
64     * Overrides any existing values.
65     *
66     * If the options array has an 'options' entry it is forwarded to the
67     * Zend_Http_Client. See the Zend_Http_Clientdocs for the many config
68     * options available.
69     *
70     * The $options param should be an array or an object that has a toArray
71     * method, like Zend_Config
72     *
73     * @param array|object $options
74     * @param boolean $overwrite
75     * @return Solarium_Client_Adapter_ZendHttp Provides fluent interface
76     */
77    public function setOptions($options, $overwrite = false)
78    {
79        parent::setOptions($options, $overwrite);
80
81        // forward options to zendHttp instance
82        if (null !== $this->_zendHttp) {
83
84            // forward timeout setting
85            $adapterOptions = array('timeout' => $this->getTimeout());
86
87            // forward adapter options if available
88            if (isset($this->_options['options'])) {
89                $adapterOptions = array_merge($adapterOptions, $this->_options['options']);
90            }
91           
92            $this->_zendHttp->setConfig($adapterOptions);
93        }
94
95        return $this;
96    }
97
98    /**
99     * Set the Zend_Http_Client instance
100     *
101     * This method is optional, if you don't set a client it will be created
102     * upon first use, using default and/or custom options (the most common use
103     * case)
104     *
105     * @param Zend_Http_Client $zendHttp
106     * @return Solarium_Client_Adapter_ZendHttp Provides fluent interface
107     */
108    public function setZendHttp($zendHttp)
109    {
110        $this->_zendHttp = $zendHttp;
111        return $this;
112    }
113
114    /**
115     * Get the Zend_Http_Client instance
116     *
117     * If no instance is available yet it will be created automatically based on
118     * options.
119     *
120     * You can use this method to get a reference to the client instance to set
121     * options, get the last response and use many other features offered by the
122     * Zend_Http_Client API.
123     *
124     * @return Zend_Http_Client
125     */
126    public function getZendHttp()
127    {
128        if (null == $this->_zendHttp) {
129            $options = array('timeout' => $this->getOption('timeout'));
130
131            // forward zendhttp options
132            if (isset($this->_options['options'])) {
133                $options = array_merge(
134                    $options,
135                    $this->_options['options']
136                );
137            }
138
139            $this->_zendHttp = new Zend_Http_Client(null, $options);
140        }
141
142        return $this->_zendHttp;
143    }
144
145    /**
146     * Execute a Solr request using the Zend_Http_Client instance
147     *
148     * @param Solarium_Client_Request $request
149     * @return Solarium_Client_Response
150     */
151    public function execute($request)
152    {
153        $client = $this->getZendHttp();
154
155        $client->setMethod($request->getMethod());
156        $client->setUri($this->getBaseUri() . $request->getUri());
157        $client->setHeaders($request->getHeaders());
158        $client->setRawData($request->getRawData());
159
160        $response = $client->request();
161
162        // throw an exception in case of a HTTP error
163        if ($response->isError()) {
164            throw new Solarium_Client_HttpException(
165                $response->getMessage(),
166                $response->getStatus()
167            );
168        }
169
170        if ($request->getMethod() == Solarium_Client_Request::METHOD_HEAD) {
171            $data = '';
172        } else {
173            $data = $response->getBody();
174        }
175
176        // this is used because getHeaders doesn't return the HTTP header...
177        $headers = explode("\n", $response->getHeadersAsString());
178
179        return new Solarium_Client_Response($data, $headers);
180    }
181
182}
Note: See TracBrowser for help on using the repository browser.