yiisoft / aliases
Named paths and URLs storage
Fund package maintenance!
Opencollective
yiisoft
Installs: 510 606
Dependents: 74
Suggesters: 0
Security: 0
Stars: 20
Watchers: 16
Forks: 8
Open Issues: 4
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.6
- yiisoft/di: ^1.0
This package is auto-updated.
Last update: 2023-05-14 17:10:13 UTC
README
Yii Aliases
The package aim is to store path aliases, i.e. short name representing a long path (a file path, a URL, etc.).
Path alias value may have another value as its part. For example, @vendor
may store path to vendor
directory
while @bin
may store @vendor/bin
.
Requirements
- PHP 7.4 or higher.
Installation
The package could be installed with composer:
composer require yiisoft/aliases --prefer-dist
General usage
A path alias must start with the character '@' so that it can be easily differentiated from non-alias paths.
use Yiisoft\Aliases\Aliases; $aliases = new Aliases([ '@root' => __DIR__, ]); $aliases->set('@vendor', '@root/vendor'); $aliases->set('@bin', '@vendor/bin'); echo $aliases->get('@bin/phpunit');
The code about would output "/path/to/vendor/bin/phpunit".
Note that set()
method does not check if the given path exists or not. All it does is to associate the alias with
the path.
The path could be:
- a directory or a file path (e.g.
/tmp
,/tmp/main.txt
) - a URL (e.g.
http://www.yiiframework.com
) - a path alias (e.g.
@yii/base
). It will be resolved on {@see get()} call.
Any trailing /
and \
characters in the given path will be trimmed.
To bulk translate path aliases into actual paths use getArray()
method:
$aliases = new Aliases([ '@root' => '/my/app', ]); // Value will be ['src' => '/my/app/src', 'tests' => '/my/app/tests'] $directories = $aliases->getAll(['src' => '@root/src', 'tests' => '@root/tests']);
Alias priorities
In case multiple aliases are registered with same root (prefix), then the most specific has precedence:
use Yiisoft\Aliases\Aliases; $aliases = new Aliases([ '@vendor' => __DIR__ . '/vendor', '@vendor/test' => '/special/location' ]); echo $aliases->get('@vendor/test');
That would output /special/location
since @vendor/test
is more specific match than @vendor
.
Alias removal
If you need to remove alias runtime:
use Yiisoft\Aliases\Aliases; $aliases = new Aliases([ '@root' => __DIR__, ]); $aliases->remove('@root');
Testing
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:
./vendor/bin/roave-infection-static-analysis-plugin
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
License
The Yii Aliases is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.