-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLoader.php
322 lines (302 loc) · 9.9 KB
/
Loader.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* Copyright (c) 2017. Alexandr Kosarev, @kosarev.by
*/
namespace Joomplace\X;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Events\Dispatcher as EloquentDispatcher;
use Illuminate\Container\Container;
use Joomla\CMS\Factory;
class Loader
{
protected function __construct()
{
require_once __DIR__ . '/vendor/autoload.php';
self::registerPsr4Autoloader();
if(version_compare(JVERSION,'4.0.0') < 0){
class_alias(Legacy\Dispatcher::class,'\\Joomplace\\X\\Dispatcher');
class_alias(Legacy\Controller::class,'\\Joomplace\\X\\Controller');
class_alias(Legacy\View::class,'\\Joomplace\\X\\View');
}
AbstractPaginator::currentPageResolver(function($var){
return Factory::getApplication()->input->get($var,
Factory::getApplication()->input->get('limitstart',0) /
Factory::getApplication()->input->get('limit',Factory::getConfig()->get('list_limit',20))
+1
);
});
}
public static function boot()
{
static $instance = false;
if(!$instance){
$instance = new self();
}
return $instance;
}
public static function bootDatabase($config = null)
{
static $booted = false;
if(!$booted){
if (!$config) {
$config = \Joomla\CMS\Factory::getConfig();
}
$capsule = new Capsule;
$dbtype = null;
switch ($config->get('dbtype')) {
default:
$dbtype = $config->get('dbtype');
case 'mysqli':
$dbtype = 'mysql';
break;
}
$prefix = $config->get('dbprefix');
$capsule->addConnection(array(
'driver' => $dbtype,
'host' => $config->get('host'),
'database' => $config->get('db'),
'username' => $config->get('user'),
'password' => $config->get('password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => $prefix
));
$dispatcher = new EloquentDispatcher(new Container);
$capsule->setEventDispatcher($dispatcher);
if($config->get('debug',0)){
$capsule->getConnection()->enableQueryLog();
\PlgSystemDebug::addDisplayCallback('Eloquent', function () use ($capsule, $prefix) {
$log = $capsule->getConnection()->getQueryLog();
foreach ($log as $index => $query){
ob_start();
include 'layouts'.DIRECTORY_SEPARATOR.'debug'.DIRECTORY_SEPARATOR.'debug_query.html.php';
$html[] = ob_get_contents();
ob_end_clean();
}
return implode("\n<hr/>\n",$html);
});
}
$capsule->bootEloquent();
$capsule->setAsGlobal();
$booted = true;
// $capsule->getConnection()->listen(function ($sql){
// echo "<pre>";
// print_r($sql);
// echo "</pre>";
// });
}
}
public static function registerPsr4Autoloader(){
spl_autoload_register(array(self::class, 'loadByPsr4'));
}
protected static $vendors = array('Joomplace');
/**
* Allow to register 3rd party vendors
*
* @param string $vendor 3rd party vendor name
*
*
* @since 1.0
*/
public static function registerVendor($vendor)
{
if (!in_array($vendor, self::$vendors))
{
self::$vendors[] = $vendor;
}
}
/**
* Add compatibility for Joomla!CMS (3.x) routing system
*
* @param string $class Router class name
*
* @return bool
*
* @since 1.0
*/
// public static function loadRouterForJoomla($class)
// {
// if (strpos($class, 'Router'))
// {
// $component = str_replace('Router', '', $class);
// foreach (self::$vendors as $vendor)
// {
// $fqcn = $vendor . '\\' . $component . '\\Site\\Router';
// if(class_exists($fqcn, false) || self::loadByPsr4($fqcn))
// {
// class_alias($fqcn,$class);
// return true;
// }
// }
// }
// return false;
// }
/**
* Class loading by PSR-4
*
* @param string $class Fully qualified class name
*
* @return bool Result
*
* @since 1.0
*/
public static function loadByPsr4($class)
{
$files = self::extractExistingPaths($class);
foreach ($files as $path)
{
$return = include_once $path;
if (class_exists($class, false))
{
return (bool) $return;
}
}
return false;
}
/**
* Extract only existing paths by class name
*
* @param string $class Fully qualified class name
* @param string $ext Files extension
* @param bool $override_logic Use override logic of ...
*
* @return array Array of absolute paths
*
* @since 1.0
*/
protected static function extractExistingPaths($class)
{
$paths = self::extractPaths($class);
foreach ($paths as $i => $path)
{
if (!file_exists($path))
{
unset($paths[$i]);
}
}
return $paths;
}
/**
* Extract paths by class name
*
* @param string $class Fully qualified class name
* @param string $ext Files extension
*
* @return array Array of absolute paths
*
* @since 1.0
*/
public static function extractPaths($class)
{
list($classFile, $filePath, $classPathParts, $return) = self::parseClass($class);
if($filePath){
$internal = implode(DIRECTORY_SEPARATOR, $classPathParts);
$internal = $internal ? (DIRECTORY_SEPARATOR . $internal) : '';
foreach ($filePath as $path)
{
$classFilePath = $path . $internal . DIRECTORY_SEPARATOR . $classFile;
$return[] = $classFilePath;
}
}
return $return;
}
/**
* Proxy for extractExistingPaths
*
* @param string $class Fully qualified class name
* @param string $ext Files extension
*
* @return array Array of absolute paths
*
* @since 1.0
*/
public static function getPathByPsr4($class)
{
$paths = self::extractExistingPaths($class);
return $paths;
}
/**
* @param $class
* @param $ext
*
* @return array
*
* @since 1.0
*/
protected static function parseClass($class)
{
// Remove the root backslash if present.
if ($class[0] == '\\')
{
$class = substr($class, 1);
}
// Find the location of the last NS separator.
$pos = strrpos($class, '\\');
// If one is found, we're dealing with a NS'd class.
if ($pos !== false)
{
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
$className = substr($class, $pos + 1);
}
// If not, no need to parse path.
else
{
$classPath = null;
$className = $class;
}
$classFile = $className . '.php';
// Applying logic of "vendor - extension type" on path
$filePath = array();
$classPathParts = array_filter(explode(DIRECTORY_SEPARATOR, $classPath));
// If no $classPathParts then we have nothing to autoload by PSR-4
// And everything that have no $classPathParts needs to register namespaces, also processed in PSR-0
$return = array();
if($classPathParts){
$vendor = ucfirst(array_shift($classPathParts));
$exType = ucfirst(array_shift($classPathParts));
// CLIENT . BASE_PATH - TYPE_FOLDER . (EX_NAME OR VENDOR + EX_NAME ) . ELSE
$folds = array();
switch ($exType)
{
case 'Plugin':
// plugins/type
$folds[] = JPATH_PLUGINS;
$folds[] = lcfirst(array_shift($classPathParts));
$ext = array_shift($classPathParts);
break;
default:
$folds[] = JPATH_LIBRARIES;
$ext = array_shift($classPathParts);
break;
case 'Module':
case 'Component':
$ext = array_shift($classPathParts);
switch (array_shift($classPathParts)){
case 'Admin':
$folds[] = JPATH_ADMINISTRATOR;
break;
default:
$folds[] = JPATH_SITE;
break;
}
$folds[] = lcfirst($exType).'s';
break;
}
$prfx = '';
switch ($exType)
{
case 'Module':
$prfx = 'mod_';
break;
case 'Component':
$prfx = 'com_';
break;
}
$basePath = implode(DIRECTORY_SEPARATOR,$folds);
$filePath[] = $basePath . DIRECTORY_SEPARATOR . $prfx . lcfirst($ext);
$filePath[] = $basePath . DIRECTORY_SEPARATOR . $prfx . lcfirst($vendor) . '_' . lcfirst($ext);
}
return array($classFile, $filePath, $classPathParts, $return);
}
}