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
|
|
|
|
52
|
|
|
if (empty($dirname)) { |
53
|
|
|
// nothing specified, use current module |
54
|
|
|
// check if we are running in 2.6 |
55
|
|
|
if (class_exists('Xoops', false)) { |
56
|
|
|
$xoops = \Xoops::getInstance(); |
57
|
|
|
if ($xoops->isModule()) { |
58
|
|
|
$this->module = $xoops->module; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
$this->module = $GLOBALS['xoopsModule']; |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
// assume dirname specified, try to get a module object |
65
|
|
|
$moduleHandler = xoops_getHandler('module'); |
|
|
|
|
66
|
|
|
$this->module = $moduleHandler->getByDirname($dirname); |
67
|
|
|
} |
68
|
|
|
if (is_object($this->module)) { |
69
|
|
|
$this->init(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* init() is called once/if __construct has a module object. |
75
|
|
|
* $this->module will have a module object that any further |
76
|
|
|
* initialization can use. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
abstract public function init(); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set debug option on or off |
84
|
|
|
* |
85
|
|
|
* @param bool $bool true to turn on debug logging, false for off |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
1 |
|
public function setDebug($bool = true) |
90
|
|
|
{ |
91
|
1 |
|
$this->debug = (bool) $bool; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add a message to the module log |
96
|
|
|
* |
97
|
|
|
* @param string $log log message |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
1 |
|
public function addLog($log) |
102
|
|
|
{ |
103
|
1 |
|
if ($this->debug) { |
104
|
|
|
\Xoops::getInstance()->logger()->debug($log, array('channel'=>'Extra')); |
105
|
|
|
} |
106
|
1 |
|
} |
107
|
|
|
} |
108
|
|
|
|
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: