uestla/aliaser

Simple PHP library for parsing and handling class aliases defined by USE statements.

1.1.0 2013-08-14 17:51 UTC

This package is auto-updated.

Last update: 2024-12-11 21:39:26 UTC


README

Simple PHP library for parsing and handling class and function aliases defined by USE statements.

Usage

Book.php

Here we have an entity class Book with some annotations:

namespace Model\Entities;

use \DateTime;

/**
 * @property User $author
 * @property DateTime $written
 */
class Book
{}

function foo()
{}

foo.php

And now let's say we would like to take those annotations and get the full class name of those @property types defined. And that's where we'll use Aliaser:

$reflection = new ReflectionClass('Model\Entities\Book');

Aliaser\Container::getClass('User', $reflection); // 'Model\Entities\User'
Aliaser\Container::getClass('DateTime', $reflection); // 'DateTime'

We can discover full function/callback name according to given namespace context as well.

Aliaser\Container::getCallback('DateTime::format', $reflection); // 'DateTime::format'
Aliaser\Container::getCallback('User::getName', $reflection); // 'Model\Entities\User::getName'

Aliaser\Container::getCallback('foo', $reflection); // 'Model\Entities\foo'

It handles multiple namespace definitions in a single file as well.

However, parsing can be quite expensive - we can use a Nette Framework Cache:

$storage = new Nette\Caching\Storages\FileStorage(__DIR__ . '/temp');
Aliaser\Container::setCacheStorage($storage);

// ...

Enjoy.