1
|
<?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
|
* @package Core
|
24
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
25
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
26
|
* @license GNU General Public License 2.0
|
27
|
* @version 0.0.1
|
28
|
* @revision $Id: CoreAutoloader.php 29 2017-11-14 11:54:20Z Manuela $
|
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 CoreAutoloader
|
44
|
{
|
45
|
|
46
|
private static $sInstallPath = null;
|
47
|
private static $aPatterns = [];
|
48
|
private static $aReplacements = [];
|
49
|
|
50
|
private static $aTransNs = [];
|
51
|
/**
|
52
|
* add new Namespace->Directory relation or overwrite existing
|
53
|
* @param mixed $sNamespace
|
54
|
* @param string $sDirectory (default: '')
|
55
|
*/
|
56
|
public static function addNamespace($mNamespace, $sDirectory = '')
|
57
|
{
|
58
|
if (\is_null(self::$sInstallPath)) {
|
59
|
throw new \RuntimeException('can not add namespaces before autoloader is registered!!');
|
60
|
}
|
61
|
if (\is_string($mNamespace)) {
|
62
|
$mNamespace = [$mNamespace, $sDirectory];
|
63
|
}
|
64
|
if (\is_array($mNamespace)) {
|
65
|
foreach ($mNamespace as $sNamespace => $sDirectory) {
|
66
|
$sNamespace = \trim(\str_replace('\\', '/', $sNamespace), '/');
|
67
|
$sDirectory = \trim(\str_replace('\\', '/', $sDirectory), '/');
|
68
|
self::$aTransNs[$sNamespace] = $sDirectory;
|
69
|
}
|
70
|
\krsort(self::$aTransNs);
|
71
|
self::$aPatterns = self::$aReplacements = [];
|
72
|
foreach (self::$aTransNs as $sPattern => $sReplacement) {
|
73
|
self::$aPatterns[] = '@^('.$sPattern.'/)@su';
|
74
|
self::$aReplacements[] = $sReplacement.'/';
|
75
|
}
|
76
|
}
|
77
|
}
|
78
|
|
79
|
public static function autoLoad($sClassName)
|
80
|
{
|
81
|
$sTmp = \basename($sClassName);
|
82
|
if (\strtolower(\substr($sTmp, -5)) == 'trait') {
|
83
|
$sClassName = 'bin\traits\\'.$sTmp;
|
84
|
}
|
85
|
$aMatches = \preg_split(
|
86
|
'=/=',
|
87
|
\str_replace('\\', '/',$sClassName.'.php'),
|
88
|
null,
|
89
|
\PREG_SPLIT_NO_EMPTY
|
90
|
);
|
91
|
// insert default NS if no one is given
|
92
|
if (\sizeof($aMatches) == 1) { \array_unshift($aMatches, 'bin'); }
|
93
|
// extract default filename
|
94
|
$sClassFileName = \array_pop($aMatches);
|
95
|
// translate namespaces into the real dir entries
|
96
|
$sClassDirName = self::$sInstallPath.\preg_replace(
|
97
|
self::$aPatterns,
|
98
|
self::$aReplacements,
|
99
|
\implode('/', $aMatches).'/'
|
100
|
);
|
101
|
// first seek normal filename
|
102
|
$sFilePath = $sClassDirName.$sClassFileName;
|
103
|
if (\is_readable($sFilePath)) {
|
104
|
include $sFilePath;
|
105
|
} else {
|
106
|
// second seek filename with prefix 'class.'
|
107
|
$sFilePath = $sClassDirName.'class.'.$sClassFileName;
|
108
|
if (\is_readable($sFilePath)) {
|
109
|
include $sFilePath;
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
/**
|
114
|
* register this autoloader
|
115
|
*/
|
116
|
public static function doRegister($sPathPrefix)
|
117
|
{
|
118
|
self::$sInstallPath = \rtrim(\str_replace('\\', '/', $sPathPrefix), '/').'/';
|
119
|
if (\is_dir(self::$sInstallPath) && \is_readable(self::$sInstallPath)) {
|
120
|
\krsort(self::$aTransNs);
|
121
|
foreach (self::$aTransNs as $sPattern => $sReplacement) {
|
122
|
self::$aPatterns[] = '@^('.$sPattern.'/)@su';
|
123
|
self::$aReplacements[] = $sReplacement.'/';
|
124
|
}
|
125
|
\spl_autoload_register([__CLASS__, 'autoLoad']);
|
126
|
} else {
|
127
|
throw new \RuntimeException('invalid PathPrefix given!!');
|
128
|
}
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* unregister this autoloader
|
133
|
*/
|
134
|
public static function unRegister()
|
135
|
{
|
136
|
\spl_autoload_unregister([__CLASS__, 'autoLoad']);
|
137
|
}
|
138
|
|
139
|
}
|