Container.php
2.43 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
<?php
namespace Your\Namespace;
use StellarWP\ContainerContract\ContainerInterface;
// If you are including PHP-DI container using Strauss (recommended), then:
use Your\Namespace\DI\Container as PHPDIContainer;
// If you are including the PHP-DI container directly, then you'd want to do:
//use DI\Container as PHPDIContainer;
class Container implements ContainerInterface {
protected $container;
protected $singletons = [];
/**
* Container constructor.
*/
public function __construct() {
$this->container = new PHPDIContainer();
}
/**
* @inheritDoc
*/
public function bind( string $id, $implementation = null ) {
return $this->container->set( $id, $implementation );
}
/**
* @inheritDoc
*/
public function get( string $id ) {
return $this->container->get( $id );
}
/**
* @inheritDoc
*/
public function has( string $id ) {
return $this->container->has( $id );
}
/**
* Build an entry of the container by its name.
*
* This method behave like get() except resolves the entry again every time.
* For example if the entry is a class then a new instance will be created each time.
*
* This method makes the container behave like a factory.
*
* @template T
*
* @param string|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.
*
* @return mixed|T
* @throws InvalidArgumentException The name parameter must be of type string.
* @throws DependencyException Error while resolving the entry.
* @throws NotFoundException No entry found for the given name.
*/
public function make( string $id, array $parameters = [] ) {
if ( ! empty( $this->singletons[ $id ] ) ) {
return $this->container->get( $id );
}
return $this->container->make( $id, $parameters );
}
/**
* @inheritDoc
*/
public function singleton( string $id, $implementation = null ) {
$this->singletons[ $id ] = true;
return $this->container->set( $id, $implementation );
}
/**
* Defer all other calls to the container object.
*/
public function __call( $name, $args ) {
return $this->container->{$name}( ...$args );
}
}