-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAbstractServiceProvider.php
130 lines (117 loc) · 3.9 KB
/
AbstractServiceProvider.php
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Internal\DependencyManagement;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\League\Container\Definition\DefinitionInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\League\Container\ServiceProvider\AbstractServiceProvider as LeagueProvider;
/**
* Class AbstractServiceProvider
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Internal\DependencyManagement
*/
abstract class AbstractServiceProvider extends LeagueProvider {
/**
* Array of classes provided by this container.
*
* Keys should be the class name, and the value can be anything (like `true`).
*
* @var array
*/
protected $provides = [];
/**
* Returns a boolean if checking whether this provider provides a specific
* service or returns an array of provided services if no argument passed.
*
* @param string $service
*
* @return boolean
*/
public function provides( string $service ): bool {
return array_key_exists( $service, $this->provides );
}
/**
* Use the register method to register items with the container via the
* protected $this->leagueContainer property or the `getLeagueContainer` method
* from the ContainerAwareTrait.
*
* @return void
*/
public function register() {
foreach ( $this->provides as $class => $provided ) {
$this->share( $class );
}
}
/**
* Add an interface to the container.
*
* @param string $interface The interface to add.
* @param string|null $concrete (Optional) The concrete class.
*
* @return DefinitionInterface
*/
protected function share_concrete( string $interface, $concrete = null ): DefinitionInterface {
return $this->getLeagueContainer()->share( $interface, $concrete );
}
/**
* Share a class and add interfaces as tags.
*
* @param string $class The class name to add.
* @param mixed ...$arguments Constructor arguments for the class.
*
* @return DefinitionInterface
*/
protected function share_with_tags( string $class, ...$arguments ): DefinitionInterface {
$definition = $this->share( $class, ...$arguments );
foreach ( class_implements( $class ) as $interface ) {
$definition->addTag( $interface );
}
return $definition;
}
/**
* Share a class.
*
* Shared classes will always return the same instance of the class when the class is requested
* from the container.
*
* @param string $class The class name to add.
* @param mixed ...$arguments Constructor arguments for the class.
*
* @return DefinitionInterface
*/
protected function share( string $class, ...$arguments ): DefinitionInterface {
return $this->getLeagueContainer()->share( $class )->addArguments( $arguments );
}
/**
* Add a class.
*
* Classes will return a new instance of the class when the class is requested from the container.
*
* @param string $class The class name to add.
* @param mixed ...$arguments Constructor arguments for the class.
*
* @return DefinitionInterface
*/
protected function add( string $class, ...$arguments ): DefinitionInterface {
return $this->getLeagueContainer()->add( $class )->addArguments( $arguments );
}
/**
* Maybe share a class and add interfaces as tags.
*
* This will also check any classes that implement the Conditional interface and only add them if
* they are needed.
*
* @param string $class The class name to add.
* @param mixed ...$arguments Constructor arguments for the class.
*/
protected function conditionally_share_with_tags( string $class, ...$arguments ) {
$implements = class_implements( $class );
if ( array_key_exists( Conditional::class, $implements ) ) {
/** @var Conditional $class */
if ( ! $class::is_needed() ) {
return;
}
}
$this->provides[ $class ] = true;
$this->share_with_tags( $class, ...$arguments );
}
}