Issues

ZF2-532: ClassMapAutoloader::realPharPath() rejects valid phar scheme

Description

While testing phar based modules I ran into the following: - System: Apache/2.4.2 (Win32) PHP/5.4.5

Errors: Warning: include(): Filename cannot be empty in C:\SOME_PATH\vendor\ZF2\library\Zend\Loader\ClassMapAutoloader.php on line 185

Warning: include(): Failed opening '' for inclusion (include_path='phar://C:\SOME_PATH\module\Album.phar;.;C:\php\pear') in C:\SOME_PATH\vendor\ZF2\library\Zend\Loader\ClassMapAutoloader.php on line 185

Fatal error: Uncaught exception 'Zend\Loader\Exception\InvalidArgumentException' with message 'Map file provided does not return a map.

The path injected into ClassMapAutoloader::realPharPath() was: -

phar://C/SOME_PATH/module/…

This the function returns void as phar:/// does not exist at strpos position 0.

With the strpos adjusted as a test the substr length is then incorrect.

The following seems to work as intended: -


    public static function realPharPath($path)
    {
        // If phar:// does not exist in the string at position 0 then return
        if (strpos($path, 'phar://') !== 0) {
            return;
        }
        // We need the correct substr length for phar:// | phar:///
        $substr_length = (strpos($path, 'phar:///') === false) ? 7 : 8;
        $parts = explode('/', str_replace(array('/','\\'), '/', substr($path, $substr_length)));
        $parts = array_values(array_filter($parts, function($p) { return ($p !== '' && $p !== '.'); }));

        array_walk($parts, function ($value, $key) use(&$parts) {
            if ($value === '..') {
                unset($parts[$key], $parts[$key-1]);
                $parts = array_values($parts);
            }
        });
        // Adjust phar scheme for full path or alias
        $phar_scheme = ($substr_length === 8) ? 'phar:///' : 'phar://';
        if (file_exists($realPath = $phar_scheme . implode('/', $parts))) {
            return $realPath;
        }
    }

Comments

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2566