Magento 2: Work Around for Loading Static Assets from Symlinks?

there are still some places in Magento 2, which are not compatible with components outside of Magento root. We're working on fixing them. Thanks for reporting, so we would not miss any such place (this one is already in our list).

Unfortunately, I can't suggest any workaround for it now.


Found an ugly, but temporary workaround for the static assets problem. For reasons that aren't 100% clear, for symlinked files Magento will pass an already absolute $path the getAbsolutePath method in the Magento\Framework\Filesystem\Driver\File class

#File: vendor/magento/framework/Filesystem/Driver/File.php
public function getAbsolutePath($basePath, $path, $scheme = null)
{
    return $this->getScheme($scheme) . $basePath . ltrim($this->fixSeparator($path), '/');
}

Since $path is already absolute, this method ends up generating an incorrect absolute path.

My fix is to temporarily hack in some code to detect this situation

public function getAbsolutePath($basePath, $path, $scheme = null)
{
    if(time() > strToTime('2016-01-25'))
    {
        throw new \Exception("Don't forget this hack: " . __FILE__);
    }
    if(strlen($path) > 0 && $path[0] === '/' && file_exists($path) )
    {
        return $path;
    }

    return $this->getScheme($scheme) . $basePath . ltrim($this->fixSeparator($path), '/');
}

The first if clause is something I've started doing with all my temporary hacks to ensure if they somehow end up on an important system, they have a time bomb explosion that will point to them clearly.

The second if clause detects if the method's been passed an already absolute path, and if so returns the correct path (assuming the file actually exists).

Why a hack instead of a plugin? Because it looks like the static asset serving application at pub/static.php does not allow you to plugin to classes.