Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AliasFactory
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/container.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/container
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Container\Factory;
20
21use Psr\Container\ContainerInterface;
22
23/**
24 * A factory that resolves an alias to another service within a PSR-11 container.
25 *
26 * This factory MUST be used when a service should act as an alias for another
27 * service already registered in the container.
28 *
29 * When invoked, it SHALL delegate resolution to the aliased service identifier.
30 */
31 class AliasFactory implements FactoryInterface
32{
33    /**
34     * @var array<string, self> Registry of AliasFactory instances indexed by alias name.
35     *                          This MAY be used to cache and reuse factory instances.
36     */
37    private static array $aliases = [];
38
39    /**
40     * Constructs the AliasFactory with the target service identifier.
41     *
42     * @param string $alias the identifier of the service to which this factory points
43     */
44    public function __construct(
45        private  string $alias
46    ) {}
47
48    /**
49     * Resolves the aliased service from the container.
50     *
51     * This method MUST return the same instance as if the original alias identifier
52     * were used directly with the container.
53     *
54     * @param ContainerInterface $container the container instance to resolve the alias from
55     *
56     * @return mixed the resolved service instance
57     */
58    public function __invoke(ContainerInterface $container): mixed
59    {
60        return $container->get($this->alias);
61    }
62
63    /**
64     * Retrieves or creates a cached AliasFactory for a given alias.
65     *
66     * This static method SHOULD be used to avoid instantiating multiple factories
67     * for the same alias unnecessarily. The same instance will be reused for each alias.
68     *
69     * @param string $alias the identifier to create or retrieve the factory for
70     *
71     * @return self an AliasFactory instance associated with the provided alias
72     */
73    public static function get(string $alias): self
74    {
75        return self::$aliases[$alias] ??= new self($alias);
76    }
77}