Bontiv-Sourceer source code viewer
Root | Help
./nk-auto-install-googlecode/class/dir.php
<?php
require_once 'PathException.php';

abstract class
Dir
{
    protected
$location;

    public function
__construct()
    {
       
$this->location = '/';
    }
   
    protected function
cleanPath ($base)
    {
       
// Remove multi slashes on the path
       
while (substr_count($base, '//'))
        {
           
$base = str_replace('//', '/', $base);
        }
       
        if (
substr($base, -1) == '/')
        {
            return
substr($base, 0, -1);
        }
        return
$base;
    }

    protected function
getPath ($base = '.')
    {
       
$base = explode('/', $base);
        foreach (
$base as &$dir)
        {
            if (
$dir == '.')
            {
               
$dir = $this->location;
            }
        }
       
$base = implode('/', $base);
       
$base = $this->cleanPath($base);

        if (isset (
$base[0]) && $base[0] !== '/')
        {
           
$base = $this->location . '/' . $base;
        }
       
        return
$base;
    }
   
    public function
chdir ($dir)
    {
        if (isset (
$dir[0]) && $dir[0] != '/')
        {
           
$dir = './' . $dir;
        }

       
$this->location = $this->cleanPath ($this->getPath ($dir));
       
//DEBUG:echo 'New location : ' . $this->location . ' (from ' . $dir . ')<br />';
   
}
   
    public function
isDir ($location = '.')
    {
        return
substr($location, -1, 1) == '/';
    }
   
    public function
isFile ($location = '.')
    {
        return
substr($location, -1, 1) != '/';
    }
}
Presented with Bontiv-Sourceer