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 Xmf\Module\Helper;
13:
14: /**
15: * Xmf\Module\Helper\AbstractHelper defines the basis for various
16: * helpers that simplify routine module tasks.
17: *
18: * @category Xmf\Module\Helper\AbstractHelper
19: * @package Xmf
20: * @author trabis <lusopoemas@gmail.com>
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2016-2018 XOOPS Project (https://xoops.org)
23: * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24: * @link https://xoops.org
25: */
26: abstract class AbstractHelper
27: {
28: /**
29: * @var string module directory name
30: */
31: protected $dirname;
32:
33: /**
34: * @var XoopsModule
35: */
36: protected $module;
37:
38: /**
39: * @var bool true if debug is enabled
40: */
41: protected $debug;
42:
43: /**
44: * Instantiate a XoopsModule object for the helper to use.
45: * The module is determined as follows:
46: * - if null is passed, use the current module
47: * - if a string is passed, use as dirname to load
48: *
49: * @param string|null $dirname dirname
50: */
51: public function __construct($dirname = null)
52: {
53: $this->module = null;
54:
55: if (class_exists('Xoops', false)) {
56: $xoops = \Xoops::getInstance();
57: }
58: if (empty($dirname)) {
59: // nothing specified, use current module
60: if (isset($xoops)) {
61: $this->module = $xoops->module;
62: } else {
63: $this->module = $GLOBALS['xoopsModule'];
64: }
65: } else {
66: // assume dirname specified, try to get a module object
67: if (isset($xoops)) {
68: $moduleHandler = $xoops->getHandlerModule();
69: } else {
70: /** @var \XoopsModuleHandler $moduleHandler */
71: $moduleHandler = xoops_getHandler('module');
72: }
73: $this->module = $moduleHandler->getByDirname($dirname);
74: }
75: if (is_object($this->module)) {
76: $this->dirname = $this->module->getVar('dirname');
77: $this->init();
78: }
79: }
80:
81: /**
82: * init() is called once/if __construct has a module object.
83: * $this->module will have a module object that any further
84: * initialization can use.
85: *
86: * @return void
87: */
88: abstract public function init();
89:
90: /**
91: * Return the dirname for this helper
92: *
93: * @return string|null a dirname
94: */
95: public function dirname()
96: {
97: return $this->dirname;
98: }
99:
100: /**
101: * Set debug option on or off
102: *
103: * @param bool $bool true to turn on debug logging, false for off
104: *
105: * @return void
106: */
107: public function setDebug($bool = true)
108: {
109: $this->debug = (bool) $bool;
110: }
111:
112: /**
113: * Add a message to the module log
114: *
115: * @param mixed $log log item, can be message or variable
116: *
117: * @return void
118: */
119: public function addLog($log)
120: {
121: if ($this->debug) {
122: $message = $this->serializeForHelperLog($log);
123: if (class_exists('Xoops', false)) {
124: \Xoops::getInstance()->logger()->debug($message, array('channel'=>'Extra'));
125: } elseif (is_object($GLOBALS['xoopsLogger'])) {
126: $GLOBALS['xoopsLogger']->addExtra(get_called_class(), $message);
127: }
128: }
129: }
130:
131: /**
132: * Serialize an arbitrary value to string. Intended for data being addLog()ed
133: *
134: * @param mixed $value
135: *
136: * @return string
137: */
138: protected function serializeForHelperLog($value)
139: {
140: if (is_resource($value)) {
141: $value = '(resource:' . get_resource_type($value) . ')';
142: }
143: if (!is_string($value)) {
144: $value = json_encode($value);
145: }
146: return (string) $value;
147: }
148: }
149: