Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter #2

Merged
merged 4 commits into from Feb 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
408 changes: 187 additions & 221 deletions Client.php

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions Client/AdapterInterface.php
@@ -0,0 +1,30 @@
<?php

/**
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
*/
/**
* Adapter interface for requesting the CheddarGetter service
* @category CheddarGetter
* @package CheddarGetter
* @author Christophe Coevoet <stof@notk.org>
* @example example/example.php
*/

interface CheddarGetter_Client_AdapterInterface {

/**
* Execute CheddarGetter API request
*
* @param string $url Url to the API action
* @param string $username Username
* @param string $password Password
* @param array|null $args HTTP post key value pairs
* @return string Body of the response from the CheddarGetter API
* @throws CheddarGetter_Client_Exception
*/
function request($url, $username, $password, array $args = null);

}
92 changes: 92 additions & 0 deletions Client/CurlAdapter.php
@@ -0,0 +1,92 @@
<?php

/**
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
*/
/**
* Adapter implementation based on php-curl for requesting the CheddarGetter service
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
* @author Christophe Coevoet <stof@notk.org>
* @example example/example.php
*/

class CheddarGetter_Client_CurlAdapter implements CheddarGetter_Client_AdapterInterface {

protected $_resource;

/**
* @param resource $resource
* @throws CheddarGetter_Client_Exception Throws an exception if php-curl is not available.
*/
public function __construct($resource = null) {
if (!function_exists('curl_init')) {
throw new CheddarGetter_Client_Exception('The curl extension is not loaded.', CheddarGetter_Client_Exception::USAGE_INVALID);
}

if ($resource && (!is_resource($resource) || get_resource_type($resource) != 'curl')) {
throw new CheddarGetter_Client_Exception('The curl resource is invalid.', CheddarGetter_Client_Exception::USAGE_INVALID);
}

$this->_resource = $resource;
}

/**
* Execute CheddarGetter API request
*
* @param string $url Url to the API action
* @param string $username Username
* @param string $password Password
* @param array|null $args HTTP post key value pairs
* @return string Body of the response from the CheddarGetter API
* @throws CheddarGetter_Client_Exception Throws an exception if the curl session results in an error.
*/
public function request($url, $username, $password, array $args = null) {

if (!$this->_resource) {
$this->_resource = curl_init($url);
$userAgent = (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] . ' - CheddarGetter_Client PHP' : 'CheddarGetter_Client PHP';
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_USERPWD => $username . ':' . $password,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10
);
foreach ($options as $key=>$val) {
curl_setopt($this->_resource, $key, $val);
}
} else {
curl_setopt($this->_resource, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($this->_resource, CURLOPT_HTTPGET, true);
}

if ($args) {
curl_setopt($this->_resource, CURLOPT_POST, true);
curl_setopt($this->_resource, CURLOPT_POSTFIELDS, http_build_query($args, null, '&'));
}

$result = curl_exec($this->_resource);

if ($result === false || curl_error($this->_resource) != '') {
throw new CheddarGetter_Client_Exception('cUrl session resulted in an error: (' . curl_errno($this->_resource) . ')' . curl_error($this->_resource), CheddarGetter_Client_Exception::UNKNOWN);
}

return $result;

}

/**
* @return null|resource
*/
public function getCurlResource() {
return $this->_resource;
}
}
81 changes: 81 additions & 0 deletions Client/ZendAdapter.php
@@ -0,0 +1,81 @@
<?php

/**
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
*/
/**
* Adapter implementation based on Zend_Http_Client for requesting the CheddarGetter service
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
* @author Christophe Coevoet <stof@notk.org>
* @example example/example.php
*/

class CheddarGetter_Client_ZendAdapter implements CheddarGetter_Client_AdapterInterface {

protected $_client;

/**
* @param Zend_Http_Client $client
* @throws CheddarGetter_Client_Exception Throws an exception if Zend_Http_Client is not available.
*/
public function __construct(Zend_Http_Client $client = null) {
if (!class_exists('Zend_Http_Client')) {
throw new CheddarGetter_Client_Exception('The Zend client is not available.', CheddarGetter_Client_Exception::USAGE_INVALID);
}

$this->_client = $client;
}

/**
* Execute CheddarGetter API request
*
* @param string $url Url to the API action
* @param string $username Username
* @param string $password Password
* @param array|null $args HTTP post key value pairs
* @return string Body of the response from the CheddarGetter API
* @throws Zend_Http_Client_Exception A Zend_Http_Client_Exception may be thrown under a number of conditions but most likely if the tcp socket fails to connect.
*/
public function request($url, $username, $password, array $args = null) {

if (!$this->_client) {
$userAgent = (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] . ' - CheddarGetter_Client PHP' : 'CheddarGetter_Client PHP';

$this->_client = new Zend_Http_Client(
$url,
array(
'timeout' => 60,
'useragent' => $userAgent
)
);
} else {
if ($this->_client->getUri() != $url) {
$this->_client->setUri($url);
}
}

$this->_client->setAuth($username, $password);

if ($args) {
$this->_client->setMethod(Zend_Http_Client::POST);
$this->_client->setParameterPost($args);
} else {
$this->_client->setMethod(Zend_Http_Client::GET);
$this->_client->resetParameters();
}

return $this->_client->request()->getBody();

}

/**
* @return null|Zend_Http_Client
*/
public function getClient() {
return $this->_client;
}
}
66 changes: 66 additions & 0 deletions Http/AdapterInterface.php
@@ -0,0 +1,66 @@
<?php

