1: <?php
2:
3: class ClassSearcher {
4:
5: 6: 7: 8: 9:
10: public static function findClasses($path) {
11: $classes = array();
12: foreach (CakePlugin::loaded() as $plugin) {
13: $classes = array_merge(
14: $classes,
15: self::_findClassesOnPlugin($plugin,
16: CakePlugin::path($plugin), $path)
17: );
18: }
19: return array_merge(
20: $classes, self::_findClassesOnPlugin(false, APP, $path)
21: );
22: }
23:
24: 25: 26: 27: 28:
29: public static function findInstances($path) {
30: $instances = array();
31: foreach(self::findClasses($path) as $className => $path) {
32: $instances[] = self::_createInstance($className, $path);
33: }
34: return $instances;
35: }
36:
37: 38: 39: 40: 41: 42: 43:
44: public static function findInstance($path, $className) {
45: $classes = self::findClasses($path);
46: if (empty($classes[$className])) {
47: throw new Exception("Class \"$className\" not found");
48: } else {
49: return self::_createInstance($className, $classes[$className]);
50: }
51: }
52:
53: 54: 55: 56: 57: 58: 59:
60: public static function findInstanceAndInstantiate($path, $className) {
61: return self::findInstance($path, $className);
62: }
63:
64: 65: 66: 67: 68: 69:
70: private static function _createInstance($className, $path) {
71: App::uses($className, $path);
72: return new $className();
73: }
74:
75: 76: 77: 78: 79: 80: 81:
82: public static function _findClassesOnPlugin($pluginName, $pluginRoot, $path) {
83: $fileSystemPath = $pluginRoot . implode(DS, explode('/', $path));
84: if (!is_dir($fileSystemPath)) {
85: return array();
86: }
87: $dir = new DirectoryIterator($fileSystemPath);
88: $classes = array();
89: while ($dir->valid()) {
90: if ($dir->isFile() && $dir->getExtension() == 'php') {
91: $classes[$dir->getBasename('.' . $dir->getExtension())] = ($pluginName ? $pluginName . '.' : '') . $path;
92: }
93: $dir->next();
94: }
95: return $classes;
96: }
97:
98: }
99: