summaryrefslogtreecommitdiff
path: root/inc/mailgun/php-http/message/src/Formatter
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2017-03-02 22:49:24 -0500
committerCarson Fleming <cflems@cflems.net>2017-03-02 22:49:24 -0500
commitb76e2ff898b23745d4c9aaee49eeb7d88f2896ab (patch)
tree9b794be8db310a575d70165d9ebde0a183b61b01 /inc/mailgun/php-http/message/src/Formatter
parentbfcc9f7a7656a2db0c905b3c13114664f00f6c37 (diff)
downloadbulletin-b76e2ff898b23745d4c9aaee49eeb7d88f2896ab.tar.gz
Updated mailgun plugin
Diffstat (limited to 'inc/mailgun/php-http/message/src/Formatter')
-rw-r--r--inc/mailgun/php-http/message/src/Formatter/CurlCommandFormatter.php80
-rw-r--r--inc/mailgun/php-http/message/src/Formatter/FullHttpMessageFormatter.php91
-rw-r--r--inc/mailgun/php-http/message/src/Formatter/SimpleFormatter.php42
3 files changed, 213 insertions, 0 deletions
diff --git a/inc/mailgun/php-http/message/src/Formatter/CurlCommandFormatter.php b/inc/mailgun/php-http/message/src/Formatter/CurlCommandFormatter.php
new file mode 100644
index 0000000..5364ccc
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Formatter/CurlCommandFormatter.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Http\Message\Formatter;
+
+use Http\Message\Formatter;
+use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\ResponseInterface;
+
+/**
+ * A formatter that prints a cURL command for HTTP requests.
+ *
+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
+ */
+class CurlCommandFormatter implements Formatter
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function formatRequest(RequestInterface $request)
+ {
+ $command = sprintf('curl %s', escapeshellarg((string) $request->getUri()->withFragment('')));
+ if ($request->getProtocolVersion() === '1.0') {
+ $command .= ' --http1.0';
+ } elseif ($request->getProtocolVersion() === '2.0') {
+ $command .= ' --http2';
+ }
+
+ $method = strtoupper($request->getMethod());
+ if ('HEAD' === $method) {
+ $command .= ' --head';
+ } elseif ('GET' !== $method) {
+ $command .= ' --request '.$method;
+ }
+
+ $command .= $this->getHeadersAsCommandOptions($request);
+
+ $body = $request->getBody();
+ if ($body->getSize() > 0) {
+ if (!$body->isSeekable()) {
+ return 'Cant format Request as cUrl command if body stream is not seekable.';
+ }
+ $command .= sprintf(' --data %s', escapeshellarg($body->__toString()));
+ $body->rewind();
+ }
+
+ return $command;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function formatResponse(ResponseInterface $response)
+ {
+ return '';
+ }
+
+ /**
+ * @param RequestInterface $request
+ *
+ * @return string
+ */
+ private function getHeadersAsCommandOptions(RequestInterface $request)
+ {
+ $command = '';
+ foreach ($request->getHeaders() as $name => $values) {
+ if ('host' === strtolower($name) && $values[0] === $request->getUri()->getHost()) {
+ continue;
+ }
+
+ if ('user-agent' === strtolower($name)) {
+ $command .= sprintf('-A %s', escapeshellarg($values[0]));
+ continue;
+ }
+
+ $command .= sprintf(' -H %s', escapeshellarg($name.': '.$request->getHeaderLine($name)));
+ }
+
+ return $command;
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Formatter/FullHttpMessageFormatter.php b/inc/mailgun/php-http/message/src/Formatter/FullHttpMessageFormatter.php
new file mode 100644
index 0000000..3fa1029
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Formatter/FullHttpMessageFormatter.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Http\Message\Formatter;
+
+use Http\Message\Formatter;
+use Psr\Http\Message\MessageInterface;
+use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\ResponseInterface;
+
+/**
+ * A formatter that prints the complete HTTP message.
+ *
+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
+ */
+class FullHttpMessageFormatter implements Formatter
+{
+ /**
+ * The maximum length of the body.
+ *
+ * @var int
+ */
+ private $maxBodyLength;
+
+ /**
+ * @param int $maxBodyLength
+ */
+ public function __construct($maxBodyLength = 1000)
+ {
+ $this->maxBodyLength = $maxBodyLength;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function formatRequest(RequestInterface $request)
+ {
+ $message = sprintf(
+ "%s %s HTTP/%s\n",
+ $request->getMethod(),
+ $request->getRequestTarget(),
+ $request->getProtocolVersion()
+ );
+
+ foreach ($request->getHeaders() as $name => $values) {
+ $message .= $name.': '.implode(', ', $values)."\n";
+ }
+
+ return $this->addBody($request, $message);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function formatResponse(ResponseInterface $response)
+ {
+ $message = sprintf(
+ "HTTP/%s %s %s\n",
+ $response->getProtocolVersion(),
+ $response->getStatusCode(),
+ $response->getReasonPhrase()
+ );
+
+ foreach ($response->getHeaders() as $name => $values) {
+ $message .= $name.': '.implode(', ', $values)."\n";
+ }
+
+ return $this->addBody($response, $message);
+ }
+
+ /**
+ * Add the message body if the stream is seekable.
+ *
+ * @param MessageInterface $request
+ * @param string $message
+ *
+ * @return string
+ */
+ private function addBody(MessageInterface $request, $message)
+ {
+ $stream = $request->getBody();
+ if (!$stream->isSeekable() || $this->maxBodyLength === 0) {
+ // Do not read the stream
+ $message .= "\n";
+ } else {
+ $message .= "\n".mb_substr($stream->__toString(), 0, $this->maxBodyLength);
+ $stream->rewind();
+ }
+
+ return $message;
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Formatter/SimpleFormatter.php b/inc/mailgun/php-http/message/src/Formatter/SimpleFormatter.php
new file mode 100644
index 0000000..b1fcabd
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Formatter/SimpleFormatter.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Http\Message\Formatter;
+
+use Http\Message\Formatter;
+use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\ResponseInterface;
+
+/**
+ * Normalize a request or a response into a string or an array.
+ *
+ * @author Joel Wurtz <joel.wurtz@gmail.com>
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+class SimpleFormatter implements Formatter
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function formatRequest(RequestInterface $request)
+ {
+ return sprintf(
+ '%s %s %s',
+ $request->getMethod(),
+ $request->getUri()->__toString(),
+ $request->getProtocolVersion()
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function formatResponse(ResponseInterface $response)
+ {
+ return sprintf(
+ '%s %s %s',
+ $response->getStatusCode(),
+ $response->getReasonPhrase(),
+ $response->getProtocolVersion()
+ );
+ }
+}