Issues (3210)

htdocs/class/model/xoopsmodel.php (2 issues)

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-2025 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @subpackage          model
16
 * @since               2.3.0
17
 * @author              Taiwen Jiang <[email protected]>
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 <[email protected]>
27
 * @package kernel
28
 */
29
class XoopsModelFactory
30
{
31
    /**
32
     * static private
33
     */
34
    public $handlers = [];
35
36
    /**
37
     * XoopsModelFactory::__construct()
38
     */
39
    protected function __construct() {}
40
41
    /**
42
     * Get singleton instance
43
     *
44
     * @access public
45
     */
46
    public function getInstance()
47
    {
48
        static $instance;
49
        if (!isset($instance)) {
50
            $class    = self::class;
51
            $instance = new $class();
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     * Load object handler
59
     *
60
     * @access   public
61
     *
62
     * @param XoopsPersistableObjectHandler $ohandler reference to {@link XoopsPersistableObjectHandler}
63
     * @param string $name     handler name
64
     * @param mixed  $args     args
65
     *
66
     * @internal param XoopsPersistableObjectHandler $ohandler reference to {@link XoopsPersistableObjectHandler}
67
     * @return object of handler
68
     */
69
    public static function loadHandler(XoopsPersistableObjectHandler $ohandler, $name, $args = null)
70
    {
71
        static $handlers;
72
        if (!isset($handlers[$name])) {
73
            if (file_exists($file = __DIR__ . '/' . $name . '.php')) {
74
                include_once $file;
75
                $className = 'XoopsModel' . ucfirst($name);
76
                $handler   = new $className();
77
            } elseif (xoops_load('model', 'framework')) {
78
                $handler = XoopsModel::loadHandler($name);
79
            }
80
            if (!is_object($handler)) {
81
                trigger_error('Handler not found in file ' . __FILE__ . ' at line ' . __LINE__, E_USER_WARNING);
82
83
                return null;
84
            }
85
            $handlers[$name] = $handler;
86
        }
87
        $handlers[$name]->setHandler($ohandler);
88
        if (!empty($args) && \is_array($args) && is_a($handlers[$name], 'XoopsModelAbstract')) {
89
            $handlers[$name]->setVars($args);
90
        }
91
92
        return $handlers[$name];
93
    }
94
}
95
96
/**
97
 * abstract class object handler
98
 *
99
 * @author  Taiwen Jiang <[email protected]>
100
 * @package kernel
101
 */
102
class XoopsModelAbstract
103
{
104
    /**
105
     * holds referenced to handler object
106
     *
107
     * @var   object
108
     * @param XoopsPersistableObjectHandler $ohandler reference to {@link XoopsPersistableObjectHandler}
109
     * @access protected
110
     */
111
    public $handler;
112
113
    /**
114
     * constructor
115
     *
116
     * normally, this is called from child classes only
117
     *
118
     * @access protected
119
     * @param null $args
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $args is correct as it would always require null to be passed?
Loading history...
120
     * @param null $handler
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $handler is correct as it would always require null to be passed?
Loading history...
121
     */
122
    public function __construct($args = null, $handler = null)
123
    {
124
        $this->setHandler($handler);
125
        $this->setVars($args);
126
    }
127
128
    /**
129
     * XoopsModelAbstract::setHandler()
130
     *
131
     * @param  mixed $handler
132
     * @return bool
133
     */
134
    public function setHandler($handler)
135
    {
136
        if (is_object($handler) && is_a($handler, 'XoopsPersistableObjectHandler')) {
137
            $this->handler = & $handler;
138
139
            return true;
140
        }
141
142
        return false;
143
    }
144
145
    /**
146
     * XoopsModelAbstract::setVars()
147
     *
148
     * @param  mixed $args
149
     * @return bool
150
     */
151
    public function setVars($args)
152
    {
153
        if (!empty($args) && \is_array($args)) {
154
            foreach ($args as $key => $value) {
155
                $this->$key = $value;
156
            }
157
        }
158
159
        return true;
160
    }
161
}
162