Small lib for the fast managing file system
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Build Status

The DirCommander is a simple lib for fast manipulating of the file system.

I created it for code generation purposes, but it's good for others tasks.

How to use:

  1. First create an adapter. Adapter represents functions for working with the file system in PHP. Also adapter implements IDirCommanderAdapter interface.

    $adapter = new LocalDirCommanderAdapter();
  2. Create DirCommander instance, passing adapter as it's first argument and root path.

    $rootPath = __DIR__; // no "/" in the end
    $dc = new DirCommander($adapter, $rootPath);

Now we may use $dc for manipulating file system.

Create a new directory in current root


Remove a directory or file

$dc->remove('newDir'); // all sub directories and files will be deleted

WARING: Removing directories and files is recursive!

Get current directory

For example, if we pass /home/user to constructor:

echo $dc->getCurrentPath(); // prints /home/user

Change current directory

For example, we have "alex" folder in /home/user dir and current directory is:

echo $dc->getCurrentPath(); // prints /home/user

It will change current directory to /home/user/alex

echo $dc->getCurrentPath() - prints /home/user/alex

It means that all future operations we will do in alex directory.

Current directory ALWAYS saved in object.

If we do:


We will create directory "documents" in alex directory.

The $dc->makeDir() does not change the current directory. Only two methods may change current directory: $dc->cd() and $dc->up()

Method $dc->up() change current dir to upper. For example:

echo $dc->getCurrentPath(); // prints /home/user/alex
echo $dc->getCurrentPath(); // prints /home/user

Instead $dc->up() you may use $dc->cd('..')

Create file

$dc->makeFile('file.php'); // create an empty file in the current directory
$dc->makeFile('file.php', 'data'); // create file with text data

Get content of the file

$dc->getContent('file.php'); // return string content

Copy and Paste:

For example:

echo $dc->getCurrentPath() - prints /home/user/alex

Current directory is alex, we need create a backup copy of it.

 $dc->up(); // now we in /home/user
 $dc->paste('alexBackup'); // it create /home/user/alexBackup directory

Copy paste works recursive that mean your subdirectories and files was completely copy.

It works for files too:

$dc->past('copySomeFile.php', true); // second argument true means, that if file exist it we be overwritten

You also may not past firs argument. For example: dir alex contains file "someFile.php" we need copy it to user directories:

echo $dc->getCurrentPath(); // prints /home/user/alex
$dc->up(); // now we in /home/user
$dc->paste(); // it create copy of file "someFile.php" in /home/user dir

Chain of calls

Only two methods return a string: getContent and getCurrentPath. Others return self, you may use it for fast manipulate file system.

Something like that:

          ->cd('lib')->makeFile(ucfirst($this->controllerName).'Controller.php', $controllerClass)
        ->makeFile('router.php', $routerFile)

Error handling

All methods throws DirCommanderException use try catch.

In the End

  • More examples see in tests
  • WARNING! All operations not transactional!