Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Rest/Client.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Service/Abstract.php';
00026 
00028 require_once 'Zend/Rest/Client/Result.php';
00029 
00031 require_once 'Zend/Uri.php';
00032 
00040 class Zend_Rest_Client extends Zend_Service_Abstract
00041 {
00046     protected $_data = array();
00047 
00052     protected $_uri = null;
00053 
00060     public function __construct($uri = null)
00061     {
00062         if (!empty($uri)) {
00063             $this->setUri($uri);
00064         }
00065     }
00066 
00073     public function setUri($uri)
00074     {
00075         if ($uri instanceof Zend_Uri_Http) {
00076             $this->_uri = $uri;
00077         } else {
00078             $this->_uri = Zend_Uri::factory($uri);
00079         }
00080 
00081         return $this;
00082     }
00083 
00089     public function getUri()
00090     {
00091         return $this->_uri;
00092     }
00093 
00101     final private function _prepareRest($path)
00102     {
00103         // Get the URI object and configure it
00104         if (!$this->_uri instanceof Zend_Uri_Http) {
00105             require_once 'Zend/Rest/Client/Exception.php';
00106             throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
00107         }
00108 
00109         $uri = $this->_uri->getUri();
00110 
00111         if ($path[0] != '/' && $uri[strlen($uri)-1] != '/') {
00112             $path = '/' . $path;
00113         }
00114 
00115         $this->_uri->setPath($path);
00116 
00121         self::getHttpClient()->resetParameters()->setUri($this->_uri);
00122     }
00123 
00132     final public function restGet($path, array $query = null)
00133     {
00134         $this->_prepareRest($path);
00135         $client = self::getHttpClient();
00136         $client->setParameterGet($query);
00137         return $client->request('GET');
00138     }
00139 
00151     protected function _performPost($method, $data = null)
00152     {
00153         $client = self::getHttpClient();
00154         if (is_string($data)) {
00155             $client->setRawData($data);
00156         } elseif (is_array($data) || is_object($data)) {
00157             $client->setParameterPost((array) $data);
00158         }
00159         return $client->request($method);
00160     }
00161 
00170     final public function restPost($path, $data = null)
00171     {
00172         $this->_prepareRest($path);
00173         return $this->_performPost('POST', $data);
00174     }
00175 
00184     final public function restPut($path, $data = null)
00185     {
00186         $this->_prepareRest($path);
00187         return $this->_performPost('PUT', $data);
00188     }
00189 
00197     final public function restDelete($path)
00198     {
00199         $this->_prepareRest($path);
00200         return self::getHttpClient()->request('DELETE');
00201     }
00202 
00224     public function __call($method, $args)
00225     {
00226         $methods = array('post', 'get', 'delete', 'put');
00227 
00228         if (in_array(strtolower($method), $methods)) {
00229             if (!isset($args[0])) {
00230                 $args[0] = $this->_uri->getPath();
00231             }
00232             $this->_data['rest'] = 1;
00233             $data = array_slice($args, 1) + $this->_data;
00234             $response = $this->{'rest' . $method}($args[0], $data);
00235             $this->_data = array();//Initializes for next Rest method.
00236             return new Zend_Rest_Client_Result($response->getBody());
00237         } else {
00238             // More than one arg means it's definitely a Zend_Rest_Server
00239             if (sizeof($args) == 1) {
00240                 // Uses first called function name as method name
00241                 if (!isset($this->_data['method'])) {
00242                     $this->_data['method'] = $method;
00243                     $this->_data['arg1']  = $args[0];
00244                 }
00245                 $this->_data[$method]  = $args[0];
00246             } else {
00247                 $this->_data['method'] = $method;
00248                 if (sizeof($args) > 0) {
00249                     foreach ($args as $key => $arg) {
00250                         $key = 'arg' . $key;
00251                         $this->_data[$key] = $arg;
00252                     }
00253                 }
00254             }
00255             return $this;
00256         }
00257     }
00258 }
 All Data Structures Namespaces Files Functions Variables Enumerations