Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
7 / 7 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Factory | |
100.00% |
7 / 7 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
new | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOrNew | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
set | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getFactory | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework Factories Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\Factories; |
11 | |
12 | /** |
13 | * Class Factory. |
14 | * |
15 | * @package factories |
16 | */ |
17 | class Factory |
18 | { |
19 | /** |
20 | * All class objects set in the current Factory. |
21 | * |
22 | * @var array<string,object> |
23 | */ |
24 | protected array $classes = []; |
25 | /** |
26 | * Factory instances. |
27 | * |
28 | * @var array<string,Factory> |
29 | */ |
30 | protected static array $factories = []; |
31 | |
32 | /** |
33 | * Get an object based in the FQCN. |
34 | * |
35 | * @template T of object |
36 | * |
37 | * @param class-string<T> $fqcn The Full Qualified Class Name |
38 | * |
39 | * @return T|null The FQCN object or null if it was not set |
40 | */ |
41 | public function get(string $fqcn) : object|null |
42 | { |
43 | return $this->classes[$fqcn] ?? null; // @phpstan-ignore-line |
44 | } |
45 | |
46 | /** |
47 | * Create a new object based on the FQCN. |
48 | * |
49 | * This method will replace the class instance. |
50 | * |
51 | * @template T of object |
52 | * |
53 | * @param class-string<T> $fqcn The Full Qualified Class Name |
54 | * @param array<int,mixed> $construct Class constructor arguments |
55 | * |
56 | * @return T The created object |
57 | */ |
58 | public function new(string $fqcn, array $construct = []) : object |
59 | { |
60 | return $this->classes[$fqcn] = new $fqcn(...$construct); |
61 | } |
62 | |
63 | /** |
64 | * Try to get an existing class instance based on FQCN. |
65 | * If it is not set, create a new instance and return it. |
66 | * |
67 | * @template T of object |
68 | * |
69 | * @param class-string<T> $fqcn The Full Qualified Class Name |
70 | * @param array<int,mixed> $construct Class constructor arguments |
71 | * |
72 | * @return T The existing or created object |
73 | */ |
74 | public function getOrNew(string $fqcn, array $construct = []) : object |
75 | { |
76 | return $this->get($fqcn) ?? $this->new($fqcn, $construct); |
77 | } |
78 | |
79 | /** |
80 | * Set a new object to the list of classes set. |
81 | * |
82 | * @param object $object A object |
83 | * |
84 | * @return static |
85 | */ |
86 | public function set(object $object) : static |
87 | { |
88 | $this->classes[$object::class] = $object; |
89 | return $this; |
90 | } |
91 | |
92 | /** |
93 | * Get (existing or created) Factory instance based on a custom name. |
94 | * |
95 | * @param string $name The Factory name |
96 | * |
97 | * @return Factory The Factory instance |
98 | */ |
99 | public static function getFactory(string $name = 'default') : Factory |
100 | { |
101 | return self::$factories[$name] |
102 | ?? (self::$factories[$name] = new self()); |
103 | } |
104 | } |