Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 6ddd02f

Browse files
committed
Merge branch 'hotfix/165' into develop
Forward port #165
2 parents a2c5548 + b1d14aa commit 6ddd02f

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ All notable changes to this project will be documented in this file, in reverse
4444

4545
### Fixed
4646

47+
- [#165](https://github.com/zendframework/zend-http/pull/165) fixes detection of the base URL when operating under a CLI environment.
48+
4749
- [#149](https://github.com/zendframework/zend-http/pull/149) provides fixes to `Client::setUri()` to ensure its status as a relative
4850
or absolute URI is correctly memoized.
4951

src/PhpEnvironment/Request.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ protected function detectBaseUrl()
489489
// Backtrack up the SCRIPT_FILENAME to find the portion
490490
// matching PHP_SELF.
491491

492+
$argv = $this->getServer()->get('argv', []);
493+
if (isset($argv[0]) && strpos($filename, $argv[0]) === 0) {
494+
$filename = substr($filename, strlen($argv[0]));
495+
}
496+
492497
$baseUrl = '/';
493498
$basename = basename($filename);
494499
if ($basename) {

test/PhpEnvironment/RequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,4 +791,18 @@ public function testDetectBaseUrlDoesNotRaiseErrorOnEmptyBaseUrl()
791791
// If no baseUrl is detected at all, an empty string is returned.
792792
$this->assertEquals('', $url);
793793
}
794+
795+
public function testDetectCorrectBaseUrlForConsoleRequests()
796+
{
797+
$_SERVER['argv'] = ['/home/user/package/vendor/bin/phpunit'];
798+
$_SERVER['argc'] = 1;
799+
$_SERVER['SCRIPT_FILENAME'] = '/home/user/package/vendor/bin/phpunit';
800+
$_SERVER['SCRIPT_NAME'] = '/home/user/package/vendor/bin/phpunit';
801+
$_SERVER['PHP_SELF'] = '/home/user/package/vendor/bin/phpunit';
802+
803+
$request = new Request();
804+
$request->setRequestUri('/path/query/phpunit');
805+
806+
$this->assertSame('', $request->getBaseUrl());
807+
}
794808
}

0 commit comments

Comments
 (0)