-
Notifications
You must be signed in to change notification settings - Fork 1
/
DomainManager.php
120 lines (108 loc) · 3.73 KB
/
DomainManager.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
<?php
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
namespace Xmf\Xadr;
use Xmf\Xadr\Exceptions\DomainFailureException;
use Xmf\Xadr\Exceptions\InvalidDomainException;
/**
* A DomainManager manages the loading, start up and shut down of models.
*
* @category Xmf\Xadr\DomainManager
* @package Xmf
* @author Richard Griffith <richard@geekwright.com>
* @copyright 2013-2015 XOOPS Project (http://xoops.org)
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @link http://xoops.org
*/
class DomainManager extends ContextAware
{
/**
* An associative array of Domain objects, keyed by unit and name
*
* @var array
*/
protected $domains = array();
/**
* An indexed array of loaded domains, each element as array('unit' => $unit,'name' => $name))
*
* @var array
*/
protected $domainOrder=array();
/**
* Return a domain instance.
*
* @param string $domainName A Domain name.
* @param string|null $unitName A unit name, defaults to current unit
*
* @return Domain
*
* @throws DomainFailureException
* @throws InvalidDomainException
*/
public function getDomain($domainName, $unitName = null)
{
if ($unitName === null) {
$unitName = $this->controller()->getCurrentUnit();
}
if (!isset($this->domains[$unitName][$domainName])) {
$domain = $this->controller()->getDomainComponent($domainName, $unitName);
if (!($domain instanceof Domain)) {
throw new InvalidDomainException($this->notDomainMessage($domainName));
}
$initStatus = false;
try {
$initStatus = $domain->initialize();
} catch (\InvalidArgumentException $e) {
throw new DomainFailureException($this->notInitializedDomainMessage($domainName), 0, $e);
}
if (!$initStatus) {
throw new DomainFailureException($this->notInitializedDomainMessage($domainName));
}
$this->domains[$unitName][$domainName]=$domain;
// add to head of list - will shutdown in reverse order of adding
array_unshift($this->domainOrder, array('unit' => $unitName, 'name' => $domainName));
}
return $this->domains[$unitName][$domainName];
}
/**
* Shutdown the DomainManager
*
* @return boolean true if all domains reported clean shutdown otherwise false
*/
public function shutdown()
{
$return = true;
foreach ($this->domainOrder as $domain) {
$return &= $this->domains[$domain['unit']][$domain['name']]->cleanup();
}
return (boolean) $return;
}
/**
* Prepare message for not a domain object exception
*
* @param string $domainName domain name
*
* @return string translated message with name inserted
*/
private function notDomainMessage($domainName)
{
return sprintf('%s is not a Domain object', $domainName);
}
/**
* Prepare message for a domain initialize failure
*
* @param string $domainName domain name
*
* @return string translated message with name inserted
*/
private function notInitializedDomainMessage($domainName)
{
return sprintf('Domain $s did not initialize', $domainName);
}
}