Project

General

Profile

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 10 Manuela
//declare(encoding = 'UTF-8');
35 7 Manuela
36
namespace bin;
37
38
// use
39
40
/**
41
 * short description of class
42
 */
43 9 Manuela
class CoreAutoloader
44 7 Manuela
{
45
46 9 Manuela
    private static $sInstallPath = '';
47
    private static $aPatterns = [];
48
    private static $aReplacements = [];
49
50
    private static $aTransNs = [
51 12 Manuela
        'vendor/jscalendar' => 'include/jscalendar',
52 17 Manuela
        'vendor'            => 'include',
53 12 Manuela
        'bin/interfaces'    => 'framework',
54 17 Manuela
        'bin/db'            => 'framework/db',
55 12 Manuela
        'bin'               => 'framework',
56
        'addon'             => 'modules',
57
        'acp'               => 'admin',
58 7 Manuela
    ];
59 17 Manuela
/**
60
 * add new Namespace->Directory relation or overwrite existing
61
 * @param string $sNamespace
62
 * @param string $sDirectory
63
 */
64
    public static function addNamespace($sNamespace, $sDirectory)
65
    {
66
        $sNamespace = \trim(\str_replace('\\', '/', $sNamespace), '/');
67
        $sDirectory = \trim(\str_replace('\\', '/', $sDirectory), '/');
68
        self::$aTransNs[$sNamespace] = $sDirectory;
69
        \krsort(self::$aTransNs);
70
        self::$aPatterns = self::$aReplacements = [];
71
        foreach (self::$aTransNs as $sPattern => $sReplacement) {
72
            self::$aPatterns[]     = '@^('.$sPattern.'/)@su';
73
            self::$aReplacements[] = $sReplacement.'/';
74
        }
75
    }
76 7 Manuela
77
    public static function autoLoad($sClassName)
78
    {
79 9 Manuela
        $aMatches = \preg_split(
80
            '=/=',
81
            \str_replace('\\', '/',$sClassName.'.php'),
82
            null,
83
            \PREG_SPLIT_NO_EMPTY
84
        );
85 7 Manuela
        // insert default NS if no one is given
86 8 Manuela
        if (\sizeof($aMatches) == 1) { \array_unshift($aMatches, 'framework'); }
87 9 Manuela
        // extract default filename
88
        $sClassFileName = \array_pop($aMatches);
89
        // translate namespaces into the real dir entries
90
        $sClassDirName = self::$sInstallPath.\preg_replace(
91
            self::$aPatterns,
92
            self::$aReplacements,
93
            \implode('/', $aMatches).'/'
94
        );
95
        // first seek normal filename
96
        $sFilePath = $sClassDirName.$sClassFileName;
97 8 Manuela
        if (\is_readable($sFilePath)) {
98 7 Manuela
            include $sFilePath;
99
        } else {
100 9 Manuela
            // second seek filename with prefix 'class.'
101
            $sFilePath = $sClassDirName.'class.'.$sClassFileName;
102 8 Manuela
            if (\is_readable($sFilePath)) {
103 7 Manuela
                include $sFilePath;
104
            }
105
        }
106
    }
107
/**
108 8 Manuela
 * register this autoloader
109 7 Manuela
 */
110 10 Manuela
    public static function doRegister(array $aAdditionalTranslations = null)
111 7 Manuela
    {
112 10 Manuela
        if (!is_null($aAdditionalTranslations)) {
113
            foreach ($aAdditionalTranslations as $sKey=>$sValue) {
114
                self::$aTransNs[$sKey] = $sValue;
115
            }
116
        }
117 17 Manuela
        \krsort(self::$aTransNs);
118 8 Manuela
        self::$sInstallPath = \rtrim(\str_replace('\\', '/', \dirname(__DIR__)), '/').'/';
119 9 Manuela
        foreach (self::$aTransNs as $sPattern => $sReplacement) {
120 10 Manuela
            self::$aPatterns[]     = '@^('.$sPattern.'/)@su';
121
            self::$aReplacements[] = $sReplacement.'/';
122 9 Manuela
        }
123
        \spl_autoload_register([__CLASS__, 'autoLoad']);
124 7 Manuela
    }
125
126
/**
127 8 Manuela
 * unregister this autoloader
128 7 Manuela
 */
129
    public static function unRegister()
130
    {
131 9 Manuela
        \spl_autoload_unregister([__CLASS__, 'autoLoad']);
132 7 Manuela
    }
133
134
}