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 23 Manuela
/*
21 7 Manuela
 * 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 23 Manuela
 * @license      GNU General Public License 2.0
27 7 Manuela
 * @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 23 Manuela
    private static $sInstallPath = null;
47 9 Manuela
    private static $aPatterns = [];
48
    private static $aReplacements = [];
49
50 23 Manuela
    private static $aTransNs = [];
51 17 Manuela
/**
52
 * add new Namespace->Directory relation or overwrite existing
53 23 Manuela
 * @param mixed $sNamespace
54
 * @param string $sDirectory (default: '')
55 17 Manuela
 */
56 23 Manuela
    public static function addNamespace($mNamespace, $sDirectory = '')
57 17 Manuela
    {
58 23 Manuela
        if (\is_null(self::$sInstallPath)) {
59
            throw new \RuntimeException('can not add namespaces before autoloader is registered!!');
60 17 Manuela
        }
61 23 Manuela
        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 17 Manuela
    }
78 7 Manuela
79
    public static function autoLoad($sClassName)
80
    {
81 9 Manuela
        $aMatches = \preg_split(
82
            '=/=',
83
            \str_replace('\\', '/',$sClassName.'.php'),
84
            null,
85
            \PREG_SPLIT_NO_EMPTY
86
        );
87 7 Manuela
        // insert default NS if no one is given
88 8 Manuela
        if (\sizeof($aMatches) == 1) { \array_unshift($aMatches, 'framework'); }
89 9 Manuela
        // extract default filename
90
        $sClassFileName = \array_pop($aMatches);
91
        // translate namespaces into the real dir entries
92
        $sClassDirName = self::$sInstallPath.\preg_replace(
93
            self::$aPatterns,
94
            self::$aReplacements,
95
            \implode('/', $aMatches).'/'
96
        );
97
        // first seek normal filename
98
        $sFilePath = $sClassDirName.$sClassFileName;
99 8 Manuela
        if (\is_readable($sFilePath)) {
100 7 Manuela
            include $sFilePath;
101
        } else {
102 9 Manuela
            // second seek filename with prefix 'class.'
103
            $sFilePath = $sClassDirName.'class.'.$sClassFileName;
104 8 Manuela
            if (\is_readable($sFilePath)) {
105 7 Manuela
                include $sFilePath;
106
            }
107
        }
108
    }
109
/**
110 8 Manuela
 * register this autoloader
111 7 Manuela
 */
112 23 Manuela
    public static function doRegister($sPathPrefix)
113 7 Manuela
    {
114 23 Manuela
        self::$sInstallPath = \rtrim(\str_replace('\\', '/', $sPathPrefix), '/').'/';
115
        if (\is_dir(self::$sInstallPath) && \is_readable(self::$sInstallPath)) {
116
            \krsort(self::$aTransNs);
117
            foreach (self::$aTransNs as $sPattern => $sReplacement) {
118
                self::$aPatterns[]     = '@^('.$sPattern.'/)@su';
119
                self::$aReplacements[] = $sReplacement.'/';
120 10 Manuela
            }
121 23 Manuela
            \spl_autoload_register([__CLASS__, 'autoLoad']);
122
        } else {
123
            throw new \RuntimeException('invalid PathPrefix given!!');
124 10 Manuela
        }
125 7 Manuela
    }
126
127
/**
128 8 Manuela
 * unregister this autoloader
129 7 Manuela
 */
130
    public static function unRegister()
131
    {
132 9 Manuela
        \spl_autoload_unregister([__CLASS__, 'autoLoad']);
133 7 Manuela
    }
134
135
}