Project

General

Profile

« Previous | Next » 

Revision 23

Added by Manuela over 6 years ago

CoreAutoloader::addNamespace() + ::doRegister() modified.
initialize:: register Autoloader fitted to modified methods

View differences:

CoreAutoloader.php
17 17
 * You should have received a copy of the GNU General Public License 2
18 18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 19
 */
20
/**
20
/*
21 21
 * Description of Autoloader
22 22
 *
23 23
 * @package      Core
24 24
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
25 25
 * @author       Manuela v.d.Decken <manuela@isteam.de>
26
 * @license      GNU General Public License 3.0
26
 * @license      GNU General Public License 2.0
27 27
 * @version      0.0.1
28 28
 * @revision     $Id$
29 29
 * @since        File available since 05.07.2017
......
43 43
class CoreAutoloader
44 44
{
45 45

  
46
    private static $sInstallPath = '';
46
    private static $sInstallPath = null;
47 47
    private static $aPatterns = [];
48 48
    private static $aReplacements = [];
49 49

  
50
    private static $aTransNs = [
51
        'vendor/jscalendar' => 'include/jscalendar',
52
        'vendor'            => 'include',
53
        'bin/interfaces'    => 'framework',
54
        'bin/db'            => 'framework/db',
55
        'bin'               => 'framework',
56
        'addon'             => 'modules',
57
        'acp'               => 'admin',
58
    ];
50
    private static $aTransNs = [];
59 51
/**
60 52
 * add new Namespace->Directory relation or overwrite existing
61
 * @param string $sNamespace
62
 * @param string $sDirectory
53
 * @param mixed $sNamespace
54
 * @param string $sDirectory (default: '')
63 55
 */
64
    public static function addNamespace($sNamespace, $sDirectory)
56
    public static function addNamespace($mNamespace, $sDirectory = '')
65 57
    {
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.'/';
58
        if (\is_null(self::$sInstallPath)) {
59
            throw new \RuntimeException('can not add namespaces before autoloader is registered!!');
74 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
        }
75 77
    }
76 78

  
77 79
    public static function autoLoad($sClassName)
......
107 109
/**
108 110
 * register this autoloader
109 111
 */
110
    public static function doRegister(array $aAdditionalTranslations = null)
112
    public static function doRegister($sPathPrefix)
111 113
    {
112
        if (!is_null($aAdditionalTranslations)) {
113
            foreach ($aAdditionalTranslations as $sKey=>$sValue) {
114
                self::$aTransNs[$sKey] = $sValue;
114
        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.'/';
115 120
            }
121
            \spl_autoload_register([__CLASS__, 'autoLoad']);
122
        } else {
123
            throw new \RuntimeException('invalid PathPrefix given!!');
116 124
        }
117
        \krsort(self::$aTransNs);
118
        self::$sInstallPath = \rtrim(\str_replace('\\', '/', \dirname(__DIR__)), '/').'/';
119
        foreach (self::$aTransNs as $sPattern => $sReplacement) {
120
            self::$aPatterns[]     = '@^('.$sPattern.'/)@su';
121
            self::$aReplacements[] = $sReplacement.'/';
122
        }
123
        \spl_autoload_register([__CLASS__, 'autoLoad']);
124 125
    }
125 126

  
126 127
/**

Also available in: Unified diff