diff --git a/Dockerfile b/Dockerfile index 2d85cd0..e1efc9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ -FROM php:7.4-cli +FROM php:8.2-cli RUN pecl install xdebug RUN apt-get update RUN apt-get install -y libicu-dev zip unzip RUN docker-php-ext-enable xdebug -RUN docker-php-ext-install intl \ No newline at end of file +RUN docker-php-ext-install intl diff --git a/composer.json b/composer.json index 0187a6f..6c0670d 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,5 @@ { + "version": "1.0.0", "name": "simovative/zeus", "description": "A Post-Redirect-Get specialised framework for PHP", "keywords": [ diff --git a/docker-compose.yaml b/docker-compose.yaml index 9a66ad4..865feee 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,3 @@ -version: "3.9" services: php-cli: build: . @@ -7,4 +6,4 @@ services: type: bind target: /usr/src/myapp tty: true - working_dir: /usr/src/myapp \ No newline at end of file + working_dir: /usr/src/myapp diff --git a/src/Http/Url/Url.php b/src/Http/Url/Url.php index 1667830..3e93bb7 100644 --- a/src/Http/Url/Url.php +++ b/src/Http/Url/Url.php @@ -25,6 +25,28 @@ public function __construct($url) { $this->url = $url; } + private static function createFromCliRequest(array $serverArray): Url + { + $url = 'cli://'; + # Add hostname + $hostname = gethostname(); + if ($hostname !== false) { + $url .= $hostname; + } else { + $hostname = 'unknown'; + } + # Add script name + if (! empty($serverArray['argv'])) { + $url .= '/' . $serverArray['argv'][0]; + } + return new self($url); + } + + private static function isCliRequest(array $serverArray): bool + { + return ! empty($serverArray['argv']); + } + /** * @author mnoerenberg * @return string @@ -106,6 +128,9 @@ public function getFragment(): string { * @return Url */ public static function createFromServerArray(array $serverArray): Url { + if (self::isCliRequest($serverArray)) { + return self::createFromCliRequest($serverArray); + } if (self::isForwardedRequest($serverArray)) { return self::createUrlForForwardedRequest($serverArray); } diff --git a/tests/Unit/Http/Url/UrlTest.php b/tests/Unit/Http/Url/UrlTest.php index f902a8a..e235f12 100644 --- a/tests/Unit/Http/Url/UrlTest.php +++ b/tests/Unit/Http/Url/UrlTest.php @@ -15,6 +15,8 @@ class UrlTest extends TestCase { private const URL_WITH_CREDENTIALS = 'https://username:password@www.simovative.com:443/api/v1/event/20?param=1&user=1#myFragment'; private const URL = 'https://www.simovative.com/api/v1/event/20?param=1&user=1#myFragment'; + + private const URL_PATTERN_CLI = '#^cli\:\/\/[^\/]+\/bin\/cli\.php$#'; private const URL_HTTPS = 'on'; private const URL_REQUEST_URI = '/api/v1/event/20?param=1&user=1#myFragment'; private const URL_SERVER_PORT = 443; @@ -179,4 +181,14 @@ public function testThatUrlIsCorrectlyParsedFromForwardedServerArray(): void $url = Url::createFromServerArray($serverArray); self::assertEquals(self::URL, $url->__toString()); } + + public function testThatUrlIsCorrectlyParsedFromCliRequest(): void + { + $serverArray = [ + 'argv' => ['bin/cli.php'], + 'argc' => 1, + ]; + $url = Url::createFromServerArray($serverArray); + self::assertMatchesRegularExpression(self::URL_PATTERN_CLI, $url->__toString()); + } }