Writing a custom DirectoryIteratorObservable
In the previous chapter, we used a couple of DirectoryIterators
to recursively get all files in a directory and all its subdirectories. When iterating files, we might want to filter not only by filenames, but also by file size or access restrictions. Ideally, we could have a custom Observable that just checks the files names to match a certain pattern and then emits SplFileInfo
objects so we can implement our filtering logic ourselves.
For this purpose, we'll write our own DirectoryIteratorObservable
that does all this and has some extra options on top of that. We can split the implementation into two smaller chunks:
// DirectoryIteratorObservable.php class DirectoryIteratorObservable extends Observable { private $iter; private $scheduler; private $selector; private $pattern; public function __construct($dir, $pattern = null, $selector = null, $recursive = true, $scheduler = null) { $this->scheduler = $scheduler...