This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
/
base.php
271 lines (243 loc) · 5.87 KB
/
base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* @package Joomla.Platform
* @subpackage Application
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
jimport('joomla.event.dispatcher');
/**
* Joomla Platform Base Application Class
*
* @package Joomla.Platform
* @subpackage Application
* @since 12.1
*/
abstract class JApplicationBase
{
/**
* The application input object.
*
* @var JInput
* @since 12.1
*/
public $input;
/**
* The application configuration object.
*
* @var JRegistry
* @since 12.1
*/
protected $config;
/**
* The character encoding string.
*
* @var string
* @since 12.1
*/
protected $charSet = 'utf-8';
/**
* The application dispatcher object.
*
* @var JDispatcher
* @since 12.1
*/
protected $dispatcher;
/**
* The application instance.
*
* @var JApplicationBase
* @since 12.1
*/
protected static $instance;
/**
* Method to close the application.
*
* @param integer $code The exit code (optional; default is 0).
*
* @return void
*
* @since 12.1
*/
public function close($code = 0)
{
exit($code);
}
/**
* Method to execute the application.
*
* @return void
*
* @since 12.1
*/
abstract public function execute();
/**
* Method to get a property of the application or the default value if the property is not set.
*
* @param string $key The name of the property.
* @param mixed $default The default value (optional) if none is set.
*
* @return mixed The value of the configuration.
*
* @since 12.1
*/
public function get($key, $default = null)
{
return $this->config->get($key, $default);
}
/**
* Method to get the application character set.
*
* @return string The character set.
*
* @since 12.1
*/
public function getCharacterSet()
{
return $this->charSet;
}
/**
* Method to load an object or array into the application configuration object.
*
* @param mixed $data Either an array or object to be loaded into the configuration object.
*
* @return JApplicationBase The application to allow chaining.
*
* @since 12.1
*/
public function loadConfiguration($data)
{
// Load the data into the configuration object.
if (is_array($data))
{
$this->config->loadArray($data);
}
elseif (is_object($data))
{
$this->config->loadObject($data);
}
return $this;
}
/**
* Registers a handler to a particular event group.
*
* @param string $event The event name.
* @param callback $handler The handler, a function or an instance of a event object.
*
* @return JApplicationBase The application to allow chaining.
*
* @since 12.1
*/
public function registerEvent($event, $handler)
{
if ($this->dispatcher instanceof JDispatcher)
{
$this->dispatcher->register($event, $handler);
}
return $this;
}
/**
* Method to set a property of the application, creating it if it does not already exist.
*
* @param string $key The name of the property.
* @param mixed $value The value of the property to set (optional).
*
* @return mixed Previous value of the property
*
* @since 12.1
*/
public function set($key, $value = null)
{
$previous = $this->config->get($key);
$this->config->set($key, $value);
return $previous;
}
/**
* Method to set the application character set.
*
* @param string $charset The character set.
*
* @return JApplicationBase The application to allow chaining.
*
* @since 12.1
*/
public function setCharacterSet($charset)
{
$this->charSet = $charset;
return $this;
}
/**
* Calls all handlers associated with an event group.
*
* @param string $event The event name.
* @param array $args An array of arguments (optional).
*
* @return array An array of results from each function call, or null if no dispatcher is defined.
*
* @since 12.1
*/
public function triggerEvent($event, array $args = null)
{
if ($this->dispatcher instanceof JDispatcher)
{
return $this->dispatcher->trigger($event, $args);
}
return null;
}
/**
* Method to load a PHP configuration class file based on convention and return the instantiated data object. You
* will extend this method in child classes to provide configuration data from whatever data source is relevant
* for your specific application.
*
* @param string $file The path and filename of the configuration file. If not provided, configuration.php
* in JPATH_BASE will be used.
* @param string $class The class name to instantiate.
*
* @return mixed Either an array or object to be loaded into the configuration object.
*
* @since 12.1
*/
protected function fetchConfigurationData($file = '', $class = 'JConfig')
{
// Instantiate variables.
$config = array();
if (empty($file) && defined('JPATH_BASE'))
{
$file = JPATH_BASE . '/configuration.php';
// Applications can choose not to have any configuration data
// by not implementing this method and not having a config file.
if (!file_exists($file))
{
$file = '';
}
}
if (!empty($file))
{
JLoader::register($class, $file);
if (class_exists($class))
{
$config = new $class;
}
else
{
throw new RuntimeException('Configuration class does not exist.');
}
}
return $config;
}
/**
* Method to create an event dispatcher for the application. The logic and options for creating
* this object are adequately generic for default cases but for many applications it will make
* sense to override this method and create event dispatchers based on more specific needs.
*
* @return void
*
* @since 12.1
*/
protected function loadDispatcher()
{
$this->dispatcher = JDispatcher::getInstance();
}
}