/**
  * Read all next entries from the directory handle, starting after the last entry that was read.
  * The directory handler is opened automatically as it's required. The directory handle will be rewind automatically
  * after calling this method.
  *
  * @param bool $recursive True to scan the directory recursively.
  * @param int $types [optional] The type of filesystem objects to read. Defaults to FLAG_TYPE_ALL which allows all
  * filesystem object types. Choose from:
  * - FLAG_TYPE_OBJECT
  * - FLAG_TYPE_FILE
  * - FLAG_TYPE_DIRECTORY
  * - FLAG_TYPE_SYMBOLIC_LINK
  * - FLAG_TYPE_ALL
  * Use null to default to FLAG_TYPE_ALL.
  *
  * @return Array|null An array of all directory entries, or null on failure.
  *
  * @see FilesystemObjectFlags
  */
 public function readAll($recursive = false, $types = self::FLAG_TYPE_ALL)
 {
     // Default to FLAG_TYPE_ALL if $types is set to null
     if ($types === null) {
         $types = self::FLAG_TYPE_ALL;
     }
     // Open the directory handler if it isn't opened yet
     $this->open();
     // Create an array to store all filesystem objects in
     $entries = array();
     // Read all entries form the directory handle
     while (($entry = $this->read($this->ignorePeriodDirs)) !== null) {
         // Check this filesystem object type should be included
         if ($entry instanceof File) {
             if (!($types & self::FLAG_TYPE_FILE)) {
                 continue;
             }
         } elseif ($entry instanceof Directory) {
             if (!($types & self::FLAG_TYPE_DIRECTORY)) {
                 continue;
             }
         } elseif ($entry instanceof Directory) {
             if (!($types & self::FLAG_TYPE_SYMBOLIC_LINK)) {
                 continue;
             }
         } elseif (!($types & self::FLAG_TYPE_OBJECT)) {
             continue;
         }
         // Add the entry to the list
         $entries[] = $entry;
         // Check whether the scan is recursive and check whether the current object is a directory
         if ($recursive && $entry->isDirectory()) {
             // Create a new scanner to scan the current object recursively
             $scanner = new self($entry);
             // Scan all directory objects, make sure the result is valid
             if (($items = $scanner->readAll($recursive, $types, $this->ignorePeriodDirs)) === null) {
                 return null;
             }
             // Add the objects to the list
             $entries = array_merge($entries, $items);
         }
     }
     // Rewind the directory handler
     $this->rewind();
     // Return the list of filesystem objects
     return $entries;
 }