Repository navigation
Expand file tree
/
Copy pathContainer.php
More file actions
111 lines (93 loc) · 3.17 KB
/
Copy pathContainer.php
File metadata and controls
111 lines (93 loc) · 3.17 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php declare(strict_types=1);
namespace Tribe\Libs\Container;
use DI\FactoryInterface;
use Invoker\InvokerInterface;
use Psr\Container\ContainerInterface;
use ReflectionObject;
/**
* A mutable container wrapper.
*/
class Container extends \DI\Container implements MutableContainer, ContainerInterface, FactoryInterface, InvokerInterface {
/**
* A reflection of the wrapped container.
*
* @var ReflectionObject|null
*/
protected $reflectionContainer = null;
/**
* Wrap an existing container and create a new instance of this container.
*
* Items in the wrapped container are still resolved in this container.
*
* @param \Psr\Container\ContainerInterface|null $container
*
* @return \Tribe\Libs\Container\MutableContainer|\DI\Container|ContainerInterface|\DI\FactoryInterface|\Invoker\InvokerInterface
*/
public function wrap( ?ContainerInterface $container = null ) {
if ( $container ) {
$container = clone $container;
}
$this->delegateContainer = $container ?: $this;
$object = new self( null, null, $container );
$object->resolvedEntries = $this->resetEntries();
return $object->setReflectionContainer( $container );
}
/**
* Make a completely fresh object, including all of its dependencies.
*
* @template T
*
* @param class-string<T> $name Entry name or a class name.
* @param array $parameters Optional parameters to use to build the entry. Use this to force
* specific parameters to specific values. Parameters not defined in this
* array will be resolved using the container.
*
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \ReflectionException
*
* @return mixed|T
*/
public function makeFresh( $name, array $parameters = [] ) {
return $this->flush()->make( $name, $parameters );
}
/**
* Flush the container of all bindings and resolved instances in this
* container and the wrapped container.
*
* @throws \ReflectionException
*
* @return \Tribe\Libs\Container\MutableContainer|\DI\Container|ContainerInterface|\DI\FactoryInterface|\Invoker\InvokerInterface
*/
public function flush() {
$this->resetEntries();
if ( is_null( $this->reflectionContainer ) ) {
return $this;
}
$resolvedEntries = $this->reflectionContainer->getProperty( 'resolvedEntries' );
$resolvedEntries->setAccessible( true );
$resolvedEntries->setValue( $this->delegateContainer, [] );
return $this;
}
/**
* Reset resolved entries to the default.
*
* @return array<string, mixed>
*/
protected function resetEntries(): array {
return $this->resolvedEntries = [
MutableContainer::class => $this,
];
}
/**
* Create a reflection object of the wrapped container.
*
* @param \Psr\Container\ContainerInterface|null $container
*
* @return \Tribe\Libs\Container\MutableContainer|\DI\Container|ContainerInterface|\DI\FactoryInterface|\Invoker\InvokerInterface
*/
protected function setReflectionContainer( ?ContainerInterface $container = null ) {
$this->reflectionContainer = $container ? new ReflectionObject( $container ) : null;
return $this;
}
}