0

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Jezdimir Lončar
  • 433
  • 4
  • 14

2 Answers2

2

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();
Community
  • 1
  • 1
adlawson
  • 6,303
  • 1
  • 35
  • 46
  • I'm going to choose different approach, that's much better - everyone (every developer) should include files that they need. There's exception with main library, but for that I'm going to go with autoloader. Thanks for your answer. – Jezdimir Lončar Aug 22 '11 at 11:19
0

If those libs are PSR-0 compilant, you can use

Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

That will make Zend load any unknown classes (looking for Some_Class in Some/Class.php).

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82