|
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 <[email protected]> |
|
21
|
|
|
* @author Richard Griffith <[email protected]> |
|
22
|
|
|
* @copyright 2011-2016 XOOPS Project (http://xoops.org) |
|
23
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
24
|
|
|
* @version Release: 1.0 |
|
25
|
|
|
* @link http://xoops.org |
|
26
|
|
|
* @since 1.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class AbstractHelper |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var XoopsModule |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $module; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool true if debug is enabled |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $debug; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Instantiate a XoopsModule object for the helper to use. |
|
42
|
|
|
* The module is determined as follows: |
|
43
|
|
|
* - if null is passed, use the current module |
|
44
|
|
|
* - if a string is passed, use as dirname to load |
|
45
|
|
|
* |
|
46
|
|
|
* @param string|null $dirname dirname |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($dirname = null) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$this->module = null; |
|
51
|
|
|
if (class_exists('Xoops', false)) { |
|
52
|
|
|
$xoops = \Xoops::getInstance(); |
|
53
|
|
|
} |
|
54
|
|
|
if (empty($dirname)) { |
|
55
|
|
|
// nothing specified, use current module |
|
56
|
|
|
// check if we are running in 2.6 |
|
57
|
|
|
if (isset($xoops)) { |
|
58
|
|
|
if ($xoops->isModule()) { |
|
59
|
|
|
$this->module = $xoops->module; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->module = $GLOBALS['xoopsModule']; |
|
63
|
|
|
} |
|
64
|
|
|
} else { |
|
65
|
|
|
// assume dirname specified, try to get a module object |
|
66
|
|
|
if (isset($xoops)) { |
|
67
|
|
|
$moduleHandler = $xoops->getHandlerModule(); |
|
68
|
|
|
} else { |
|
69
|
|
|
$moduleHandler = xoops_getHandler('module'); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
$this->module = $moduleHandler->getByDirname($dirname); |
|
72
|
|
|
} |
|
73
|
|
|
if (is_object($this->module)) { |
|
74
|
|
|
$this->init(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* init() is called once/if __construct has a module object. |
|
80
|
|
|
* $this->module will have a module object that any further |
|
81
|
|
|
* initialization can use. |
|
82
|
|
|
* |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
abstract public function init(); |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set debug option on or off |
|
89
|
|
|
* |
|
90
|
|
|
* @param bool $bool true to turn on debug logging, false for off |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function setDebug($bool = true) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$this->debug = (bool) $bool; |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add a message to the module log |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $log log message |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function addLog($log) |
|
107
|
|
|
{ |
|
108
|
1 |
|
if ($this->debug) { |
|
109
|
|
|
\Xoops::getInstance()->logger()->debug($log, array('channel'=>'Extra')); |
|
110
|
|
|
} |
|
111
|
1 |
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: