/**
  * Get the next entry from the directory handle.
  *
  * @param bool $ignorePeriodDirs [optional] True to ignore period directories such as /. and /.. .
  *
  * @return File|Directory|SymbolicLink|FilesystemObject|null The next filesystem object from the directory.
  */
 public function read($ignorePeriodDirs = true)
 {
     // Make sure a directory handle is opened
     if (!$this->isOpened()) {
         return null;
     }
     // Read the next entry from the directory handle, and make sure it's valid
     // TODO: Should we close the handle when null is returned?
     if (($entry = readdir($this->handle)) === false) {
         return null;
     }
     // Process single and double period entries
     if (($entry === '.' || $entry == '..') && $ignorePeriodDirs) {
         return $this->read($ignorePeriodDirs);
     }
     // Return period directories
     if ($entry === '.') {
         return $this->dir;
     } else {
         if ($entry === '..') {
             return $this->dir->getParent();
         }
     }
     // Create a proper filesystem object instance of the entry, return the result
     return FilesystemObject::from($this->dir, $entry);
 }
 /**
  * Delete the contents of the directory.
  *
  * @param resource $context [optional] See the unlink() function for documentation
  *
  * @return int Number of deleted files, symbolic links and directories, a negative number will be returned if the
  * directory doesn't exist or on failure.
  *
  * @see unlink()
  */
 public function deleteContents($context = null)
 {
     // The directory must exist
     if (!$this->isDirectory()) {
         return -1;
     }
     // Count the deleted files, symbolic links and directories
     $count = 0;
     // TODO: Use list methods from the File class if possible!
     // Get the directory path
     $dirPath = $this->getPath();
     if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
         $dirPath .= '/';
     }
     // List and delete the files and sub directories
     // TODO: List all files using the list() method from the Directory class, instead of glob!
     $files = glob($dirPath . '*', GLOB_MARK);
     foreach ($files as $filePath) {
         $file = new FilesystemObject($filePath);
         $count += $file->delete($context, true);
     }
     // Return the number of removed files and directories
     return $count;
 }
 /**
  * Get the target of the symbolic link.
  *
  * @param SymbolicLink|string $link Symbolic link instance or the symbolic link path as a string.
  * @param mixed|null $default [optional] The default value returned on failure.
  *
  * @return Directory|\carbon\core\io\filesystem\file\File|SymbolicLink|FilesystemObject|null
  * The target will be returned as Directory, File or SymbolicLink instance if the target exist.
  * If the target doesn't exist it will be returned as FileSystemObject instance instead.
  * The $default value will be returned on failure.
  */
 public static function getTarget($link, $default = null)
 {
     // Convert the link into a path string, return the $default value if failed
     if (($link = self::asPath($link, false)) === null) {
         return $default;
     }
     // Read the link, and make sure it's valid
     if (($path = readlink($link)) === false) {
         return $default;
     }
     // Return the result
     return FilesystemObject::from($path);
 }
 /**
  * Automatically generate a .htaccess file in the cache folder, due to security reasons.
  *
  * @param $cacheDir String Cache directory to generate the .htaccess file in.
  */
 private function createCacheHtaccessFile($cacheDir)
 {
     // Get the file path to the .htaccess file
     $filePath = new FilesystemObject($cacheDir, '.htaccess');
     // Generate the file contents
     $fileContents = "# This set_file has automatically been generated by Carbon CMS." . PHP_EOL;
     $fileContents .= "#" . PHP_EOL;
     $fileContents .= "# This .htaccess file is used to protect the cache folder from any security issues such as hackers." . PHP_EOL;
     $fileContents .= "# Do not remove this file!" . PHP_EOL;
     $fileContents .= PHP_EOL;
     $fileContents .= "Deny From All";
     // Write the .htaccess file
     $fileHandler = fopen($filePath->getCanonicalPath(), 'w') or die('Carbon CMS: Error while generating .htaccess file in the cache directory!');
     fwrite($fileHandler, $fileContents);
     fclose($fileHandler);
 }