1 |
7
|
Manuela
|
<?php
|
2 |
|
|
|
3 |
|
|
/*
|
4 |
|
|
* Copyright (C) 2017 Manuela v.d.Decken <manuela@isteam.de>
|
5 |
|
|
*
|
6 |
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT OR THIS HEADER
|
7 |
|
|
*
|
8 |
|
|
* This program is free software: you can redistribute it and/or modify
|
9 |
|
|
* it under the terms of the GNU General Public License as published by
|
10 |
|
|
* the Free Software Foundation, version 2 of the License.
|
11 |
|
|
*
|
12 |
|
|
* This program is distributed in the hope that it will be useful,
|
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
|
|
* GNU General Public License 2 for more details.
|
16 |
|
|
*
|
17 |
|
|
* You should have received a copy of the GNU General Public License 2
|
18 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19 |
|
|
*/
|
20 |
|
|
/**
|
21 |
|
|
* Description of Autoloader
|
22 |
|
|
*
|
23 |
8
|
Manuela
|
* @package Core
|
24 |
7
|
Manuela
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
25 |
|
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
26 |
|
|
* @license GNU General Public License 3.0
|
27 |
|
|
* @version 0.0.1
|
28 |
|
|
* @revision $Id$
|
29 |
|
|
* @since File available since 05.07.2017
|
30 |
|
|
* @deprecated no / since 0000/00/00
|
31 |
|
|
* @description xxx
|
32 |
|
|
*/
|
33 |
|
|
//declare(strict_types = 1);
|
34 |
|
|
declare(encoding = 'UTF-8');
|
35 |
|
|
|
36 |
|
|
namespace bin;
|
37 |
|
|
|
38 |
|
|
// use
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* short description of class
|
42 |
|
|
*/
|
43 |
|
|
class Autoloader
|
44 |
|
|
{
|
45 |
|
|
|
46 |
|
|
protected static $sInstallPath = '';
|
47 |
|
|
protected static $aTransNs = [
|
48 |
|
|
'bin' => 'framework',
|
49 |
|
|
'addon' => 'modules',
|
50 |
|
|
'acp' => 'admin'
|
51 |
|
|
];
|
52 |
|
|
|
53 |
|
|
public static function autoLoad($sClassName)
|
54 |
|
|
{
|
55 |
|
|
$aMatches = \preg_split('=[\\\\/]=', $sClassName, null, \PREG_SPLIT_NO_EMPTY);
|
56 |
|
|
// insert default NS if no one is given
|
57 |
8
|
Manuela
|
if (\sizeof($aMatches) == 1) { \array_unshift($aMatches, 'framework'); }
|
58 |
7
|
Manuela
|
// translate namespaces into real dir entries
|
59 |
8
|
Manuela
|
$aMatches[0] = \str_replace(\array_keys(self::$aTransNs), self::$aTransNs, $aMatches[0]);
|
60 |
7
|
Manuela
|
// first seek in namespace
|
61 |
|
|
$sFilePath = self::$sInstallPath.\implode('/', $aMatches).'.php';
|
62 |
8
|
Manuela
|
if (\is_readable($sFilePath)) {
|
63 |
7
|
Manuela
|
include $sFilePath;
|
64 |
|
|
} else {
|
65 |
|
|
// second seek in namespace with file prefix 'class.'
|
66 |
8
|
Manuela
|
$sTmp = \array_pop($aMatches);
|
67 |
7
|
Manuela
|
$sFilePath = self::$sInstallPath.\implode('/', $aMatches).'/class.'.$sTmp.'.php';
|
68 |
8
|
Manuela
|
\array_push($aMatches, $sTmp);
|
69 |
|
|
if (\is_readable($sFilePath)) {
|
70 |
7
|
Manuela
|
include $sFilePath;
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
/**
|
75 |
8
|
Manuela
|
* register this autoloader
|
76 |
7
|
Manuela
|
*/
|
77 |
|
|
public static function doRegister()
|
78 |
|
|
{
|
79 |
8
|
Manuela
|
self::$sInstallPath = \rtrim(\str_replace('\\', '/', \dirname(__DIR__)), '/').'/';
|
80 |
7
|
Manuela
|
\spl_autoload_register([self, 'autoLoad']);
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
8
|
Manuela
|
* unregister this autoloader
|
85 |
7
|
Manuela
|
*/
|
86 |
|
|
public static function unRegister()
|
87 |
|
|
{
|
88 |
|
|
\spl_autoload_unregister([self, 'autoLoad']);
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
}
|