Todaymade\Daux\DauxHelper::getFile PHP Method

getFile() public static method

Locate a file in the tree. Returns the file if found or false
public static getFile ( Directory $tree, string $request ) : Todaymade\Daux\Tree\Content | Tree\Raw | false
$tree Todaymade\Daux\Tree\Directory
$request string
return Todaymade\Daux\Tree\Content | Tree\Raw | false
    public static function getFile($tree, $request)
    {
        $request = explode('/', $request);
        foreach ($request as $node) {
            // If the element we're in currently is not a
            // directory, we failed to find the requested file
            if (!$tree instanceof Directory) {
                return false;
            }
            // Some relative paths may start with ./
            if ($node == '.') {
                continue;
            }
            if ($node == '..') {
                $tree = $tree->getParent();
                continue;
            }
            $node = urldecode($node);
            // if the node exists in the current request tree,
            // change the $tree variable to reference the new
            // node and proceed to the next url part
            if (isset($tree->getEntries()[$node])) {
                $tree = $tree->getEntries()[$node];
                continue;
            }
            // if the node doesn't exist, we can try
            // two variants of the requested file:
            // with and w/o the .html extension
            foreach (static::getFilenames($tree->getConfig(), $node) as $filename) {
                if (isset($tree->getEntries()[$filename])) {
                    $tree = $tree->getEntries()[$filename];
                    continue 2;
                }
            }
            // At this stage, we're in a directory, but no
            // sub-item matches, so the current node must
            // be an index page or we failed
            if ($node !== 'index' && $node !== 'index.html') {
                return false;
            }
            return $tree->getIndexPage();
        }
        // If the entry we found is not a directory, we're done
        if (!$tree instanceof Directory) {
            return $tree;
        }
        if ($index = $tree->getIndexPage()) {
            return $index;
        }
        return false;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @param string $url
  * @return Entry
  * @throws LinkNotFoundException
  */
 protected function resolveInternalFile($url)
 {
     $triedAbsolute = false;
     // Legacy absolute paths could start with
     // "!" In this case we will try to find
     // the file starting at the root
     if ($url[0] == '!' || $url[0] == '/') {
         $url = ltrim($url, '!/');
         if ($file = DauxHelper::getFile($this->daux['tree'], $url)) {
             return $file;
         }
         $triedAbsolute = true;
     }
     // Seems it's not an absolute path or not found,
     // so we'll continue with the current folder
     if ($file = DauxHelper::getFile($this->daux->getCurrentPage()->getParent(), $url)) {
         return $file;
     }
     // If we didn't already try it, we'll
     // do a pass starting at the root
     if (!$triedAbsolute && ($file = DauxHelper::getFile($this->daux['tree'], $url))) {
         return $file;
     }
     throw new LinkNotFoundException("Could not locate file '{$url}'");
 }
All Usage Examples Of Todaymade\Daux\DauxHelper::getFile