1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Kernel;
13:
14: /**
15: * Object factory class.
16: *
17: * @category Xoops\Core\Kernel\XoopsModelFactory
18: * @package Xoops\Core\Kernel
19: * @author Taiwen Jiang <phppp@users.sourceforge.net>
20: * @copyright 2000-2013 XOOPS Project (http://xoops.org)
21: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22: * @link http://xoops.org
23: * @since 2.3.0
24: */
25: class XoopsModelFactory
26: {
27: /**
28: * static private
29: */
30: static private $handlers = array();
31:
32: /**
33: * Get singleton instance
34: *
35: * @access public
36: * @return XoopsModelFactory
37: */
38: public static function getInstance()
39: {
40: static $instance;
41: if (!isset($instance)) {
42: $class = __CLASS__;
43: $instance = new $class();
44: }
45: return $instance;
46: }
47:
48: /**
49: * Load object handler
50: *
51: * @param XoopsPersistableObjectHandler $oHandler handler to load
52: * @param string $name name
53: * @param mixed $args args
54: *
55: * @return null|XoopsModelAbstract
56: */
57: public function loadHandler(XoopsPersistableObjectHandler $oHandler, $name, $args = null)
58: {
59: if (!isset(self::$handlers[$name])) {
60: $handler = null;
61: $className = '\Xoops\Core\Kernel\Model\\' . ucfirst($name);
62: @$handler = new $className();
63: if (!is_object($handler)) {
64: trigger_error('Handler ' . $className . ' not found in file ' . __FILE__, E_USER_WARNING);
65: return null;
66: }
67: self::$handlers[$name] = $handler;
68: }
69: /* @var $handler XoopsModelAbstract */
70: $handler = clone (self::$handlers[$name]);
71: $handler->setHandler($oHandler);
72: if (!empty($args) && is_array($args) && is_a($handler, 'Xoops\Core\Kernel\XoopsModelAbstract')) {
73: $handler->setVars($args);
74: }
75: return $handler;
76: }
77: }
78: