[7588] | 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 | */ |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * CustomizeRequest plugin |
---|
| 40 | * |
---|
| 41 | * You can use this plugin to customize the requests generated for Solarium queries by adding or overwriting |
---|
| 42 | * params and/or headers. |
---|
| 43 | * |
---|
| 44 | * @package Solarium |
---|
| 45 | * @subpackage Plugin |
---|
| 46 | */ |
---|
| 47 | class Solarium_Plugin_CustomizeRequest extends Solarium_Plugin_Abstract |
---|
| 48 | { |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Holds customizations added to this plugin |
---|
| 52 | * |
---|
| 53 | * @var array |
---|
| 54 | */ |
---|
| 55 | protected $_customizations = array(); |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * Initialize options |
---|
| 59 | * |
---|
| 60 | * @return void |
---|
| 61 | */ |
---|
| 62 | protected function _init() |
---|
| 63 | { |
---|
| 64 | foreach ($this->_options AS $name => $value) { |
---|
| 65 | switch ($name) { |
---|
| 66 | case 'customization': |
---|
| 67 | $this->addCustomizations($value); |
---|
| 68 | break; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Create a Customization instance |
---|
| 75 | * |
---|
| 76 | * If you supply a string as the first arguments ($options) it will be used as the key for the Customization |
---|
| 77 | * and it will be added to this plugin. |
---|
| 78 | * If you supply an options array/object that contains a key the Customization will also be added to the plugin. |
---|
| 79 | * |
---|
| 80 | * When no key is supplied the Customization cannot be added, in that case you will need to add it manually |
---|
| 81 | * after setting the key, by using the addCustomization method. |
---|
| 82 | * |
---|
| 83 | * @param mixed $options |
---|
| 84 | * @return Solarium_Plugin_CustomizeRequest_Customization |
---|
| 85 | */ |
---|
| 86 | public function createCustomization($options = null) |
---|
| 87 | { |
---|
| 88 | if (is_string($options)) { |
---|
| 89 | $fq = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
| 90 | $fq->setKey($options); |
---|
| 91 | } else { |
---|
| 92 | $fq = new Solarium_Plugin_CustomizeRequest_Customization($options); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | if ($fq->getKey() !== null) { |
---|
| 96 | $this->addCustomization($fq); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | return $fq; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * Add a customization |
---|
| 104 | * |
---|
| 105 | * Supports a Customization instance or a config array, in that case a new |
---|
| 106 | * Customization instance wil be created based on the options. |
---|
| 107 | * |
---|
| 108 | * @param Solarium_Plugin_CustomizeRequest_Customization|array $customization |
---|
| 109 | * @return Solarium_Plugin_CustomizeRequest Provides fluent interface |
---|
| 110 | */ |
---|
| 111 | public function addCustomization($customization) |
---|
| 112 | { |
---|
| 113 | if (is_array($customization)) { |
---|
| 114 | $customization = new Solarium_Plugin_CustomizeRequest_Customization($customization); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | $key = $customization->getKey(); |
---|
| 118 | |
---|
| 119 | // check for non-empty key |
---|
| 120 | if (0 === strlen($key)) { |
---|
| 121 | throw new Solarium_Exception('A Customization must have a key value'); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // check for a unique key |
---|
| 125 | if (array_key_exists($key, $this->_customizations)) { |
---|
| 126 | //double add calls for the same customization are ignored, others cause an exception |
---|
| 127 | if ($this->_customizations[$key] !== $customization) { |
---|
| 128 | throw new Solarium_Exception('A Customization must have a unique key value'); |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | $this->_customizations[$key] = $customization; |
---|
| 133 | |
---|
| 134 | return $this; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Add multiple Customizations |
---|
| 139 | * |
---|
| 140 | * @param array $customizations |
---|
| 141 | * @return Solarium_Plugin_CustomizeRequest Provides fluent interface |
---|
| 142 | */ |
---|
| 143 | public function addCustomizations(array $customizations) |
---|
| 144 | { |
---|
| 145 | foreach ($customizations AS $key => $customization) { |
---|
| 146 | |
---|
| 147 | // in case of a config array: add key to config |
---|
| 148 | if (is_array($customization) && !isset($customization['key'])) { |
---|
| 149 | $customization['key'] = $key; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | $this->addCustomization($customization); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | return $this; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * Get a Customization |
---|
| 160 | * |
---|
| 161 | * @param string $key |
---|
| 162 | * @return string |
---|
| 163 | */ |
---|
| 164 | public function getCustomization($key) |
---|
| 165 | { |
---|
| 166 | if (isset($this->_customizations[$key])) { |
---|
| 167 | return $this->_customizations[$key]; |
---|
| 168 | } else { |
---|
| 169 | return null; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Get all Customizations |
---|
| 175 | * |
---|
| 176 | * @return array |
---|
| 177 | */ |
---|
| 178 | public function getCustomizations() |
---|
| 179 | { |
---|
| 180 | return $this->_customizations; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Remove a single Customization |
---|
| 185 | * |
---|
| 186 | * You can remove a Customization by passing it's key, or by passing the Customization instance |
---|
| 187 | * |
---|
| 188 | * @param string|Solarium_Plugin_CustomizeRequest_Customization $customization |
---|
| 189 | * @return Solarium_Plugin_CustomizeRequest Provides fluent interface |
---|
| 190 | */ |
---|
| 191 | public function removeCustomization($customization) |
---|
| 192 | { |
---|
| 193 | if (is_object($customization)) { |
---|
| 194 | $customization = $customization->getKey(); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | if (isset($this->_customizations[$customization])) { |
---|
| 198 | unset($this->_customizations[$customization]); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | return $this; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * Remove all Customizations |
---|
| 206 | * |
---|
| 207 | * @return Solarium_Plugin_CustomizeRequest Provides fluent interface |
---|
| 208 | */ |
---|
| 209 | public function clearCustomizations() |
---|
| 210 | { |
---|
| 211 | $this->_customizations = array(); |
---|
| 212 | return $this; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Set multiple Customizations |
---|
| 217 | * |
---|
| 218 | * This overwrites any existing Customizations |
---|
| 219 | * |
---|
| 220 | * @param array $customizations |
---|
| 221 | */ |
---|
| 222 | public function setCustomizations($customizations) |
---|
| 223 | { |
---|
| 224 | $this->clearCustomizations(); |
---|
| 225 | $this->addCustomizations($customizations); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * Event hook to customize the request object |
---|
| 230 | * |
---|
| 231 | * @param Solarium_Query $query |
---|
| 232 | * @param Solarium_Client_Request $request |
---|
| 233 | * @return void |
---|
| 234 | */ |
---|
| 235 | public function postCreateRequest($query, $request) |
---|
| 236 | { |
---|
| 237 | foreach ($this->getCustomizations() as $key => $customization) { |
---|
| 238 | |
---|
| 239 | // first validate |
---|
| 240 | if (!$customization->isValid()) { |
---|
| 241 | throw new Solarium_Exception('Request customization with key "' . $key . '" is invalid'); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | // apply to request, depending on type |
---|
| 245 | switch ($customization->getType()) { |
---|
| 246 | case Solarium_Plugin_CustomizeRequest_Customization::TYPE_PARAM: |
---|
| 247 | $request->addParam( |
---|
| 248 | $customization->getName(), |
---|
| 249 | $customization->getValue(), |
---|
| 250 | $customization->getOverwrite() |
---|
| 251 | ); |
---|
| 252 | break; |
---|
| 253 | case Solarium_Plugin_CustomizeRequest_Customization::TYPE_HEADER: |
---|
| 254 | $request->addHeader($customization->getName() . ': ' . $customization->getValue()); |
---|
| 255 | break; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // remove single-use customizations after use |
---|
| 259 | if (!$customization->getPersistent()) { |
---|
| 260 | $this->removeCustomization($key); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | } |
---|