Skip to content

Aliases

In many cases, classes will have typehints for interfaces, rather than concrete classes. As the Container cannot assume which class to inject, the Alias Manager exists to provide aliases to actual classes.

Centum\Container\AliasManager();

Centum\Container\AliasManager implements Centum\Interfaces\Container\AliasManagerInterface.

You can obtain the Alias Manager from a Container:

use Centum\Interfaces\Container\ContainerInterface;

/** @var ContainerInterface $container */

$aliasManager = $container->getAliasManager();

Aliases can be added using the add() method:

use Centum\Flash\Formatter\HtmlFormatter;
use Centum\Interfaces\Flash\FormatterInterface;

$aliasManager->add(FormatterInterface::class, HtmlFormatter::class);

Now, any call with FormatterInterface will return the HtmlFormatter class instead:

use Centum\Flash\Formatter\HtmlFormatter;
use Centum\Interfaces\Flash\FormatterInterface;

$alias = $aliasManager->get(FormatterInterface::class); // = HtmlFormatter::class

If an alias hasn’t been set, then the original class will be returned:

$alias = $aliasManager->get(RandomClass::class); // = RandomClass::class

The Container will implicitly handle aliases internally so getting FormatterInterface from the Container will now actually return a HtmlFormatter object:

use Centum\Flash\Formatter\HtmlFormatter;
use Centum\Interfaces\Flash\FormatterInterface;

$formatter = $container->get(FormatterInterface::class); // = HtmlFormatter object

Default Aliases

By default, some aliases have already been set:

Interface Class
Centum\Interfaces\Access\AccessInterface Centum\Access\Access
Centum\Interfaces\Console\ApplicationInterface Centum\Console\Application
Centum\Interfaces\Console\TerminalInterface Centum\Console\Terminal
Centum\Interfaces\Cron\CronInterface Centum\Cron\Cron
Centum\Interfaces\Flash\FlashInterface Centum\Flash\Flash
Centum\Interfaces\Flash\FormatterInterface Centum\Flash\Formatter\HtmlFormatter
Centum\Interfaces\Flash\StorageInterface Centum\Flash\Storage
Centum\Interfaces\Http\Csrf\GeneratorInterface Centum\Http\Csrf\Generator
Centum\Interfaces\Http\Csrf\StorageInterface Centum\Http\Csrf\Storage
Centum\Interfaces\Http\Csrf\ValidatorInterface Centum\Http\Csrf\Validator
Centum\Interfaces\Http\RequestInterface Centum\Http\Request
Centum\Interfaces\Http\SessionInterface Centum\Http\Session\GlobalSession
Centum\Interfaces\Queue\TaskRunnerInterface Centum\Queue\TaskRunner
Centum\Interfaces\Router\RouterInterface Centum\Router\Router
Centum\Interfaces\Translation\TranslatorInterface Centum\Translation\Translator
Centum\Interfaces\Url\UrlInterface Centum\Url\Url