Skip to content

Commit

Permalink
Add alias function
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jul 24, 2023
1 parent 3caecb5 commit ad9601b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,20 @@ redirect('site/index', [], ['page' => 2], Status::PERMANENT_REDIRECT); // => A r

// Generating absolute url
redirect('/path/to/redirect', [], ['page' => 2], Status::PERMANENT_REDIRECT, true); // => A response object with code 308 and header 'Location' with value 'http://localhost/path/to/redirect?page=2'
```

### `alias(string $path): string`

- `$path` is an alias name

```php
alias('@runtime'); // => '/path/to/runtime'
```

### `aliases(string ...$paths): array`

- `$paths` is alias names

```php
aliases('@runtime', '@webroot'); // => ['/path/to/runtime', '/path/to/webroot']
```
24 changes: 24 additions & 0 deletions src/alias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Yiisoft\Aliases\Aliases;


function alias(string $path): string
{
/**
* @var Aliases $aliases

Check failure on line 11 in src/alias.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

UnnecessaryVarAnnotation

src/alias.php:11:13: UnnecessaryVarAnnotation: The @var Yiisoft\Aliases\Aliases annotation for $aliases is unnecessary (see https://psalm.dev/212)

Check failure on line 11 in src/alias.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-ubuntu-latest

UnnecessaryVarAnnotation

src/alias.php:11:13: UnnecessaryVarAnnotation: The @var Yiisoft\Aliases\Aliases annotation for $aliases is unnecessary (see https://psalm.dev/212)
*/
$aliases = container(Aliases::class);
return $aliases->get($path);
}

function aliases(string ...$paths): array
{
/**
* @var Aliases $aliases

Check failure on line 20 in src/alias.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

UnnecessaryVarAnnotation

src/alias.php:20:13: UnnecessaryVarAnnotation: The @var Yiisoft\Aliases\Aliases annotation for $aliases is unnecessary (see https://psalm.dev/212)

Check failure on line 20 in src/alias.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-ubuntu-latest

UnnecessaryVarAnnotation

src/alias.php:20:13: UnnecessaryVarAnnotation: The @var Yiisoft\Aliases\Aliases annotation for $aliases is unnecessary (see https://psalm.dev/212)
*/
$aliases = container(Aliases::class);
return $aliases->getArray($paths);
}
45 changes: 45 additions & 0 deletions tests/AliasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Xepozz\YiiShort\Tests;

use Xepozz\YiiShort\State;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Test\Support\Container\SimpleContainer;

class AliasTest extends FunctionsTestCase
{
public function testFunctionLoaded(): void
{
$this->assertTrue(function_exists('alias'));
$this->assertTrue(function_exists('aliases'));
}

public function testContainerIsUnset(): void
{
State::$container = null;

$this->expectException(\RuntimeException::class);
alias('test');
}

public function testAliases(): void
{
State::$container = new SimpleContainer([
Aliases::class => new Aliases([
'@test1' => '/root/test1',
'@test2' => '/root/test2',
]),
]);
$this->assertEquals('/root/test1', alias('@test1'));
$this->assertEquals('/root/test2', alias('@test2'));
$this->assertEquals(['/root/test1', '/root/test2'], aliases('@test1', '@test2'));
}

public function bootstrapFiles(): iterable
{
yield __DIR__ . '/../src/container.php';
yield __DIR__ . '/../src/alias.php';
}
}

0 comments on commit ad9601b

Please sign in to comment.