Created
October 26, 2012 21:41
-
-
Save jeanmonod/3961735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Liip\SoapRecorderBundle\Client; | |
class RecordableSoapClient extends \SoapClient | |
{ | |
const FETCHING_LOCAL_ONLY = 1; | |
const FETCHING_LOCAL_FIRST = 2; | |
const FETCHING_REMOTE = 3; | |
protected static $recordCommunications = false; | |
protected static $fetchingMode = FETCHING_REMOTE_ONLY; | |
protected static $requestFolder = null; | |
protected static $responseFolder = null; | |
public static function startRecording() | |
{ | |
self::$recordCommunications = true; | |
} | |
public static function stopRecording() | |
{ | |
self::$recordCommunications = false; | |
} | |
public static function setFetchingMode($fetchingMode) | |
{ | |
if (!in_array(self::FETCHING_LOCAL_FIRST, self::FETCHING_LOCAL_ONLY, self::FETCHING_REMOTE)) { | |
throw new \InvalidArgumentException("You must set the fetching mode with one of the provided constants"); | |
} | |
self::$fetchingMode = $fetchingMode; | |
} | |
function __doRequest($request, $location, $action, $version, $one_way = 0) | |
{ | |
// Generate a local records filename if require | |
if (self::$fetchingMode !== self::FETCHING_REMOTE || self::$recordCommunications){ | |
$requestFile = $this->getRecordRequestFolder().DIRECTORY_SEPARATOR.$this->generateRecordFilename($request); | |
$responseFile = $this->getRecordResponseFolder().DIRECTORY_SEPARATOR.$this->generateRecordFilename($request); | |
} | |
// Handle local request fetching | |
if (self::$fetchingMode === self::FETCHING_LOCAL_ONLY || self::$fetchingMode === self::FETCHING_LOCAL_FIRST) { | |
if (file_exists($responseFile)){ | |
return file_get_contents($responseFile); | |
} | |
elseif (self::$fetchingMode === self::FETCHING_LOCAL_ONLY) { | |
throw new \RuntimeException("Impossible to find a recorded SOAP response for the following request:\n$request"); | |
} | |
} | |
// Process the real SOAP call | |
$response = parent::__doRequest($request, $location, $action, $version, $one_way); | |
// Potentially record the call | |
if (self::$recordCommunications) { | |
file_put_contents($requestFile, $request); | |
file_put_contents($responseFile, $response); | |
} | |
return $response; | |
} | |
protected function generateRecordFilename($request) | |
{ | |
return md5($request).'.xml'; | |
} | |
protected function setRecordFolders($requestFolder, $responseFolder) | |
{ | |
self::$requestFolder = $requestFolder; | |
self::$responseFolder = $responseFolder; | |
} | |
protected function getRecordRequestFolder() | |
{ | |
if (self::$requestFolder === null) { | |
throw new \RuntimeException("You must call RecordableSoapClient::setRecordFolders() before using the recorder"); | |
} | |
return '/Users/dj/Sites/vca/data/swse/requests/'; | |
} | |
protected function getRecordResponseFolder() | |
{ | |
if (self::$responseFolder === null) { | |
throw new \RuntimeException("You must call RecordableSoapClient::setRecordFolders() before using the recorder"); | |
} | |
return '/Users/dj/Sites/vca/data/swse/responses/'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment