|
1
|
|
|
<?php namespace XoopsModules\Smartobject; |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* You may not change or alter any portion of this comment or credits |
|
5
|
|
|
* of supporting developers from this source code or any supporting source code |
|
6
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
7
|
|
|
* |
|
8
|
|
|
* This program is distributed in the hope that it will be useful, |
|
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @copyright XOOPS Project https://xoops.org/ |
|
15
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
16
|
|
|
* @package |
|
17
|
|
|
* @since |
|
18
|
|
|
* @author XOOPS Development Team |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use CriteriaCompo; |
|
22
|
|
|
use XoopsModules\Smartobject; |
|
23
|
|
|
|
|
24
|
|
|
// defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* SmartObjects Registry |
|
28
|
|
|
* |
|
29
|
|
|
* The SmartObjects Registry is an object containing SmartObject objects that will be reused in the same process |
|
30
|
|
|
* |
|
31
|
|
|
* @package SmartObject |
|
32
|
|
|
* @author marcan <[email protected]> |
|
33
|
|
|
* @link http://smartfactory.ca The SmartFactory |
|
34
|
|
|
*/ |
|
35
|
|
|
class ObjectsRegistry |
|
36
|
|
|
{ |
|
37
|
|
|
public $_registryArray; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Access the only instance of this class |
|
41
|
|
|
* |
|
42
|
|
|
* @return \XoopsModules\Smartobject\ObjectsRegistry |
|
43
|
|
|
* |
|
44
|
|
|
* @static |
|
45
|
|
|
* @staticvar object |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function getInstance() |
|
48
|
|
|
{ |
|
49
|
|
|
static $instance; |
|
50
|
|
|
if (null === $instance) { |
|
51
|
|
|
$instance = new static(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $instance; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Adding objects to the registry |
|
59
|
|
|
* |
|
60
|
|
|
* @param PersistableObjectHandler $handler of the objects to add |
|
61
|
|
|
* @param bool|CriteriaCompo $criteria to pass to the getObjects method of the handler (with id_as_key) |
|
62
|
|
|
* @return FALSE if an error occured |
|
63
|
|
|
*/ |
|
64
|
|
|
public function addObjectsFromHandler(&$handler, $criteria = false) |
|
65
|
|
|
{ |
|
66
|
|
View Code Duplication |
if (method_exists($handler, 'getObjects')) { |
|
|
|
|
|
|
67
|
|
|
$objects = $handler->getObjects($criteria, true); |
|
68
|
|
|
$this->_registryArray['objects'][$handler->_moduleName][$handler->_itemname] = $objects; |
|
69
|
|
|
|
|
70
|
|
|
return $objects; |
|
71
|
|
|
} else { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Adding objects to the registry from an item name |
|
78
|
|
|
* This method will fetch the handler of the item / module and call the addObjectsFromHandler |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $item name of the item |
|
81
|
|
|
* @param bool|string $modulename name of the module |
|
82
|
|
|
* @param bool|CriteriaCompo $criteria to pass to the getObjects method of the handler (with id_as_key) |
|
83
|
|
|
* @return FALSE if an error occured |
|
84
|
|
|
*/ |
|
85
|
|
|
public function addObjectsFromItemName($item, $modulename = false, $criteria = false) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$modulename) { |
|
88
|
|
|
global $xoopsModule; |
|
89
|
|
|
if (!is_object($xoopsModule)) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} else { |
|
92
|
|
|
$modulename = $xoopsModule->dirname(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
$objectHandler = xoops_getModuleHandler($item, $modulename); |
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
if (method_exists($objectHandler, 'getObjects')) { |
|
|
|
|
|
|
98
|
|
|
$objects = $objectHandler->getObjects($criteria, true); |
|
99
|
|
|
$this->_registryArray['objects'][$objectHandler->_moduleName][$objectHandler->_itemname] = $objects; |
|
100
|
|
|
|
|
101
|
|
|
return $objects; |
|
102
|
|
|
} else { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Fetching objects from the registry |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $itemname |
|
111
|
|
|
* @param string $modulename |
|
112
|
|
|
* |
|
113
|
|
|
* @return false|array the requested objects or FALSE if they don't exists in the registry |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function getObjects($itemname, $modulename) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
if (!$modulename) { |
|
118
|
|
|
global $xoopsModule; |
|
119
|
|
|
if (!is_object($xoopsModule)) { |
|
120
|
|
|
return false; |
|
121
|
|
|
} else { |
|
122
|
|
|
$modulename = $xoopsModule->dirname(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
if (isset($this->_registryArray['objects'][$modulename][$itemname])) { |
|
126
|
|
|
return $this->_registryArray['objects'][$modulename][$itemname]; |
|
127
|
|
|
} else { |
|
128
|
|
|
// if they were not in registry, let's fetch them and add them to the reigistry |
|
129
|
|
|
$moduleHandler = xoops_getModuleHandler($itemname, $modulename); |
|
130
|
|
|
if (method_exists($moduleHandler, 'getObjects')) { |
|
131
|
|
|
$objects = $moduleHandler->getObjects(); |
|
132
|
|
|
} |
|
133
|
|
|
$this->_registryArray['objects'][$modulename][$itemname] = $objects; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
return $objects; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Fetching objects from the registry, as a list: objectid => identifier |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $itemname |
|
143
|
|
|
* @param string $modulename |
|
144
|
|
|
* |
|
145
|
|
|
* @return false|array the requested objects or FALSE if they don't exists in the registry |
|
146
|
|
|
*/ |
|
147
|
|
View Code Duplication |
public function getList($itemname, $modulename) |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
|
|
if (!$modulename) { |
|
150
|
|
|
global $xoopsModule; |
|
151
|
|
|
if (!is_object($xoopsModule)) { |
|
152
|
|
|
return false; |
|
153
|
|
|
} else { |
|
154
|
|
|
$modulename = $xoopsModule->dirname(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
if (isset($this->_registryArray['list'][$modulename][$itemname])) { |
|
158
|
|
|
return $this->_registryArray['list'][$modulename][$itemname]; |
|
159
|
|
|
} else { |
|
160
|
|
|
// if they were not in registry, let's fetch them and add them to the reigistry |
|
161
|
|
|
$moduleHandler = xoops_getModuleHandler($itemname, $modulename); |
|
162
|
|
|
if (method_exists($moduleHandler, 'getList')) { |
|
163
|
|
|
$objects = $moduleHandler->getList(); |
|
164
|
|
|
} |
|
165
|
|
|
$this->_registryArray['list'][$modulename][$itemname] = $objects; |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
return $objects; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Retreive a single object |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $itemname |
|
175
|
|
|
* @param string $key |
|
176
|
|
|
* |
|
177
|
|
|
* @param bool $modulename |
|
178
|
|
|
* @return false|\XoopsObject the requestd object or FALSE if they don't exists in the registry |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getSingleObject($itemname, $key, $modulename = false) |
|
181
|
|
|
{ |
|
182
|
|
|
if (!$modulename) { |
|
183
|
|
|
global $xoopsModule; |
|
184
|
|
|
if (!is_object($xoopsModule)) { |
|
185
|
|
|
return false; |
|
186
|
|
|
} else { |
|
187
|
|
|
$modulename = $xoopsModule->dirname(); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
if (isset($this->_registryArray['objects'][$modulename][$itemname][$key])) { |
|
191
|
|
|
return $this->_registryArray['objects'][$modulename][$itemname][$key]; |
|
192
|
|
|
} else { |
|
193
|
|
|
$objectHandler = xoops_getModuleHandler($itemname, $modulename); |
|
194
|
|
|
$object = $objectHandler->get($key); |
|
195
|
|
|
if (!$object->isNew()) { |
|
196
|
|
|
return $object; |
|
197
|
|
|
} else { |
|
198
|
|
|
return false; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.