Map-based autoloader across php and phar resources

⌈⌋ branch:  Canonic Autoloader


Check-in [89e99feed4]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge ->absolute_path() back into loader callback __invoke()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:89e99feed4e50b6cc4e425b6995b3262bf169257
User & Date: mario 2014-02-13 02:25:35
Context
2014-02-13
05:05
constants are lowercased again too, autoloader does not yet discern them (which makes little sense anyway unless rfc:function_autoloading ever gets implemented; it's all just a proof of concept with current php versions) check-in: fc05ec0d9b user: mario tags: trunk
02:25
Merge ->absolute_path() back into loader callback __invoke() check-in: 89e99feed4 user: mario tags: trunk
01:59
(no comment) check-in: 054e009bf3 user: mario tags: trunk
Changes

Changes to stub.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
35
36
37
38
39
40
41
42

43
44
45
46
47
48
49
..
94
95
96
97
98
99
100
101














102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
 * api: php
 * title: Canonic Autoloader / shared.phar
 * description: Map-based autoloader across .php and .phar resources.
 * version: 0.3.1-4
 * depends: php:phar, php >= 5.3
 * category: cli
 * license: Public Domain
 *
 *
 * Self-contained autoloader which provides .php and .phar class
 * loading based on a map. Loader and map are also contained in a
 * phar.
................................................................................
 *
 * The phar could also be split up into separate autoload, map and
 * update includes.
 *
 */


//

if (!class_exists("Canonic_Autoloader")) {
    class Canonic_Autoloader extends ArrayObject {


        // Support for rfc:function_autoloading identifier typing.
        public $types = array(1=>"class", 2=>"function", 4=>"constant");

................................................................................
            {
                $path = $this[$type][$name];
            }

            // And load according include script
            if (isset($path))
            {
#var_dump($name, $type, $path);














                return include_once($this->absolute_path($path, $this->basedir));
            }

            // Once more with feeling
            if ($this->auto_update) {
                return $this->auto_update() and $this($name, $type);
            }
        }

        
        
        /**
         * Turn relative paths and phar:// locations into absolute.
         *
         * @param  string  $path       Dirname/Phar path.
         * @param  int     $base       Base directory to prepend on relative paths.
         * @return string
         *
         */
        public function absolute_path($path, $base) {
            // it was absolute already
            if ($path[0] != "/") {
                // relative dir, if no phar:// prefix
                if (strncmp($path, "phar://", 7)) {
                    return "$base/$path";
                }
                // relative phar://
                elseif ($path[7] != "/") {
                    return "phar://$base/" . substr($path, 7);
                }
            }
            return $path;

                
                // t
                $path = strncmp($path, "phar://", 7)
                      ? "$base/$path"
                      : ($path[7] != "/" ? "phar://$base/" . substr($path, 7) : $path);

                // b
                strncmp($path, "phar://", 7) and ($path = "$base/$path")  // turn relative into absolute dir
                or
                ($path[7] != "/") and ($path = "phar://$base/" . substr($path, 7)); // prepend basedir to relative phar://file/
        }



        /**
         * Automatically update classmap ($this) at runtime, for development setups.
         *
         * @return boolean
         *
         */        





|
|







 







|
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|









<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
..
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126




































127
128
129
130
131
132
133
<?php
/**
 * api: php
 * title: Canonic Autoloader / shared.phar
 * description: Map-based autoloader across .php and .phar resources.
 * version: 0.3.1-5
 * depends: php:phar | php >= 5.3
 * category: cli
 * license: Public Domain
 *
 *
 * Self-contained autoloader which provides .php and .phar class
 * loading based on a map. Loader and map are also contained in a
 * phar.
................................................................................
 *
 * The phar could also be split up into separate autoload, map and
 * update includes.
 *
 */



// Declared just once for relocatable instantation
if (!class_exists("Canonic_Autoloader")) {
    class Canonic_Autoloader extends ArrayObject {


        // Support for rfc:function_autoloading identifier typing.
        public $types = array(1=>"class", 2=>"function", 4=>"constant");

................................................................................
            {
                $path = $this[$type][$name];
            }

            // And load according include script
            if (isset($path))
            {
                // Convert relative paths
                if ($path[0] != "/") {

                    // Relative directory, if no phar: prefix
                    if (strncmp($path, "phar://", 7)) {
                        $path = "$this->basedir/$path";
                    }

                    // If non-absolute phar:// url, inject basedir
                    elseif ($path[7] != "/") {
                        $path = "phar://$this->basedir/" . substr($path, 7);
                    }
                }

                // Include
                return include_once($path);
            }

            // Once more with feeling
            if ($this->auto_update) {
                return $this->auto_update() and $this($name, $type);
            }
        }

        





































        /**
         * Automatically update classmap ($this) at runtime, for development setups.
         *
         * @return boolean
         *
         */        

Changes to update.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
 * api: php
 * description: Classmap builder which reads through directories and phar collections.
 * title: Canonic Classmap
 * version: 0.7.7
 * catgory: library
 * priority: hide
 * type: library
 * classes: RecursivePharDirIterator, PhpAndPharDirIterator, ExtractPhpIdentifierDeclarations, Canonic_Classmap
 * license: Public Domain
 *
 *
 * Classmap generation






|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
 * api: php
 * description: Classmap builder which reads through directories and phar collections.
 * title: Canonic Classmap
 * version: 0.7.7
 * category: library
 * priority: hide
 * type: library
 * classes: RecursivePharDirIterator, PhpAndPharDirIterator, ExtractPhpIdentifierDeclarations, Canonic_Classmap
 * license: Public Domain
 *
 *
 * Classmap generation