Skip to content

Commit

Permalink
Add aliases aplication parameters (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Mar 31, 2023
1 parent 7788eac commit 3b9d167
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
1 change: 0 additions & 1 deletion .styleci.yml
Expand Up @@ -56,7 +56,6 @@ enabled:
- phpdoc_order
- phpdoc_property
- phpdoc_scalar
- phpdoc_separation
- phpdoc_singular_inheritdoc
- phpdoc_trim
- phpdoc_trim_consecutive_blank_line_separation
Expand Down
12 changes: 10 additions & 2 deletions src/Parameter.php
Expand Up @@ -4,6 +4,7 @@

namespace Yii\Service;

use Yiisoft\Aliases\Aliases;
use Yiisoft\Arrays\ArrayHelper;

/**
Expand All @@ -29,12 +30,19 @@
*/
final class Parameter implements ParameterInterface
{
public function __construct(private array $parameters)
public function __construct(private array $parameters, private Aliases $aliases)
{
}

public function get(string $key, mixed $default = null): mixed
{
return ArrayHelper::getValueByPath($this->parameters, $key, $default);
/** @var mixed $value */
$value = ArrayHelper::getValueByPath($this->parameters, $key, $default);

if (is_string($value)) {
$value = $this->aliases->get($value);
}

return $value;
}
}
4 changes: 2 additions & 2 deletions src/Redirect.php
Expand Up @@ -16,10 +16,10 @@ public function __construct(
) {
}

public function run(string $url): ResponseInterface
public function run(string $url, int $code = 302): ResponseInterface
{
return $this->responseFactory
->createResponse(302)
->createResponse($code)
->withHeader('Location', $this->urlGenerator->generate($url));
}
}
9 changes: 9 additions & 0 deletions tests/Parameter/Test.php
Expand Up @@ -25,6 +25,15 @@ public function testParameterNoExists(): void
$this->assertNull($this->parameter->get('app.noExist'));
}

public function testParametersWithAliases(): void
{
$this->createContainer();

$this->aliases->set('@root', dirname(__DIR__, 2));

$this->assertDirectoryExists($this->parameter->get('app.aliases.tests'));
}

public function testParameterNoExistsWithDefaultValue(): void
{
$this->createContainer();
Expand Down
11 changes: 11 additions & 0 deletions tests/Redirect/RedirectServiceTest.php
Expand Up @@ -17,6 +17,17 @@ public function testRedirect(): void

$redirect = $this->redirect->run('home');

$this->assertSame(302, $redirect->getStatusCode());
$this->assertSame(['Location' => ['/home/index']], $redirect->getHeaders());
}

public function testRedirectWithCode200(): void
{
$this->createContainer();

$redirect = $this->redirect->run('home', 200);

$this->assertSame(200, $redirect->getStatusCode());
$this->assertSame(['Location' => ['/home/index']], $redirect->getHeaders());
}
}
3 changes: 3 additions & 0 deletions tests/data/config/application-params.php
Expand Up @@ -5,5 +5,8 @@
return [
'app' => [
'name' => 'Yii Demo',
'aliases' => [
'tests' => '@root/tests',
],
],
];

0 comments on commit 3b9d167

Please sign in to comment.