diff --git a/README.MD b/README.MD index 3905e25..cbfbe65 100644 --- a/README.MD +++ b/README.MD @@ -12,15 +12,15 @@ Simple bridge between Symfony and RoadRunner without PSR7 layer (HttpClient). ```php load(__DIR__ . '/.env'); + (new \Symfony\Component\Dotenv\Dotenv(false))->load(__DIR__ . '/.env'); } diff --git a/src/SymfonyIntegration.php b/src/SymfonyIntegration.php index f357c3a..ddb812a 100644 --- a/src/SymfonyIntegration.php +++ b/src/SymfonyIntegration.php @@ -115,11 +115,13 @@ public function processRequest(array $ctx, $body): array $this->_symfonyRequest = $this->buildSymfonyRequest($ctx, $body); $this->_symfonyResponse = $this->_kernel->handle($this->_symfonyRequest); - if (!$this->_symfonyRequest->cookies->has(\session_name())) { + $sessionName = \session_name(); + + if (!$this->_symfonyRequest->cookies->has($sessionName)) { $container = $this->_kernel->getContainer(); $cookieOptions = $container->hasParameter('session.storage.options') ? $container->getParameter('session.storage.options') : []; $this->_symfonyResponse->headers->setCookie(new Cookie( - \session_name(), + $sessionName, \session_id(), $cookieOptions['cookie_lifetime'] ?? 0, $cookieOptions['cookie_path'] ?? '/', @@ -179,28 +181,26 @@ private function buildResponse(Response $response): array $body = ''; if ($response instanceof BinaryFileResponse) { $body = file_get_contents($response->getFile()->getPathname()); - } else { - if ($response instanceof StreamedResponse) { - ob_start(function ($buffer) use (&$body) { - $body .= $buffer; + } elseif ($response instanceof StreamedResponse) { + ob_start(static function ($buffer) use (&$body) { + $body .= $buffer; - return ''; - }); + return ''; + }); - $response->sendContent(); - ob_end_clean(); - } else { - $body = $response->getContent(); - } + $response->sendContent(); + ob_end_clean(); + } else { + $body = $response->getContent(); } $headers = $response->headers->all(); - if (!isset($headers['Set-Cookie']) && !isset($headers['set-cookie'])) { + if (!isset($headers['Set-Cookie'], $headers['set-cookie'])) { $cookies = $response->headers->getCookies(); if (!empty($cookies)) { $headers['Set-Cookie'] = []; foreach ($cookies as $cookie) { - $headers['Set-Cookie'][] = $cookie->__toString(); + $headers['Set-Cookie'][] = (string) $cookie; } } } @@ -241,7 +241,7 @@ private function configureServer(array $ctx): array $server['HTTP_HOST'] = $server['SERVER_NAME'] . ':' . $server['SERVER_PORT']; foreach ($ctx['headers'] as $key => $value) { $key = strtoupper(str_replace('-', '_', $key)); - if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) { + if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'], true)) { $server[$key] = implode(', ', $value); } else { $server['HTTP_' . $key] = implode(', ', $value); @@ -257,7 +257,7 @@ private function configureServer(array $ctx): array * @param array $uploads Array of files from RR request * @return array $_FILES style array */ - private function prepareFiles(array $uploads) + private function prepareFiles(array $uploads): array { foreach ($uploads as $key => &$file) { $file['tmp_name'] = $file['tmpName']; @@ -274,7 +274,7 @@ private function prepareFiles(array $uploads) * @param string $path * @return string */ - private function filterPath($path) + private function filterPath(string $path): string { $path = preg_replace_callback( '/(?:[^a-zA-Z0-9_\-\.~\pL)(:@&=\+\$,\/;%]+|%(?![A - Fa - f0 - 9]{2}))/u', @@ -297,14 +297,14 @@ private function filterPath($path) } /** - * Filter a query string to ensure it is propertly encoded. + * Filter a query string to ensure it is properly encoded. * * Ensures that the values in the query string are properly urlencoded. * * @param string $query * @return string */ - private function filterQuery($query) + private function filterQuery(string $query): string { if ('' !== $query && strpos($query, '?') === 0) { $query = substr($query, 1); @@ -312,7 +312,7 @@ private function filterQuery($query) $parts = explode('&', $query); foreach ($parts as $index => $part) { - list($key, $value) = $this->splitQueryValue($part); + [$key, $value] = $this->splitQueryValue($part); if ($value === null) { $parts[$index] = $this->filterQueryOrFragment($key); continue; @@ -333,7 +333,7 @@ private function filterQuery($query) * @param string $value * @return array A value with exactly two elements, key and value */ - private function splitQueryValue($value) + private function splitQueryValue(string $value): array { $data = explode(' = ', $value, 2); if (!isset($data[1])) { @@ -348,7 +348,7 @@ private function splitQueryValue($value) * @param string $value * @return string */ - private function filterQueryOrFragment($value) + private function filterQueryOrFragment(string $value): string { return preg_replace_callback( ' / (?:[^a-zA-Z0-9_\-\.~\pL!\$ & \'\(\)\*\+,;=%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/u', @@ -363,7 +363,7 @@ private function filterQueryOrFragment($value) * @param array $matches * @return string */ - private function urlEncodeChar(array $matches) + private function urlEncodeChar(array $matches): string { return rawurlencode($matches[0]); }