|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
use EventEspresso\core\domain\values\FilePath; |
|
7
|
|
|
use EventEspresso\core\domain\values\FullyQualifiedName; |
|
8
|
|
|
use EventEspresso\core\domain\values\Version; |
|
9
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
10
|
|
|
use EventEspresso\core\exceptions\InvalidFilePathException; |
|
11
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
use EventEspresso\core\services\loaders\LoaderFactory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DomainFactory |
|
17
|
|
|
* Factory class for generating addon Domain objects |
|
18
|
|
|
* |
|
19
|
|
|
* @package EventEspresso\core\domain |
|
20
|
|
|
* @author Brent Christensen |
|
21
|
|
|
* @since 4.9.50 |
|
22
|
|
|
*/ |
|
23
|
|
|
class DomainFactory |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var DomainInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
protected static $domains = []; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $domain_fqcn [required] Fully Qualified Class Name for the Domain class |
|
33
|
|
|
* @param string $main_file [required] path to the main plugin file |
|
34
|
|
|
* @param string $version [required] version string for the plugin |
|
35
|
|
|
* @return DomainInterface |
|
36
|
|
|
* @throws DomainException |
|
37
|
|
|
* @throws InvalidArgumentException |
|
38
|
|
|
* @throws InvalidDataTypeException |
|
39
|
|
|
* @throws InvalidInterfaceException |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function create(string $domain_fqcn, string $main_file, string $version): DomainInterface |
|
42
|
|
|
{ |
|
43
|
|
|
$fqcn = new FullyQualifiedName($domain_fqcn); |
|
44
|
|
|
return DomainFactory::getDomain($fqcn->string(), [$main_file, $version]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param FullyQualifiedName $domain_fqcn [required] Fully Qualified Class Name for the Domain class |
|
50
|
|
|
* @param array $arguments [required] array of arguments to be passed to the Domain class |
|
51
|
|
|
* constructor. Must at least include the following two value objects: |
|
52
|
|
|
* [ |
|
53
|
|
|
* EventEspresso\core\domain\values\FilePath $plugin_file |
|
54
|
|
|
* EventEspresso\core\domain\values\Version $version |
|
55
|
|
|
* ] |
|
56
|
|
|
* @return DomainInterface |
|
57
|
|
|
* @throws DomainException |
|
58
|
|
|
* @throws InvalidArgumentException |
|
59
|
|
|
* @throws InvalidDataTypeException |
|
60
|
|
|
* @throws InvalidInterfaceException |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function getShared(FullyQualifiedName $domain_fqcn, array $arguments): DomainInterface |
|
63
|
|
|
{ |
|
64
|
|
|
return DomainFactory::getDomain($domain_fqcn->string(), $arguments); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return DomainInterface |
|
70
|
|
|
* @throws DomainException |
|
71
|
|
|
* @throws InvalidArgumentException |
|
72
|
|
|
* @throws InvalidDataTypeException |
|
73
|
|
|
* @throws InvalidFilePathException |
|
74
|
|
|
* @throws InvalidInterfaceException |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function getEventEspressoCoreDomain(): DomainInterface |
|
77
|
|
|
{ |
|
78
|
|
|
$fqcn = 'EventEspresso\core\domain\Domain'; |
|
79
|
|
|
if (! isset(DomainFactory::$domains[ $fqcn ])) { |
|
80
|
|
|
DomainFactory::getDomain($fqcn, [EVENT_ESPRESSO_MAIN_FILE, espresso_version()]); |
|
81
|
|
|
} |
|
82
|
|
|
return DomainFactory::$domains[ $fqcn ]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $fqcn |
|
88
|
|
|
* @param array $arguments |
|
89
|
|
|
* @return DomainInterface |
|
90
|
|
|
*/ |
|
91
|
|
|
private static function getDomain(string $fqcn, array $arguments): DomainInterface |
|
92
|
|
|
{ |
|
93
|
|
|
if (! isset(DomainFactory::$domains[ $fqcn ])) { |
|
94
|
|
View Code Duplication |
if (! isset($arguments[0], $arguments[1])) { |
|
95
|
|
|
throw new InvalidArgumentException( |
|
96
|
|
|
esc_html__( |
|
97
|
|
|
'You need to pass at least two arguments, representing the addon plugin file and version, in order to generate a Domain class', |
|
98
|
|
|
'event_espresso' |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
$filepath = $arguments[0] instanceof FilePath ? $arguments[0] : new FilePath($arguments[0]); |
|
103
|
|
|
$version = $arguments[1] instanceof Version ? $arguments[1] : Version::fromString($arguments[1]); |
|
104
|
|
|
$domain = new $fqcn($filepath, $version); |
|
105
|
|
|
if (! $domain instanceof DomainBase || ! $domain instanceof $fqcn) { |
|
106
|
|
|
throw new DomainException( |
|
107
|
|
|
sprintf( |
|
108
|
|
|
esc_html__( |
|
109
|
|
|
'The requested Domain class "%1$s" could not be loaded.', |
|
110
|
|
|
'event_espresso' |
|
111
|
|
|
), |
|
112
|
|
|
$fqcn |
|
113
|
|
|
) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
DomainFactory::$domains[ $fqcn ] = $domain; |
|
117
|
|
|
// we still need to share this with the core loader to facilitate automatic dependency injection |
|
118
|
|
|
LoaderFactory::getLoader()->share($fqcn, $domain, [$filepath, $version, $domain->assetNamespace()]); |
|
119
|
|
|
} |
|
120
|
|
|
return DomainFactory::$domains[ $fqcn ]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|