From 43945a872152294b6672aeaa5351056e7ccb4618 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Fri, 15 Jun 2018 22:54:00 +0200 Subject: [PATCH] Composer > do not require symfony/http-foundation package Miscellaneous > remove getCurlResponseWithHeaders() method --- composer.json | 3 +- src/Utilities/Miscellaneous.php | 141 -------------------------- tests/Utilities/MiscellaneousTest.php | 39 ------- 3 files changed, 1 insertion(+), 182 deletions(-) diff --git a/composer.json b/composer.json index 10278cf..d1fc9df 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ "require": { "php": ">=5.5.9", "doctrine/orm": "^2.5", - "gedmo/doctrine-extensions": "^2.4", - "symfony/http-foundation": "^3.3" + "gedmo/doctrine-extensions": "^2.4" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.2", diff --git a/src/Utilities/Miscellaneous.php b/src/Utilities/Miscellaneous.php index 2146a29..da612e4 100644 --- a/src/Utilities/Miscellaneous.php +++ b/src/Utilities/Miscellaneous.php @@ -11,7 +11,6 @@ namespace Meritoo\Common\Utilities; use Gedmo\Sluggable\Util\Urlizer; use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException; use Meritoo\Common\Exception\Regex\InvalidColorHexValueException; -use Symfony\Component\HttpFoundation\Cookie; use Transliterator; /** @@ -1205,146 +1204,6 @@ class Miscellaneous return $value; } - /** - * Returns a CURL response with parsed HTTP headers as array with "headers", "cookies" and "content" keys - * - * The headers and cookies are parsed and returned as an array, and an array of Cookie objects. Returned array looks - * like this example: - * - * [ - * 'headers' => [ - * 'Content-Type' => 'text/html; charset=UTF-8', - * ... - * ], - * 'cookies' => [ - * new Symfony\Component\HttpFoundation\Cookie(), - * new Symfony\Component\HttpFoundation\Cookie(), - * ... - * ], - * 'content' => '...' - * ] - * - * - * If you want to attach HTTP headers into response content by CURL you need to set "CURLOPT_HEADER" option - * to "true". To read exact length of HTTP headers from CURL you can use "curl_getinfo()" function - * and read "CURLINFO_HEADER_SIZE" option. - * - * @param string $response Full content of response, including HTTP headers - * @param int $headerSize Length of HTTP headers in content - * @return array - */ - public static function getCurlResponseWithHeaders($response, $headerSize) - { - $headers = []; - $cookies = []; - - $headerContent = ''; - $content = ''; - - if (0 < $headerSize) { - $headerContent = mb_substr($response, 0, $headerSize); - $content = mb_substr($response, $headerSize); - } - - /* - * Let's transform headers content into two arrays: headers and cookies - */ - if (!empty($headerContent)) { - foreach (explode("\r\n", $headerContent) as $i => $line) { - /* - * First line is only HTTP status and is unneeded so skip it - */ - if (0 === $i) { - continue; - } - - if (Regex::contains($line, ':')) { - list($key, $value) = explode(': ', $line); - - /* - * If the header is a "set-cookie" let's save it to "cookies" array - */ - if ('Set-Cookie' === $key) { - $cookieParameters = explode(';', $value); - - $name = ''; - $value = ''; - $expire = 0; - $path = '/'; - $domain = null; - $secure = false; - $httpOnly = true; - - foreach ($cookieParameters as $j => $parameter) { - $param = explode('=', $parameter); - - /* - * First parameter will be always a cookie name and it's value. It is not needed to run - * further actions for them, so save the values and move to next parameter. - */ - if (0 === $j) { - $name = trim($param[0]); - $value = trim($param[1]); - continue; - } - - /* - * Now there would be the rest of cookie parameters, names of params are sent different way so - * I need to lowercase the names and remove unneeded spaces. - */ - $paramName = mb_strtolower(trim($param[0])); - $paramValue = true; - - /* - * Some parameters don't have value e.g. "secure", but the value for them if they're specified - * is "true". Otherwise - just read a value for parameter if exists. - */ - if (array_key_exists(1, $param)) { - $paramValue = trim($param[1]); - } - - switch ($paramName) { - case 'expires': - $expire = $paramValue; - break; - case 'path': - $path = $paramValue; - break; - case 'domain': - $domain = $paramValue; - break; - case 'secure': - $secure = $paramValue; - break; - case 'httponly': - $httpOnly = $paramValue; - break; - } - } - - /* - * Create new Cookie object and add it to "cookies" array. - * I must skip to next header as cookies shouldn't be saved in "headers" array. - */ - $cookies[] = new Cookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); - continue; - } - - /* - * Save response header which is not a cookie - */ - $headers[$key] = $value; - } - } - } - - return [ - 'headers' => $headers, - 'cookies' => $cookies, - 'content' => $content, - ]; - } - /** * Adds missing the "0" characters to given number until given length is reached * diff --git a/tests/Utilities/MiscellaneousTest.php b/tests/Utilities/MiscellaneousTest.php index db3145d..6145ac1 100644 --- a/tests/Utilities/MiscellaneousTest.php +++ b/tests/Utilities/MiscellaneousTest.php @@ -770,21 +770,6 @@ class MiscellaneousTest extends BaseTestCase self::assertNotEmpty(Miscellaneous::getProjectRootPath()); } - /** - * @param int $headerSize Length of HTTP headers in content - * @dataProvider provideHeaderSizeForEmptyCurlResponse - */ - public function testGetCurlResponseWithHeadersUsingEmptyResponse($headerSize) - { - $expected = [ - 'headers' => [], - 'cookies' => [], - 'content' => '', - ]; - - self::assertEquals($expected, Miscellaneous::getCurlResponseWithHeaders('', $headerSize)); - } - /** * Provides string to convert characters to latin characters and not lower cased and not human-readable * @@ -1195,30 +1180,6 @@ class MiscellaneousTest extends BaseTestCase ]; } - /** - * Provides length/size of HTTP headers for an empty response - * - * @return Generator - */ - public function provideHeaderSizeForEmptyCurlResponse() - { - yield[ - -10, - ]; - - yield[ - -1, - ]; - - yield[ - 0, - ]; - - yield[ - 10, - ]; - } - /** * {@inheritdoc} */