|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\assets; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
use EventEspresso\core\domain\DomainFactory; |
|
7
|
|
|
use EventEspresso\core\domain\DomainInterface; |
|
8
|
|
|
use EventEspresso\core\services\factory\FactoryInterface; |
|
9
|
|
|
use EventEspresso\core\domain\values\FullyQualifiedName; |
|
10
|
|
|
use EventEspresso\core\services\loaders\LoaderInterface; |
|
11
|
|
|
|
|
12
|
|
|
class BaristaFactory implements FactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var AssetManifestFactory |
|
16
|
|
|
*/ |
|
17
|
|
|
private $manifest_factory; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var BaristaInterface[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $baristas = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var LoaderInterface $loader |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $loader; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* BaristaFactory constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param AssetManifestFactory $manifest_factory |
|
34
|
|
|
* @param LoaderInterface $loader |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(AssetManifestFactory $manifest_factory, LoaderInterface $loader) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->manifest_factory = $manifest_factory; |
|
39
|
|
|
$this->loader = $loader; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $domain_fqcn |
|
45
|
|
|
* @return BaristaInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public function createFromDomainClass($domain_fqcn) |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var DomainInterface $domain */ |
|
50
|
|
|
$domain = $this->loader->getShared($domain_fqcn); |
|
51
|
|
|
return $this->createFromDomainObject($domain); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param DomainInterface $domain |
|
57
|
|
|
* @return BaristaInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
public function createFromDomainObject(DomainInterface $domain) |
|
60
|
|
|
{ |
|
61
|
|
|
$asset_manifest = $this->manifest_factory->createFromDomainObject($domain); |
|
62
|
|
|
return $this->getBaristaForDomain($asset_manifest, $domain); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $domain_fqcn Fully Qualified Class Name for the applicable DomainInterface class |
|
68
|
|
|
* @param array $domain_arguments arguments required by the applicable DomainInterface class |
|
69
|
|
|
* @return BaristaInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public function create($domain_fqcn = '', array $domain_arguments = []) |
|
72
|
|
|
{ |
|
73
|
|
|
$domain = $this->getDomain($domain_fqcn, $domain_arguments); |
|
74
|
|
|
$asset_manifest = $this->manifest_factory->createFromDomainObject($domain); |
|
75
|
|
|
return $this->getBaristaForDomain($asset_manifest, $domain); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param AssetManifestInterface $asset_manifest |
|
81
|
|
|
* @param DomainInterface $domain |
|
82
|
|
|
* @return BaristaInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
private function getBaristaForDomain(AssetManifestInterface $asset_manifest, DomainInterface $domain) |
|
85
|
|
|
{ |
|
86
|
|
|
$domain_fqcn = get_class($domain); |
|
87
|
|
|
if (! isset(BaristaFactory::$baristas[ $domain_fqcn ])) { |
|
88
|
|
|
$barista = new Barista($asset_manifest); |
|
89
|
|
|
// we still need to share this with the core loader to facilitate automatic dependency injection |
|
90
|
|
|
$this->loader->share(Barista::class, $barista, [$asset_manifest]); |
|
91
|
|
|
BaristaFactory::$baristas[ $domain_fqcn ] = $barista; |
|
92
|
|
|
} |
|
93
|
|
|
return BaristaFactory::$baristas[ $domain_fqcn ]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $domain_fqcn Fully Qualified Class Name for the applicable DomainInterface class |
|
99
|
|
|
* @param array $arguments |
|
100
|
|
|
* @return DomainInterface |
|
101
|
|
|
*/ |
|
102
|
|
|
private function getDomain($domain_fqcn, array $arguments = []) |
|
103
|
|
|
{ |
|
104
|
|
|
// if no FQCN is supplied for the domain, then we are loading the defaults for core |
|
105
|
|
|
// add-ons will always have to supply their domain FQCN and arguments to retrieve their manifest |
|
106
|
|
|
$domain = empty($domain_fqcn) |
|
107
|
|
|
? DomainFactory::getEventEspressoCoreDomain() |
|
108
|
|
|
: DomainFactory::getShared(new FullyQualifiedName($domain_fqcn), $arguments); |
|
109
|
|
|
if ($domain instanceof DomainInterface) { |
|
110
|
|
|
return $domain; |
|
111
|
|
|
} |
|
112
|
|
|
throw new DomainException( |
|
113
|
|
|
sprintf( |
|
114
|
|
|
esc_html__( |
|
115
|
|
|
'BaristaFactory::create() requires a fully qualified class name (FQCN) for the currently applicable Domain object. |
|
116
|
|
|
%1$sThe supplied FQCN ("%2$s") is either invalid or the class is missing.', |
|
117
|
|
|
'event_espresso' |
|
118
|
|
|
), |
|
119
|
|
|
'<br />', |
|
120
|
|
|
$domain_fqcn |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|