In reality, building a method that recursively traverses the file system and includes a specified file doesn’t differ too much from defining other common recursive search methods that you’ve probably coded many times before. This concept will be better understood if you look at the “load()” method below, which performs the aforementioned recursive search in a straightforward manner:
// load recursively specified file public function load($file, $path) { if (file_exists($file)) { require_once($file); return; } else { if (is_dir($path)) { if (FALSE !== ($handle = opendir($path))) { // search recursively the specified file while (FAlSE !== ($dir = readdir($handle))) { if (strpos($dir, '.') === FALSE) { $path .= '/' . $dir; $file = $path . '/' . $this->file; $this->load($file, $path); } } } closedir($handle); } } }
As usual, coding recursive methods and functions is a pretty tricky process, and the “load()” method isn’t an exception. However, its flow of execution is quite simple to follow. If the targeted file is found, then it’s included directly via the “require_once()” function. Otherwise, a recursive search is performed through the web server’s file system, starting from the specified path. Having explained how the “load()” method functions, it’s time to show the full source code of the “Loader()” class, this time including the signature of the method in question. Here it is:
<?php // define a recursive loader class class Loader { private $file = ''; private $path = ''; // constructor (not implemented) public function __construct(){}
// set file to load public function set_file($file) { $this->file = $file; }
// get file to load public function get_file() { return $this->file; }
// set path to load file public function set_path($path) { $this->path = $path; }
// get path to load file public function get_path() { return $this->path; } // load recursively specified file public function load($file, $path) { if (file_exists($file)) { require_once($file); return; } else { if (is_dir($path)) { if (FALSE !== ($handle = opendir($path))) { // search recursively the specified file while (FAlSE !== ($dir = readdir($handle))) { if (strpos($dir, '.') === FALSE) { $path .= '/' . $dir; $file = $path . '/' . $this->file; $this->load($file, $path); } } } closedir($handle); } } } }
So far, so good, right? At this stage, you've learned how to build a loader class that’s capable of recursively searching a determined file based on a supplied path and including it if it’s found. Logically, the last thing that remains undone is developing a example application that shows how to use the class in a concrete case. But this will be done in the next article of the series. Meanwhile, feel free to tweak the “Loader” class’ source code, and try to introduce into it your own enhancements. Final thoughts That’s all for now. In this third installment of the series, I discussed the progressive development a file loading class, which had the ability to include a specified file through a recursive search. Naturally, the class can be improved, particularly when it comes to refactoring its “load()” core method, but this topic will be covered in more detail in upcoming articles of the series. In the forthcoming tutorial, I’m going to set up some illustrative examples, so you’ll be able to see how the previous “Loader” class does its business. Don’t miss the next part!
blog comments powered by Disqus |
|
|
|
|
|
|
|