-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce factory for the server url helper
Adds laminas-diactoros as a dependency so that detection of the host URI can be delegated to existing code Signed-off-by: George Steel <[email protected]>
- Loading branch information
Showing
5 changed files
with
355 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\View\Helper\Service; | ||
|
||
use ArrayAccess; | ||
use Laminas\View\Exception\RuntimeException; | ||
use Laminas\View\Helper\ServerUrl; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use function assert; | ||
use function is_array; | ||
use function is_string; | ||
use function Laminas\Diactoros\marshalHeadersFromSapi; | ||
use function Laminas\Diactoros\marshalUriFromSapi; | ||
|
||
final class ServerUrlFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): ServerUrl | ||
{ | ||
return new ServerUrl( | ||
$this->fetchConfiguredServerUrl($container) ?: $this->detectServerUrlFromEnvironment() | ||
); | ||
} | ||
|
||
private function fetchConfiguredServerUrl(ContainerInterface $container): ?string | ||
{ | ||
$config = $container->has('config') ? $container->get('config') : []; | ||
assert(is_array($config) || $config instanceof ArrayAccess); | ||
|
||
$helperConfig = $config['view_helper_config'] ?? []; | ||
assert(is_array($helperConfig)); | ||
|
||
$serverUrl = $helperConfig['server_url'] ?? null; | ||
assert(is_string($serverUrl) || $serverUrl === null); | ||
|
||
return $serverUrl; | ||
} | ||
|
||
private function detectServerUrlFromEnvironment(): string | ||
{ | ||
$uri = marshalUriFromSapi($_SERVER, marshalHeadersFromSapi($_SERVER)) | ||
->withPath('') | ||
->withQuery('') | ||
->withFragment(''); | ||
|
||
if (! $uri->getHost() || ! $uri->getScheme()) { | ||
throw new RuntimeException( | ||
'The current host or scheme cannot be detected from the environment' | ||
); | ||
} | ||
|
||
return (string) $uri; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\View\Helper\Service; | ||
|
||
use Laminas\ServiceManager\ServiceManager; | ||
use Laminas\View\Exception\RuntimeException; | ||
use Laminas\View\Helper\Service\ServerUrlFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ServerUrlFactoryTest extends TestCase | ||
{ | ||
/** @var array<array-key, mixed> */ | ||
private array $serverVariables; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->serverVariables = $_SERVER; | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$_SERVER = $this->serverVariables; | ||
parent::tearDown(); | ||
} | ||
|
||
public function testThatWhenThereIsNoConfigurationTheHostUriWillBeDetectedFromGlobals(): void | ||
{ | ||
$_SERVER['HTTP_HOST'] = 'example.com'; | ||
$_SERVER['HTTPS'] = true; | ||
$_SERVER['SERVER_PORT'] = 443; | ||
|
||
$helper = (new ServerUrlFactory())(new ServiceManager()); | ||
self::assertEquals('https://example.com', $helper->__invoke()); | ||
} | ||
|
||
public function testThatWhenThereIsNoConfigurationDetectedPathQueryAndFragmentWillBeOmitted(): void | ||
{ | ||
$_SERVER['HTTP_HOST'] = 'example.com'; | ||
$_SERVER['HTTPS'] = true; | ||
$_SERVER['SERVER_PORT'] = 443; | ||
$_SERVER['REQUEST_URI'] = '/some/#thing?foo=bar'; | ||
|
||
$helper = (new ServerUrlFactory())(new ServiceManager()); | ||
self::assertEquals('https://example.com', $helper->__invoke()); | ||
} | ||
|
||
public function testThatConfiguredHostIsPreferred(): void | ||
{ | ||
$container = new ServiceManager(); | ||
$container->setService('config', [ | ||
'view_helper_config' => [ | ||
'server_url' => 'https://other.example.com', | ||
], | ||
]); | ||
|
||
$_SERVER['HTTP_HOST'] = 'example.com'; | ||
$_SERVER['HTTPS'] = true; | ||
$_SERVER['SERVER_PORT'] = 443; | ||
|
||
$helper = (new ServerUrlFactory())($container); | ||
self::assertEquals('https://other.example.com', $helper->__invoke()); | ||
} | ||
|
||
public function testUndetectableEnvironmentAndZeroConfigurationYieldsException(): void | ||
{ | ||
$_SERVER = []; | ||
|
||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('The current host or scheme cannot be detected from the environment'); | ||
|
||
(new ServerUrlFactory())(new ServiceManager()); | ||
} | ||
|
||
public function testAnEmptyEnvironmentIsAcceptableWhenConfigurationIsFound(): void | ||
{ | ||
$_SERVER = []; | ||
$container = new ServiceManager(); | ||
$container->setService('config', [ | ||
'view_helper_config' => [ | ||
'server_url' => 'https://other.example.com', | ||
], | ||
]); | ||
$helper = (new ServerUrlFactory())($container); | ||
self::assertEquals('https://other.example.com', $helper->__invoke()); | ||
} | ||
} |