-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapper.php
More file actions
86 lines (75 loc) · 2.22 KB
/
Wrapper.php
File metadata and controls
86 lines (75 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php declare(strict_types=1);
/*
* This file is part of Polymorphine/Container package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Polymorphine\Container\Setup;
use Polymorphine\Container\Records\Record;
/**
* ComposedInstanceRecord definition builder.
*/
class Wrapper
{
private string $id;
private Record $record;
private Entry $entry;
/**
* @param string $id
* @param Record $record
* @param Entry $entry
*/
public function __construct(string $id, Record $record, Entry $entry)
{
$this->id = $id;
$this->record = $record;
$this->entry = $entry;
}
/**
* Defines class instantiation wrapping previously defined instance.
* This method is similar to Entry::instance() except it returns its
* own instance until Wrapper::compose() is called, and valid definition
* requires current entry identifier as one of given dependencies to
* inject wrapped object. Otherwise exception will be thrown.
*
* @see Entry::instance()
* @see Wrapper::compose()
*
* @param string $className
* @param string ...$dependencies
*
* @throws Exception\IntegrityConstraintException
*
* @return $this
*/
public function with(string $className, string ...$dependencies): self
{
$dependencies = $this->clearWrapped($dependencies);
$this->record = new Record\ComposedInstanceRecord($className, $this->record, ...$dependencies);
return $this;
}
/**
* Adds ComposedInstanceRecord created with collected wrappers composition
* to container records.
*/
public function compose(): void
{
$this->entry->record($this->record);
}
private function clearWrapped(array $dependencies): array
{
$valid = false;
foreach ($dependencies as &$id) {
if ($id !== $this->id) { continue; }
$id = null;
$valid = true;
}
if (!$valid) {
throw Exception\IntegrityConstraintException::missingReference($this->id);
}
return $dependencies;
}
}