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\Container\AliasManagerInterface;
use Centum\Interfaces\Flash\FormatterInterface;
/** @var AliasManagerInterface $aliasManager */
$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\Container\AliasManagerInterface;
use Centum\Interfaces\Flash\FormatterInterface;
/** @var AliasManagerInterface $aliasManager */
$alias = $aliasManager->get(FormatterInterface::class); // = HtmlFormatter::class
If an alias hasn’t been set, then the original class will be returned:
use Centum\Interfaces\Console\TerminalInterface;
use Centum\Interfaces\Container\AliasManagerInterface;
/** @var AliasManagerInterface $aliasManager */
$alias = $aliasManager->get(TerminalInterface::class); // = TerminalInterface::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\Container\ContainerInterface;
use Centum\Interfaces\Flash\FormatterInterface;
/** @var ContainerInterface $container */
$formatter = $container->get(FormatterInterface::class); // = HtmlFormatter object
Default Aliases
By default, some aliases have already been set: