1<?php
2
3/**
4 * Component_Manager is the parent class of all component managers
5 * A component manager manages all components of the same types, for
6 * example, all action handlers, or all ajax handlers.
7 * @author Junling Ma <junlingm@gmail.com>
8 */
9abstract class Doku_Component_Manager {
10    /**
11     * handles a new class that is loaded in
12     * @param string $class the name of the new class.
13     */
14    abstract protected function handle($class);
15
16    protected function load($dir, $name='') {
17        $old_classes = array_flip(get_declared_classes());
18        $this->load_dir($dir, $name);
19        $new_classes = get_declared_classes();
20        foreach ($new_classes as $class) {
21            // check if this is an abstract class, or bultin class
22            $ref_class = new ReflectionClass($class);
23            if ($ref_class->isAbstract()) continue;
24            if ($ref_class->isInternal()) continue;
25            if (!isset($old_classes[$class])) $this->handle($class);
26        }
27    }
28
29    private function load_dir($dir, $name) {
30        if (!is_dir($dir)) return;
31        // read the entrys of $dir one by one
32        $dh = dir($dir);
33        if ($name && strtolower(substr($name, -4)) != '.php') $name .= '.php';
34        $subdirs = array();
35        while (false !== ($entry = $dh->read())) {
36            // skip hidden files
37            if ($entry[0] == '.') continue;
38            $path = $dir . '/' . $entry;
39            if (is_dir($path)) {
40                array_push($subdirs, $path);
41                continue;
42            }
43
44            if (strtolower(substr($entry, -4)) != '.php') continue;
45
46            if (!$name || strtolower($entry) == strtolower($name))
47                include_once($dir . '/' . $entry);
48        }
49        $dh->close();
50
51        // load scripts in subdirs recursively
52        foreach ($subdirs as $subdir) $this->load_dir($subdir, $action);
53    }
54}
55