phpzend-frameworkzend-autoloader

Zend Autoloader - Load files from directory


I was wondering if there's any way to use Zend Autoloader to load all files from specific directory and subdirectories?
I'm trying to include other libraries beside Zend such as JSTree.


Solution

  • Update

    Just found SPL's RecursiveDirectoryIterator. That may be a better option.


    There isn't anything Zend Framework specific, but you could take a look at PHP SPL's DirectoryIterator.

    You could use it like this: (untested)

    class My_DirectoryIterator extends DirectoryIterator
    {
        /**
         * Load every file in the directory and it's sub directories
         * It might be a good idea to put a limit on subdirectory iteration so you don't disappear down a black hole...
         * @return void
         */
        public function loadRecursive()
        {
            foreach ($this as $file) {
                if ($file->isDir() || $file->isLink()) {
                    $iterator = new self($file->getPathName());
                    $iterator->loadRecursive();
                } elseif ($file->isFile()) {
                    // Might want to check for .php extension or something first
                    require_once $file->getPathName();
                }
            }
        }
    }
    
    // Load them all
    $iterator = new My_DirectoryIterator('/path/to/parent/directory');
    $iterator->loadRecursive();