app.php

来自「Bug tracker, and reporter.」· PHP 代码 · 共 786 行 · 第 1/2 页

PHP
786
字号
        }        if (is_string($data)) {            $rawData = $data;        } elseif ($data instanceof Zend_Gdata_App_MediaEntry) {            $rawData = $data->encode();            if ($data->getMediaSource() !== null) {                $contentType = 'multipart/related; boundary="' . $data->getBoundary() . '"';                $headers = array('MIME-version' => '1.0');                $extraHeaders['Slug'] = $data->getMediaSource()->getSlug();            } else {                $contentType = 'application/atom+xml';            }        } elseif ($data instanceof Zend_Gdata_App_Entry) {            $rawData = $data->saveXML();            $contentType = 'application/atom+xml';        } elseif ($data instanceof Zend_Gdata_App_MediaSource) {            $rawData = $data->encode();            if ($data->getSlug() !== null) {                $extraHeaders['Slug'] = $data->getSlug();            }            if ($contentType === null) {                $contentType = $data->getContentType();            }        } else {            require_once 'Zend/Gdata/App/InvalidArgumentException.php';            throw new Zend_Gdata_App_InvalidArgumentException(                    'You must specify the data to post as either a string or a child of Zend_Gdata_App_Entry');        }        if ($uri === null) {            $uri = $this->_defaultPostUri;        }        if ($uri === null) {            require_once 'Zend/Gdata/App/InvalidArgumentException.php';            throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI to which to post.');        }        if ($contentType === null) {            $contentType = 'application/atom+xml';        }        $headers = array('x-http-method-override' => null);        if ($extraHeaders !== null) {            $headers = array_merge($headers, $extraHeaders);        }        $this->_httpClient->resetParameters();        $this->_httpClient->setHeaders($headers);        $this->_httpClient->setUri($uri);        $this->_httpClient->setConfig(array('maxredirects' => 0));        $this->_httpClient->setRawData($rawData, $contentType);        try {            $response = $this->_httpClient->request('POST');        } catch (Zend_Http_Client_Exception $e) {            require_once 'Zend/Gdata/App/HttpException.php';            throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);        }        /**         * set "S" cookie to avoid future redirects.         * TEMPORARILY removed until Zend_Http_Client has a method to set         * raw cookie data        if($cookie = $response->getHeader('Set-cookie')) {            list($cookieName, $cookieValue) = explode('=', $cookie, 2);            $this->_httpClient->setCookie($cookieName, $cookieValue);        }         */        if ($response->isRedirect()) {            if ($remainingRedirects > 0) {                $newUri = $response->getHeader('Location');                $response = $this->post($data, $newUri, $remainingRedirects - 1, $contentType, $extraHeaders);            } else {                require_once 'Zend/Gdata/App/HttpException.php';                throw new Zend_Gdata_App_HttpException(                        'No more redirects allowed', null, $response);            }        }        if (!$response->isSuccessful()) {            require_once 'Zend/Gdata/App/HttpException.php';            $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus());            $exception->setResponse($response);            throw $exception;        }        return $response;    }    /**     * PUT data with client object     *     * @param mixed $data The Zend_Gdata_App_Entry or XML to post     * @param string $uri PUT URI     * @param array $headers Additional HTTP headers to insert.     * @param string $contentType Content-type of the data     * @param array $extraHaders Extra headers to add tot he request     * @return Zend_Http_Response     * @throws Zend_Gdata_App_Exception     * @throws Zend_Gdata_App_HttpException     * @throws Zend_Gdata_App_InvalidArgumentException     */    public function put($data, $uri = null, $remainingRedirects = null,            $contentType = null, $extraHeaders = null)    {        require_once 'Zend/Http/Client/Exception.php';        if ($remainingRedirects === null) {            $remainingRedirects = self::getMaxRedirects();        }        if ($extraHeaders === null) {            $extraHeaders = array();        }        if (is_string($data)) {            $rawData = $data;        } elseif ($data instanceof Zend_Gdata_App_MediaEntry) {            $rawData = $data->encode();            if ($data->getMediaSource() !== null) {                $contentType = 'multipart/related; boundary="' . $data->getBoundary() . '"';                $headers = array('MIME-version' => '1.0');                $extraHeaders['Slug'] = $data->getMediaSource()->getSlug();            } else {                $contentType = 'application/atom+xml';            }        } elseif ($data instanceof Zend_Gdata_App_Entry) {            $rawData = $data->saveXML();        } elseif ($data instanceof Zend_Gdata_App_MediaSource) {            $rawData = $data->encode();            if ($data->getSlug() !== null) {                $extraHeaders['Slug'] = $data->getSlug();            }            if ($contentType === null) {                $contentType = $data->getContentType();            }        } else {            require_once 'Zend/Gdata/App/InvalidArgumentException.php';            throw new Zend_Gdata_App_InvalidArgumentException(                    'You must specify the data to post as either a string or a child of Zend_Gdata_App_Entry');        }        if ($uri === null) {            if ($data instanceof Zend_Gdata_App_Entry) {                $editLink = $data->getEditLink();                if ($editLink != null) {                    $uri = $editLink->getHref();                }            }        }        if ($uri === null) {            require_once 'Zend/Gdata/App/InvalidArgumentException.php';            throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI to which to put.');        }        if ($contentType === null) {            $contentType = 'application/atom+xml';        }        $headers = array('x-http-method-override' => null);        if ($extraHeaders !== null) {            $headers = array_merge($headers, $extraHeaders);        }        $this->_httpClient->resetParameters();        $this->_httpClient->setUri($uri);        $this->_httpClient->setConfig(array('maxredirects' => 0));        $this->_httpClient->setRawData($rawData, $contentType);        try {            if (Zend_Gdata_App::getHttpMethodOverride()) {                $this->_httpClient->setHeaders(array('X-HTTP-Method-Override: PUT',                    'Content-Type: ' . $contentType));                $response = $this->_httpClient->request('POST');            } else {                $this->_httpClient->setHeaders('Content-Type', $contentType);                $response = $this->_httpClient->request('PUT');            }        } catch (Zend_Http_Client_Exception $e) {            require_once 'Zend/Gdata/App/HttpException.php';            throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);        }        /**         * set "S" cookie to avoid future redirects.         * TEMPORARILY removed until Zend_Http_Client has a method to set         * raw cookie data        if($cookie = $response->getHeader('Set-cookie')) {            list($cookieName, $cookieValue) = explode('=', $cookie, 2);            $this->_httpClient->setCookie($cookieName, $cookieValue);        }         */        if ($response->isRedirect()) {            if ($remainingRedirects > 0) {                $newUri = $response->getHeader('Location');                $response = $this->put($data, $newUri, $remainingRedirects - 1, $contentType, $extraHeaders);            } else {                require_once 'Zend/Gdata/App/HttpException.php';                throw new Zend_Gdata_App_HttpException(                        'No more redirects allowed', null, $response);            }        }        if (!$response->isSuccessful()) {            require_once 'Zend/Gdata/App/HttpException.php';            $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus());            $exception->setResponse($response);            throw $exception;        }        return $response;    }    /**     * DELETE entry with client object     *     * @param mixed $data The Zend_Gdata_App_Entry or URL to delete     * @return void     * @throws Zend_Gdata_App_Exception     * @throws Zend_Gdata_App_HttpException     * @throws Zend_Gdata_App_InvalidArgumentException     */    public function delete($data, $remainingRedirects = null)    {        require_once 'Zend/Http/Client/Exception.php';        if ($remainingRedirects === null) {            $remainingRedirects = self::getMaxRedirects();        }        if (is_string($data)) {            $uri = $data;        } elseif ($data instanceof Zend_Gdata_App_Entry) {            $editLink = $data->getEditLink();            if ($editLink != null) {                $uri = $editLink->getHref();            }        } else {            require_once 'Zend/Gdata/App/InvalidArgumentException.php';            throw new Zend_Gdata_App_InvalidArgumentException(                    'You must specify the data to post as either a string or a child of Zend_Gdata_App_Entry');        }        if ($uri === null) {            require_once 'Zend/Gdata/App/InvalidArgumentException.php';            throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI which needs deleted.');        }        $this->_httpClient->resetParameters();        $this->_httpClient->setHeaders('x-http-method-override', null);        $this->_httpClient->setUri($uri);        $this->_httpClient->setConfig(array('maxredirects' => 0));        try {            if (Zend_Gdata_App::getHttpMethodOverride()) {                $this->_httpClient->setHeaders(array('X-HTTP-Method-Override: DELETE'));                $this->_httpClient->setRawData('');                $response = $this->_httpClient->request('POST');            } else {                $response = $this->_httpClient->request('DELETE');            }        } catch (Zend_Http_Client_Exception $e) {            require_once 'Zend/Gdata/App/HttpException.php';            throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);        }        if ($response->isRedirect()) {            if ($remainingRedirects > 0) {                $newUri = $response->getHeader('Location');                $response = $this->delete($newUri, $remainingRedirects - 1);            } else {                require_once 'Zend/Gdata/App/HttpException.php';                throw new Zend_Gdata_App_HttpException(                        'No more redirects allowed', null, $response);            }        }        if (!$response->isSuccessful()) {            require_once 'Zend/Gdata/App/HttpException.php';            $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus());            $exception->setResponse($response);            throw $exception;        }        return $response;    }    /**     * Inserts an entry to a given URI and returns the response as a fully formed Entry.     * @param mixed  $data The Zend_Gdata_App_Entry or XML to post     * @param string $uri POST URI     * @param string $className The class of entry to be returned.     * @return Zend_Gdata_App_Entry The entry returned by the service after insertion.     */    public function insertEntry($data, $uri, $className='Zend_Gdata_App_Entry')    {        $response = $this->post($data, $uri);        $returnEntry = new $className($response->getBody());        $returnEntry->setHttpClient(self::getstaticHttpClient());        return $returnEntry;    }    /**     * Update an entry     *     * TODO Determine if App should call Entry to Update or the opposite.     * Suspecect opposite would mkae more sense.  Also, this possibly should     * take an optional URL to override URL used in the entry, or if an     * edit URI/ID is not present in the entry     *     * @param mixed $data Zend_Gdata_App_Entry or XML (w/ID and link rel='edit')     * @return Zend_Gdata_App_Entry The entry returned from the server     * @throws Zend_Gdata_App_Exception     */    public function updateEntry($data, $uri = null, $className = null)    {        // [FIXME] This will blow up if $data is not an instance of        //         Zend_Gdata_App_Entry and $className is null.        //         --tjohns 2007-08-24        if ($className === null && $data instanceof Zend_Gdata_App_Entry) {            $className = get_class($data);        } elseif ($className === null) {            $className = 'Zend_Gdata_App_Entry';        }                $response = $this->put($data, $uri);        $returnEntry = new $className($response->getBody());        $returnEntry->setHttpClient(self::getstaticHttpClient());        return $returnEntry;    }    /**     * Provides a magic factory method to instantiate new objects with     * shorter syntax than would otherwise be required by the Zend Framework     * naming conventions.  For instance, to construct a new     * Zend_Gdata_Calendar_Extension_Color, a developer simply needs to do     * $gCal->newColor().  For this magic constructor, packages are searched     * in the same order as which they appear in the $_registeredPackages     * array     *     * @param string $method The method name being called     * @param array $args The arguments passed to the call     * @throws Zend_Gdata_App_Exception     */    public function __call($method, $args)    {        if (preg_match('/^new(\w+)/', $method, $matches)) {            $class = $matches[1];            $foundClassName = null;            foreach ($this->_registeredPackages as $name) {                 try {                     @Zend_Loader::loadClass("${name}_${class}");                     $foundClassName = "${name}_${class}";                     break;                 } catch (Zend_Exception $e) {                     // package wasn't here- continue searching                 }            }            if ($foundClassName != null) {                $reflectionObj = new ReflectionClass($foundClassName);                return $reflectionObj->newInstanceArgs($args);            } else {                require_once 'Zend/Gdata/App/Exception.php';                throw new Zend_Gdata_App_Exception(                        "Unable to find '${class}' in registered packages");            }        } else {            require_once 'Zend/Gdata/App/Exception.php';            throw new Zend_Gdata_App_Exception("No such method ${method}");        }    }    /**     * Retrieve all entries for a feed, iterating through pages as necessary.     * Be aware that calling this function on a large dataset will take a      * significant amount of time to complete. In some cases this may cause      * execution to timeout without proper precautions in place.     *     * @param $feed The feed to iterate through.     * @return mixed A new feed of the same type as the one originally      *          passed in, containing all relevent entries.     */    public function retrieveAllEntriesForFeed($feed) {        $feedClass = get_class($feed);        $reflectionObj = new ReflectionClass($feedClass);        $result = $reflectionObj->newInstance();        do {            foreach ($feed as $entry) {                $result->addEntry($entry);            }                        $next = $feed->getLink('next');            if ($next !== null) {                $feed = $this->getFeed($next->href, $feedClass);            } else {                $feed = null;            }        }        while ($feed != null);        return $result;    }    /**     * This method enables logging of requests by changing the      * Zend_Http_Client_Adapter used for performing the requests.       * NOTE: This will not work if you have customized the adapter     * already to use a proxy server or other interface.     *      * @param $logfile The logfile to use when logging the requests     */    public function enableRequestDebugLogging($logfile)     {        $this->_httpClient->setConfig(array(            'adapter' => 'Zend_Gdata_App_LoggingHttpClientAdapterSocket',            'logfile' => $logfile            ));    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?