Bob\ConfigFile::findConfigFile PHP Method

findConfigFile() static public method

# False if the file was not found.
static public findConfigFile ( $filename, $cwd )
    static function findConfigFile($filename, $cwd)
    {
        if (!is_dir($cwd)) {
            throw new \InvalidArgumentException(sprintf('%s is not a directory', $cwd));
        }
        # Look for the definition name in the $cwd
        # until one is found.
        while (!($rp = realpath("{$cwd}/{$filename}"))) {
            # Go up the hierarchy
            $cwd .= '/..';
            # We are at the filesystem boundary if there's
            # nothing to go up to.
            if (realpath($cwd) === false) {
                break;
            }
        }
        return $rp;
    }

Usage Example

Example #1
0
 protected function loadConfig()
 {
     $rootConfigPath = false;
     foreach ((array) $this['config.file'] as $file) {
         $rootConfigPath = ConfigFile::findConfigFile($file, getcwd());
         if (false !== $rootConfigPath) {
             break;
         }
     }
     if (false === $rootConfigPath) {
         $this['log']->err(sprintf("Filesystem boundary reached, none of %s found.\n", json_encode((array) $this['config.file'])));
         return false;
     }
     $this->loadConfigFile($rootConfigPath);
     # Save the original working directory, the working directory
     # gets set to the project directory while running tasks.
     $this->originalDirectory = getcwd();
     # The project dir is the directory of the root config.
     $this->projectDirectory = dirname($rootConfigPath);
     # Search for additional configs in the config load paths
     $configLoadPath = array_filter($this['config.load_path'], 'is_dir');
     if ($configLoadPath) {
         $cwd = getcwd();
         chdir($this->projectDirectory);
         $finder = Finder::create()->files()->name("*.php")->in($configLoadPath);
         foreach ($finder as $file) {
             $this->loadConfigFile($file);
         }
         chdir($cwd);
     }
     return true;
 }
ConfigFile