wb-2_10_x / branches / main / framework / CoreAutoloader.php @ 10
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 3.0
|
27 |
* @version 0.0.1
|
28 |
* @revision $Id: CoreAutoloader.php 10 2017-09-04 08:08:45Z 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 = ''; |
47 |
private static $aPatterns = []; |
48 |
private static $aReplacements = []; |
49 |
|
50 |
private static $aTransNs = [ |
51 |
'bin/interfaces' => 'framework', |
52 |
'bin' => 'framework', |
53 |
'addon' => 'modules', |
54 |
'acp' => 'admin' |
55 |
]; |
56 |
|
57 |
public static function autoLoad($sClassName) |
58 |
{ |
59 |
$aMatches = \preg_split( |
60 |
'=/=',
|
61 |
\str_replace('\\', '/',$sClassName.'.php'), |
62 |
null,
|
63 |
\PREG_SPLIT_NO_EMPTY
|
64 |
); |
65 |
// insert default NS if no one is given
|
66 |
if (\sizeof($aMatches) == 1) { \array_unshift($aMatches, 'framework'); } |
67 |
// extract default filename
|
68 |
$sClassFileName = \array_pop($aMatches); |
69 |
// translate namespaces into the real dir entries
|
70 |
$sClassDirName = self::$sInstallPath.\preg_replace( |
71 |
self::$aPatterns, |
72 |
self::$aReplacements, |
73 |
\implode('/', $aMatches).'/' |
74 |
); |
75 |
// first seek normal filename
|
76 |
$sFilePath = $sClassDirName.$sClassFileName; |
77 |
if (\is_readable($sFilePath)) { |
78 |
include $sFilePath; |
79 |
} else {
|
80 |
// second seek filename with prefix 'class.'
|
81 |
$sFilePath = $sClassDirName.'class.'.$sClassFileName; |
82 |
if (\is_readable($sFilePath)) { |
83 |
include $sFilePath; |
84 |
} |
85 |
} |
86 |
} |
87 |
/**
|
88 |
* register this autoloader
|
89 |
*/
|
90 |
public static function doRegister(array $aAdditionalTranslations = null) |
91 |
{ |
92 |
if (!is_null($aAdditionalTranslations)) { |
93 |
foreach ($aAdditionalTranslations as $sKey=>$sValue) { |
94 |
self::$aTransNs[$sKey] = $sValue; |
95 |
} |
96 |
} |
97 |
self::$sInstallPath = \rtrim(\str_replace('\\', '/', \dirname(__DIR__)), '/').'/'; |
98 |
foreach (self::$aTransNs as $sPattern => $sReplacement) { |
99 |
self::$aPatterns[] = '@^('.$sPattern.'/)@su'; |
100 |
self::$aReplacements[] = $sReplacement.'/'; |
101 |
} |
102 |
\spl_autoload_register([__CLASS__, 'autoLoad']); |
103 |
} |
104 |
|
105 |
/**
|
106 |
* unregister this autoloader
|
107 |
*/
|
108 |
public static function unRegister() |
109 |
{ |
110 |
\spl_autoload_unregister([__CLASS__, 'autoLoad']); |
111 |
} |
112 |
|
113 |
} |