01-09-2011, 05:10 PM
Just thought I would share some utility methods with getting information on specified classes. It scans a specified directory for php classes, stores them in an array, and is at your disposal. The main point of this is my emulation of PHP's export() function (mine is the exportClass() function).
Use as,
ScanClasses.php
An example run (however on a page it is properly formatted),
Use as,
PHP Code:
require('ScanClasses.php');
$classes = ScanClasses::getSingleton('Path to PHP files containing classes');
echo '<pre>';
$classes->exportClass('Test');
echo '</pre>';
ScanClasses.php
PHP Code:
<?php
class ScanClasses {
private static $class;
private $instance;
private function __construct($path) {
if (!is_dir($path)) {
die("Given path is not a valid directory!");
}
$dir = opendir($path);
while (false !== ($file = readdir($dir))) {
if (stripos($file, '.php') === false)
continue;
require($path . $file);
self::$class[] = new ReflectionClass(str_replace(".php", "", $file));
}
}
public static function getSingleton($path) {
if ($instance != null)
return $instance;
return $instance = new ScanClasses($path);
}
// Testing purposes - PHP's export()
public static function ex() {
echo ReflectionClass::export(self::$class[0]);
}
public function getClasses() {
$classes = array();
for ($i = 0; $i < count(self::$class); $i++)
array_push($classes, self::$class[$i]->newInstanceArgs());
return $classes;
}
public function getClass($class_name) {
for ($i = 0; $i < count(self::$class); $i++)
if (self::$class[$i]->getName() == $class_name)
return self::$class[$i]->newInstanceArgs();
return null;
}
public function exportClass($clazz) {
// $parsed_class = array(); <- TODO
$class = new ReflectionClass($clazz);
$export = "Class [ " . ($class->isInterface() ? 'interface' : ($class->isAbstract() ? 'abstract ' : '') . 'class' ) . " " . $class->getName() . " ";
$parent = $class->getParentClass();
if ($parent != null) {
$export .= 'extends ' . $parent->getName() . ' ';
}
$interfaces = $class->getInterfaceNames();
$interface_count = count($interfaces);
if ($interface_count > 0) {
$export .= 'implements ';
for ($i = 0; $i < $interface_count; $i++)
$export .= $interfaces[$i] . ($i <= count($interface_count) ? '' : ', ');
}
$export .= " ] {" . "\n";
$static_fields = $class->getProperties(ReflectionMethod::IS_STATIC);
$static_field_count = count($static_fields);
$export .= "\t- Static properties [" . $static_field_count . "] {";
if ($static_field_count > 0) {
for ($i = 0; $i < $static_field_count; $i++) {
$field_property = $static_fields[$i];
$export .= "\n\t\tStatic property [ " . self::modifierIntToString($field_property->getModifiers()) . " $" . $field_property->getName() . " ]";
}
}
$export .= "\n\t}\n";
$static_methods = $class->getMethods(ReflectionMethod::IS_STATIC);
$static_method_count = count($static_methods);
$export .= "\t- Static methods [" . $static_method_count . "] {";
if ($static_method_count > 0) {
for ($i = 0; $i < $static_method_count; $i++) {
$method_property = $static_methods[$i];
$export .= "\n\t\tStatic method [ " . self::modifierIntToString($method_property->getModifiers()) . " " . $method_property->getName() . " ]";
}
}
$export .= "\n\t}\n";
$fields = $class->getProperties(ReflectionMethod::IS_ABSTRACT | ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PRIVATE);
$field_count = count($fields);
if ($field_count > 0) {
$valid_fields = 0;
$export_temp = '';
for ($i = 0; $i < $field_count; $i++) {
$field_property = $fields[$i];
if ($field_property->isStatic())
continue;
$export_temp .= "\n\t\tProperty [ " . self::modifierIntToString($field_property->getModifiers()) . " $" . $field_property->getName() . " ]";
$valid_fields++;
}
$export .= "\t- Properties [" . $valid_fields . "] {" . $export_temp;
} else
$export .= "\t- Properties [0] {";
$export .= "\n\t}\n";
$methods = $class->getMethods(ReflectionMethod::IS_ABSTRACT | ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PRIVATE);
$method_count = count($methods);
if ($method_count > 0) {
$export_temp = '';
$valid_methods = 0;
for ($i = 0; $i < $method_count; $i++) {
$method_property = $methods[$i];
if ($method_property->isStatic())
continue;
$export_temp .= "\n\t\tMethod [ " . self::modifierIntToString($method_property->getModifiers()) . " " . $method_property->getName() . " ]";
$valid_methods++;
}
$export .= "\t- Methods [" . $valid_methods . "] {" . $export_temp;
} else
$export .= "\t- Methods [0] {";
$export .= "\n\t}\n";
echo $export . "}";
}
private function modifierIntToString($modifier) {
$str = '';
switch($modifier) {
case 256:
case 257:
case 65792:
$str = 'public';
break;
case 512:
case 513:
case 66048:
$str = 'protected';
break;
case 1024:
case 1025:
case 66560:
$str = 'private';
break;
case 8448:
$str = 'constructor';
break;
case 65794:
$str = 'abstract';
break;
}
return $str;
}
}
An example run (however on a page it is properly formatted),
Quote:Class [ abstract class Test implements Test2 ] { - Static properties [0] { } - Static methods [1] { Static method [ private lol ] } - Properties [1] { Property [ protected $static ] } - Methods [4] { Method [ constructor __construct ] Method [ protected lol2 ] Method [ abstract lol3 ] Method [ public lol4 ] }}Happy holidays. ;)