1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI; |
19
|
|
|
|
20
|
|
|
use Rade\DI\Exceptions\ContainerResolutionException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A scope for non-shareable, non-resolveable and lazy class-string service definiton. |
24
|
|
|
* |
25
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class ScopedDefinition |
28
|
|
|
{ |
29
|
|
|
/** Non resolveable service definition */ |
30
|
|
|
public const RAW = 'raw'; |
31
|
|
|
|
32
|
|
|
/** Non shareable service definition */ |
33
|
|
|
public const FACTORY = 'factories'; |
34
|
|
|
|
35
|
|
|
/** A lazy entry is created only when it is used. */ |
36
|
|
|
public const LAZY = 'values'; |
37
|
|
|
|
38
|
|
|
/** A class property which can be found in container */ |
39
|
|
|
public string $property; |
40
|
|
|
|
41
|
|
|
/** @var mixed A definition that suite the $property used in container */ |
42
|
|
|
public $service; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $definition |
46
|
|
|
*/ |
47
|
36 |
|
public function __construct($definition, string $type = self::FACTORY) |
48
|
|
|
{ |
49
|
36 |
|
if ($definition instanceof self) { |
50
|
1 |
|
throw new ContainerResolutionException('Scoped definition cannot be a definiton of itself.'); |
51
|
36 |
|
} elseif (self::LAZY === $type) { |
52
|
17 |
|
if (\is_string($definition) && class_exists($definition)) { |
53
|
17 |
|
$type = $definition; |
54
|
|
|
} |
55
|
|
|
|
56
|
17 |
|
$definition = fn (Container $container) => $container->call($definition); |
57
|
|
|
} |
58
|
|
|
|
59
|
36 |
|
$this->property = $type; |
60
|
36 |
|
$this->service = $definition; |
61
|
36 |
|
} |
62
|
|
|
} |
63
|
|
|
|