/**
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
*/
/**
* Adapter interface for getting and setting http related data
* @category CheddarGetter
* @package CheddarGetter
* @author Christophe Coevoet <stof@notk.org>
* @example example/example.php
*/

interface CheddarGetter_Http_AdapterInterface {

/**
* Checks whether a cookie exists.
*
* @param string $name Cookie name
* @return boolean
*/
function hasCookie($name);

/**
* Gets the value of a cookie.
*
* @param string $name Cookie name
* @return mixed
*/
function getCookie($name);

/**
* Sets the value of a cookie.
*
* @param string $name Cookie name
* @param string $data Value of the cookie
* @param int $expire
* @param string $path
* @param string $domain
* @param boolean $secure
* @param boolean $httpOnly
*/
function setCookie($name, $data, $expire, $path, $domain, $secure = false, $httpOnly = false);

/**
* Gets a request parameter.
*
* null is returned if the key is not set.
*
* @param string $key
* @return mixed
*/
function getRequestValue($key);

/**
* @return boolean
*/
function hasReferrer();

/**
* @return string
*/
function getReferrer();
}
77 changes: 77 additions & 0 deletions Http/NativeAdapter.php
@@ -0,0 +1,77 @@
<?php

/**
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
*/
/**
* Adapter implementation using PHP superglobals for getting and setting http related data
* @category CheddarGetter
* @package CheddarGetter
* @author Christophe Coevoet <stof@notk.org>
* @example example/example.php
*/

class CheddarGetter_Http_NativeAdapter implements CheddarGetter_Http_AdapterInterface {

/**
* Checks whether a cookie exists.
*
* @param string $name Cookie name
* @return boolean
*/
public function hasCookie($name) {
return isset($_COOKIE[$name]);
}

/**
* Gets the value of a cookie.
*
* @param string $name Cookie name
* @return mixed
*/
public function getCookie($name) {
return $_COOKIE[$name];
}

/**
* Sets the value of a cookie.
*
* @param string $name Cookie name
* @param string $data Value of the cookie
* @param int $expire
* @param string $path
* @param string $domain
* @param boolean $secure
* @param boolean $httpOnly
*/
public function setCookie($name, $data, $expire, $path, $domain, $secure = false, $httpOnly = false) {
if (!headers_sent()) {
// set the cookie
setcookie($name, $data, $expire, $path, $domain, $secure, $httpOnly);
}
}

/**
* @param string $key
* @return mixed
*/
public function getRequestValue($key) {
return isset($_REQUEST[$key]) ? $_REQUEST[$key] : null;
}

/**
* @return boolean
*/
public function hasReferrer() {
return isset($_SERVER['HTTP_REFERER']);
}

/**
* @return string
*/
public function getReferrer() {
return $_SERVER['HTTP_REFERER'];
}
}
34 changes: 34 additions & 0 deletions Http/ZendAdapter.php
@@ -0,0 +1,34 @@
<?php

/**
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
*/
/**
* Adapter implementation using the ZF1 abstraction for getting and setting http related data
* @category CheddarGetter
* @package CheddarGetter
* @author Christophe Coevoet <stof@notk.org>
* @example example/example.php
* @todo use the ZF abstraction for other methods
*/

class CheddarGetter_Http_ZendAdapter extends CheddarGetter_Http_NativeAdapter {

public function __construct() {
if (!class_exists('Zend_Controller_Front')) {
throw new CheddarGetter_Client_Exception('The Zend front controller is not available.', CheddarGetter_Client_Exception::USAGE_INVALID);
}
}

/**
* @param string $key
* @return mixed
*/
public function getRequestValue($key) {
$fc = Zend_Controller_Front::getInstance();

return $fc->getRequest()->getParam($key);
}
}