Search code examples
phplinuxsymfonyunixsymfony1

symfony symlinks and path resolution to src


If you have a setup on your server such as:

/path/sites/site1
  app/
  bin/
  web/
  src/
  vendor/ -> /path/vendor/Symfony_2.0.3/vendor (symbolic links)
/path/sites/site2
  app/
  bin/
  web/
  src/
  vendor/ -> /path/vendor/Symfony_2.0.3/vendor (symbolic links)
/path/vendor/Symfony_2.0.3/vendor

How does then symfony know where to find the src folder etc?

As __DIR__ etc in php will resolve the /path/sites/site2/vendor to the actual real path...


Solution

  • Through reflection!

    Symfony 1.x

    sfProjectConfiguration.class.php
    
    static public function guessRootDir()
    {
        $r = new ReflectionClass('ProjectConfiguration');
    
        return realpath(dirname($r->getFileName()).'/..');
    }
    

    Symfony 2

    Symfony\Component\HttpKernel\Kernel

    public function getRootDir()
    {
        if (null === $this->rootDir) {
            $r = new \ReflectionObject($this);
            $this->rootDir = str_replace('\\', '/', dirname($r->getFileName()));
        }
    
        return $this->rootDir;
    }