/** * test ->hasClass() * * @since 9-7-11 */ public function testHasClass() { $test_list = array(); $test_list[] = array('file' => '<' . '?php namespace Mingo; use Montage\\AutoLoad\\AutoLoadable; use MingoAutoload; class AutoLoader extends MingoAutoload implements AutoLoadable {} ', 'in' => '\\Mingo\\AutoLoader', 'out' => true); $test_list[] = array('file' => '<' . '?php namespace Mingo; use Montage\\AutoLoad\\AutoLoadable; use MingoAutoload; class AutoLoader extends MingoAutoload implements AutoLoadable {} ', 'in' => '\\blah\\AutoLoader', 'out' => false); $temp_file = tempnam(sys_get_temp_dir(), __CLASS__); foreach ($test_list as $i => $test_map) { file_put_contents($temp_file, $test_map['file'], LOCK_EX); $rfile = new ReflectionFile($temp_file); $this->assertEquals($test_map['out'], $rfile->hasClass($test_map['in']), $i); } //foreach }
/** * add a file and all the classes contained in that file * * @since 6-20-11 * @param string $file * @return count how many classes from the file were added */ protected function setFile($file) { // canary... if ($this->hasPath(new Path($file))) { return 0; } //if $ret_count = 0; $rfile = new ReflectionFile($file); $class_list = $rfile->getClasses(); foreach ($class_list as $class_map) { if ($this->setClass($class_map['class'], $file, $class_map)) { $ret_count++; } //if } //foreach return $ret_count; }
/** * check $class_name in all the $path_list * * @since 9-20-11 * @param string $class_name the original looked for class name * @param string $file_name the normalized class file name * @param array $path_list a list of paths * @return boolean */ protected function scanReq($class_name, $file_name, array $path_list) { $ret_bool = false; $class_bits = explode('\\', $class_name); $short_name = end($class_bits); // eg, for \foo\bar\baz return just baz // search for ClassName*.php, so things like ClassName.class.php are also found... $regex = sprintf('#%s\\S*?\\.(?:php\\d*|inc)#i', $short_name); foreach ($path_list as $path) { if (!$path instanceof Path) { $path = new Path($path); } //if if ($path->isDir()) { $iterator = $path->createIterator($regex); foreach ($iterator as $file) { $rfile = new ReflectionFile($file->getPathname()); if ($rfile->hasClass($class_name)) { if ($ret_bool = $this->req($class_name, $file_name, $file->getPathname())) { break 2; } //if } //if } //foreach } //if } //foreach return $ret_bool; }