/**
  * Get the Directory instance from a FileSystemObject instance or the path a string from a directory.
  * If the filesystem object is an existing file or symbolic link, $default will be returned.
  * The directory or directory path has to be valid.
  *
  * @param FilesystemObject|string $dir Filesystem object instance or the path of a directory as a string.
  * @param mixed|null $default [optional] Default value returned if the directory couldn't be instantiated,
  * possibly because the $dir param was invalid.
  *
  * @return Directory|mixed The directory as a Directory instance.
  * Or the $default param value if the directory couldn't be cast to a Directory instance.
  */
 public static function asDirectory($dir, $default = null)
 {
     // Create a new directory instance when $dir is a string, or when $dir is a FileSystemObject instance
     // but not a Directory
     if (is_string($dir) || $dir instanceof FilesystemObject && !$dir instanceof Directory) {
         $dir = new Directory($dir);
     }
     // The $dir must be a Directory instance, if not, return the default
     if (!$dir instanceof Directory) {
         return $default;
     }
     // Make sure the directory is valid, if not, return the $default value
     if (!$dir->isValid()) {
         return $default;
     }
     // Return the directory
     return $dir;
 }
 /**
  * Set the handled directory. Automatically reopens the directory handle if the handle was opened with a different
  * directory. The directory must exist before the handle can be opened.
  *
  * @param Directory|FilesystemObject|string $dir Handled directory, a directory or filesystem object
  * or the path of a directory as a string.
  *
  * @param resource $context [optional] See PHPs opendir() function for more details.
  * This directory context will be used if the handle needs to be reopened.
  * If set to null, the current directory context will be used.
  *
  * @return bool True if succeed, false on failure. Also returns true if the directory wasn't changed.
  *
  * @see opendir();
  */
 public function setDirectory($dir, $context = null)
 {
     // Get $dir as Directory instance, return false on failure
     if (($dir = Directory::asDirectory($dir)) === null) {
         return false;
     }
     // Make sure the directory changed
     if ($this->dir === $dir) {
         return true;
     }
     // Set the directory instance, return the result
     $this->dir = $dir;
     // Reopen the directory handler if it's opened already, return the result
     if ($this->isOpened()) {
         return $this->reopen($context);
     }
     return true;
 }
 public static function delete($path, $recursive = false, $context = null)
 {
     // Convert the oldPath into a string, return the $default value if failed
     if (($path = self::asPath($path, false)) === null) {
         return -1;
     }
     // Count the deleted filesystem objects
     $count = 0;
     // Delete directory contents
     if (self::isDirectory($path)) {
         // Get the current filesystem object as directory instance
         $dir = new Directory($path);
         // If we need to delete the directory recursively, we need to delete the directory contents first
         if ($recursive) {
             $count += $dir->deleteContents($context);
         }
         // Delete the directory itself, and return the number of deleted filesystem objects
         // TODO: Should this method be error muted?
         $count += @rmdir($path, $context);
     }
     // Delete the filesystem object
     // TODO: Should this method be error muted?
     if (@unlink($path, $context)) {
         $count++;
     }
     // Return the number of delete filesystem objects
     return $count;
 }