1: <?php
2: /**
3: * Object factory class.
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
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: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @subpackage model
16: * @since 2.3.0
17: * @author Taiwen Jiang <phppp@users.sourceforge.net>
18: */
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: include_once XOOPS_ROOT_PATH . '/kernel/object.php';
22:
23: /**
24: * Factory for object handlers
25: *
26: * @author Taiwen Jiang <phppp@users.sourceforge.net>
27: * @package kernel
28: */
29: class XoopsModelFactory
30: {
31: /**
32: * static private
33: */
34: public $handlers = array();
35:
36: /**
37: * XoopsModelFactory::__construct()
38: */
39: protected function __construct()
40: {
41: }
42:
43: /**
44: * Get singleton instance
45: *
46: * @access public
47: */
48: public function getInstance()
49: {
50: static $instance;
51: if (!isset($instance)) {
52: $class = __CLASS__;
53: $instance = new $class();
54: }
55:
56: return $instance;
57: }
58:
59: /**
60: * Load object handler
61: *
62: * @access public
63: *
64: * @param XoopsPersistableObjectHandler $ohandler reference to {@link XoopsPersistableObjectHandler}
65: * @param string $name handler name
66: * @param mixed $args args
67: *
68: * @internal param XoopsPersistableObjectHandler $ohandler reference to {@link XoopsPersistableObjectHandler}
69: * @return object of handler
70: */
71: public static function loadHandler(XoopsPersistableObjectHandler $ohandler, $name, $args = null)
72: {
73: static $handlers;
74: if (!isset($handlers[$name])) {
75: if (file_exists($file = __DIR__ . '/' . $name . '.php')) {
76: include_once $file;
77: $className = 'XoopsModel' . ucfirst($name);
78: $handler = new $className();
79: } elseif (xoops_load('model', 'framework')) {
80: $handler = XoopsModel::loadHandler($name);
81: }
82: if (!is_object($handler)) {
83: trigger_error('Handler not found in file ' . __FILE__ . 'at line ' . __LINE__, E_USER_WARNING);
84:
85: return null;
86: }
87: $handlers[$name] = $handler;
88: }
89: $handlers[$name]->setHandler($ohandler);
90: if (!empty($args) && is_array($args) && is_a($handlers[$name], 'XoopsModelAbstract')) {
91: $handlers[$name]->setVars($args);
92: }
93:
94: return $handlers[$name];
95: }
96: }
97:
98: /**
99: * abstract class object handler
100: *
101: * @author Taiwen Jiang <phppp@users.sourceforge.net>
102: * @package kernel
103: */
104: class XoopsModelAbstract
105: {
106: /**
107: * holds referenced to handler object
108: *
109: * @var object
110: * @param XoopsPersistableObjectHandler $ohandler reference to {@link XoopsPersistableObjectHandler}
111: * @access protected
112: */
113: public $handler;
114:
115: /**
116: * constructor
117: *
118: * normally, this is called from child classes only
119: *
120: * @access protected
121: * @param null $args
122: * @param null $handler
123: */
124: public function __construct($args = null, $handler = null)
125: {
126: $this->setHandler($handler);
127: $this->setVars($args);
128: }
129:
130: /**
131: * XoopsModelAbstract::setHandler()
132: *
133: * @param mixed $handler
134: * @return bool
135: */
136: public function setHandler($handler)
137: {
138: if (is_object($handler) && is_a($handler, 'XoopsPersistableObjectHandler')) {
139: $this->handler =& $handler;
140:
141: return true;
142: }
143:
144: return false;
145: }
146:
147: /**
148: * XoopsModelAbstract::setVars()
149: *
150: * @param mixed $args
151: * @return bool
152: */
153: public function setVars($args)
154: {
155: if (!empty($args) && is_array($args)) {
156: foreach ($args as $key => $value) {
157: $this->$key = $value;
158: }
159: }
160:
161: return true;
162: }
163: }
164: