1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
<?php
namespace Guzzle\Plugin\Md5;
use Guzzle\Common\Event;
use Guzzle\Http\Message\EntityEnclosingRequestInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CommandContentMd5Plugin implements EventSubscriberInterface
{
protected $contentMd5Param;
protected $validateMd5Param;
public function __construct($contentMd5Param = 'ContentMD5', $validateMd5Param = 'ValidateMD5')
{
$this->contentMd5Param = $contentMd5Param;
$this->validateMd5Param = $validateMd5Param;
}
public static function getSubscribedEvents()
{
return array('command.before_send' => array('onCommandBeforeSend', -255));
}
public function onCommandBeforeSend(Event $event)
{
$command = $event['command'];
$request = $command->getRequest();
if ($request instanceof EntityEnclosingRequestInterface && $request->getBody()
&& $command->getOperation()->hasParam($this->contentMd5Param)) {
if ($command[$this->contentMd5Param] === true) {
if (false !== ($md5 = $request->getBody()->getContentMd5(true, true))) {
$request->setHeader('Content-MD5', $md5);
}
}
}
if ($command[$this->validateMd5Param] === true) {
$request->addSubscriber(new Md5ValidatorPlugin(true, false));
}
}
}