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      1   <?php
     2      2   /**
     3      3    * api: php
     4      4    * title: Canonic Autoloader / shared.phar
     5      5    * description: Map-based autoloader across .php and .phar resources.
     6         - * version: 0.3.1-4
     7         - * depends: php:phar, php >= 5.3
            6  + * version: 0.3.1-5
            7  + * depends: php:phar | php >= 5.3
     8      8    * category: cli
     9      9    * license: Public Domain
    10     10    *
    11     11    *
    12     12    * Self-contained autoloader which provides .php and .phar class
    13     13    * loading based on a map. Loader and map are also contained in a
    14     14    * phar.
................................................................................
    35     35    *
    36     36    * The phar could also be split up into separate autoload, map and
    37     37    * update includes.
    38     38    *
    39     39    */
    40     40   
    41     41   
    42         -//
           42  +
           43  +// Declared just once for relocatable instantation
    43     44   if (!class_exists("Canonic_Autoloader")) {
    44     45       class Canonic_Autoloader extends ArrayObject {
    45     46   
    46     47   
    47     48           // Support for rfc:function_autoloading identifier typing.
    48     49           public $types = array(1=>"class", 2=>"function", 4=>"constant");
    49     50   
................................................................................
    94     95               {
    95     96                   $path = $this[$type][$name];
    96     97               }
    97     98   
    98     99               // And load according include script
    99    100               if (isset($path))
   100    101               {
   101         -#var_dump($name, $type, $path);
   102         -                return include_once($this->absolute_path($path, $this->basedir));
          102  +                // Convert relative paths
          103  +                if ($path[0] != "/") {
          104  +
          105  +                    // Relative directory, if no phar: prefix
          106  +                    if (strncmp($path, "phar://", 7)) {
          107  +                        $path = "$this->basedir/$path";
          108  +                    }
          109  +
          110  +                    // If non-absolute phar:// url, inject basedir
          111  +                    elseif ($path[7] != "/") {
          112  +                        $path = "phar://$this->basedir/" . substr($path, 7);
          113  +                    }
          114  +                }
          115  +
          116  +                // Include
          117  +                return include_once($path);
   103    118               }
   104    119   
   105    120               // Once more with feeling
   106    121               if ($this->auto_update) {
   107    122                   return $this->auto_update() and $this($name, $type);
   108    123               }
   109    124           }
   110    125   
   111    126           
   112         -        
   113         -        /**
   114         -         * Turn relative paths and phar:// locations into absolute.
   115         -         *
   116         -         * @param  string  $path       Dirname/Phar path.
   117         -         * @param  int     $base       Base directory to prepend on relative paths.
   118         -         * @return string
   119         -         *
   120         -         */
   121         -        public function absolute_path($path, $base) {
   122         -            // it was absolute already
   123         -            if ($path[0] != "/") {
   124         -                // relative dir, if no phar:// prefix
   125         -                if (strncmp($path, "phar://", 7)) {
   126         -                    return "$base/$path";
   127         -                }
   128         -                // relative phar://
   129         -                elseif ($path[7] != "/") {
   130         -                    return "phar://$base/" . substr($path, 7);
   131         -                }
   132         -            }
   133         -            return $path;
   134         -
   135         -                
   136         -                // t
   137         -                $path = strncmp($path, "phar://", 7)
   138         -                      ? "$base/$path"
   139         -                      : ($path[7] != "/" ? "phar://$base/" . substr($path, 7) : $path);
   140         -
   141         -                // b
   142         -                strncmp($path, "phar://", 7) and ($path = "$base/$path")  // turn relative into absolute dir
   143         -                or
   144         -                ($path[7] != "/") and ($path = "phar://$base/" . substr($path, 7)); // prepend basedir to relative phar://file/
   145         -        }
   146         -
   147         -
   148    127   
   149    128           /**
   150    129            * Automatically update classmap ($this) at runtime, for development setups.
   151    130            *
   152    131            * @return boolean
   153    132            *
   154    133            */        

Changes to update.php.

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