sebbu

PHP API informations (Reflection)

Sep 12th, 2016 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.78 KB | None | 0 0
  1. <?php
  2. function print_type(&$type) {
  3.     //var_dump($type, $type->getName());
  4.     if (!is_null($type)) {
  5.         if ($type instanceof ReflectionNamedType) {
  6.             echo ($type->allowsNull()?'?':'').$type->getName().' ';
  7.         }
  8.         else if ($type instanceof ReflectionUnionType) {
  9.             $str='';
  10.             foreach($type as $t) {
  11.                 $str.=($type->allowsNull()?'?':'').$type->getName().' | ';
  12.             }
  13.             $str=substr($str,0, -3);
  14.             echo $str;
  15.         }
  16.     }
  17. }
  18. function print_argument(&$prm)
  19. {
  20.     if($prm->isOptional()) echo '[';
  21.     //if($prm->allowsNull()) echo 'NULL '; // NOTE : it is now handled by print_type
  22.     //if($prm->canBePassedByValue()) echo 'BYVALUE '; // NOTE : it is the default, opposite is isPassedByReference
  23.     //$type = $prm->getClass(); // NOTE : it has now merged into getType
  24.     /*if(!is_null($type)) echo $c->name.' ';
  25.     else if(method_exists($prm, 'hasType') && $prm->hasType()) echo $prm->getType().' ';
  26.     else if($prm->isArray()) echo 'array ';// else echo 'string '; //*/ // NOTE : it is now merged into getType
  27.     $type=$prm->getType();
  28.     print_type($type);
  29.     if($prm->isPassedByReference()) echo '&';
  30.     echo '$'.$prm->name;//.' ';
  31.     if($prm->isOptional()) {
  32.         if($prm->isDefaultValueAvailable()) {
  33.             $value=$prm->getDefaultValue();
  34.             if(is_array($value)) {
  35.                 echo '=array(';
  36.                 foreach($value as $key2=>$value2) {
  37.                     //echo $key2.' => '.$value2.', ';
  38.                     echo $key2.' => '.var_export($value2, true).', ';
  39.                 }
  40.                 echo ')';
  41.             }
  42.             else {
  43.                 //if(!is_null($value)) echo '='.$value;
  44.                 if(!is_null($value)) echo '='.var_export($value, true);
  45.                 else echo '=null';
  46.             }
  47.         }
  48.         echo ']';
  49.     }
  50. }
  51.  
  52. function print_function(&$fct)
  53. {
  54.     if(method_exists($fct, 'hasReturnType') && $fct->hasReturnType()) echo $fct->getReturnType().' ';
  55.     echo $fct->name.'(';
  56.     //var_dump($fct->getNumberOfRequiredParameters(),$fct->getNumberOfParameters());
  57.     $prms=$fct->getParameters();
  58.     $prms_c=count($prms);
  59.     for($i=0;$i<$prms_c;++$i) {
  60.         $prm=&$prms[$i];
  61.  
  62.         print_argument($prm);
  63.  
  64.         if($i+1<$prms_c) echo ', ';
  65.     }
  66.     echo ')<br/>'."\r\n";
  67. }
  68.  
  69. function print_class_method(&$mth)
  70. {
  71.     if($mth->isAbstract()) echo 'abstract ';
  72.     //if($mth->isConstructor()) echo 'constr ';
  73.     //if($mth->isDestructor()) echo 'destr ';
  74.     if($mth->isFinal()) echo 'final ';
  75.     if($mth->isPrivate()) echo 'private ';
  76.     if($mth->isProtected()) echo 'protected ';
  77.     if($mth->isPublic()) echo 'public ';
  78.     if($mth->isStatic()) echo 'static ';
  79.     //var_dump($mth, $mth->getReturnType());
  80.     if(method_exists($mth, 'hasReturnType') && $mth->hasReturnType()) {
  81.         //echo $mth->getReturnType().' ';
  82.         $type=$mth->getReturnType();
  83.         print_type($type);
  84.     }
  85.     echo $mth->name.'(';
  86.     $prms=$mth->getParameters();
  87.     $prms_c=count($prms);
  88.     for($j=0;$j<$prms_c;++$j) {
  89.         $prm=&$prms[$j];
  90.  
  91.         print_argument($prm);
  92.  
  93.         if($j+1<$prms_c) echo ', ';
  94.     }
  95.     echo ')<br/>'."\r\n";
  96. }
  97.  
  98. function print_class_properties(&$prop)
  99. {
  100.     $prop->setAccessible(true);
  101.     if($prop->isPrivate()) echo 'private ';
  102.     if($prop->isProtected()) echo 'protected ';
  103.     if($prop->isPublic()) echo 'public ';
  104.     if($prop->isStatic()) echo 'static ';
  105.     echo $prop->name;
  106.     if($prop->isStatic()) echo ' = '.($prop->isDefault()?'<span style="text-color: green">':'<span style="text-color: red">').var_export($prop->getValue(), true).'</span>';
  107.     echo '<br/>'."\r\n";
  108. }
  109. ////////////////////////////
  110. if(!isset($action)) $action='list_ext';
  111. if(array_key_exists('action',$_GET)) $action=$_GET['action'];
  112. $class='';
  113. if(array_key_exists('class',$_GET)) $class=$_GET['class'];
  114. $ext='';
  115. if(array_key_exists('ext',$_GET)) $ext=$_GET['ext'];
  116.  
  117. function cmp_name($ar1, $ar2) {
  118.     return strnatcasecmp($ar1->name, $ar2->name);
  119. }
  120.  
  121. function cmp_getname($ar1, $ar2) {
  122.     return strnatcasecmp($ar1->getName(), $ar2->getName());
  123. }
  124.  
  125. $exts=get_loaded_extensions();
  126.  
  127. if($action=='')
  128. {
  129.     echo 'action must be defined : list_proj or list_ext by default.';
  130. }
  131. else if($action=='list_proj')
  132. {
  133.     if(!isset($classes) || !is_array($classes) || !isset($functions) || !is_array($functions))
  134.     {
  135.         echo 'you must list the $classes and the $functions.<br/>'."\r\n";
  136.         die();
  137.     }
  138.     echo '<h1>Classes</h1>'."\r\n";
  139.     echo "\r\n";
  140.    
  141.     natcasesort($classes);
  142.     foreach($classes as $class) {
  143.         echo '<a href="?action=desc_class&class='.$class.'">'.$class.'</a><br/>'."\r\n";
  144.     }
  145.  
  146.     echo '<h1>Functions</h1>'."\r\n";
  147.     foreach($functions as $function) {
  148.         $fct=new ReflectionFunction($function);
  149.         print_function($fct);
  150.     }
  151. }
  152. else if($action=='list_ext') {
  153.     echo '<h1>Extensions</h1>'."\r\n";
  154.     echo "\r\n";
  155.  
  156.     natcasesort($exts);
  157.     foreach($exts as $ext) {
  158.         echo '<a href="?action=desc_ext&ext='.$ext.'">'.$ext.'</a><br/>'."\r\n";
  159.     }
  160. }
  161. elseif($action=='desc_ext'&&$ext!='') {
  162.     $e=new ReflectionExtension($ext);
  163.     echo '<h1>'.$e->getName().'</h1>';
  164.     if($e->getVersion()!=NULL) echo 'Version = '.var_export($e->getVersion(), true);
  165.     echo "\r\n";
  166.     echo "\r\n";
  167.  
  168.     //$cls=$e->getClassNames();
  169.     $cls=$e->getClasses();
  170.     natcasesort($cls);
  171.     //usort($cls, 'cmp_getname');
  172.     //uasort($cls, 'cmp_getname');
  173.     echo '<h2>Classes</h2>'."\r\n";
  174.     foreach($cls as $cl) {
  175.         $cln=$cl->getName();
  176.         if($cl->isInterface()) echo 'interface ';
  177.         if($cl->isTrait()) echo 'trait ';
  178.         if($cl->isAbstract()) echo 'abstract ';
  179.         if(!$cl->isInstantiable()) echo 'non-instantiable ';
  180.         echo '<a href="?action=desc_class&class='.$cln.'">'.$cln.'</a><br/>'."\r\n";
  181.     }
  182.     echo "\r\n";
  183.    
  184.     $dps=$e->getDependencies();
  185.     echo '<h2>Dependancies</h2>'."\r\n";
  186.     echo '<pre>'.var_export($dps,true).'</pre>'."\r\n";
  187.    
  188.     $fcts=$e->getFunctions();
  189.     natcasesort($fcts);
  190.     //usort($fcts, 'cmp_name');
  191.     //uasort($fcts, 'cmp_name');
  192.     echo '<h2>Functions</h2>'."\r\n";
  193.     foreach($fcts as $fct) {
  194.         print_function($fct);
  195.     }
  196.     echo "\r\n";
  197.    
  198.     $cts=$e->getConstants();//NOTE : Don't sort (constants are grouped)
  199.     echo '<h2>Constants</h2>'."\r\n";
  200.     foreach($cts as $ct=>$vl) {
  201.         echo $ct.' = '.var_export($vl,true).'<br/>'."\r\n";
  202.     }
  203.     echo "\r\n";
  204.    
  205.     $inis=$e->getINIEntries();
  206.     echo '<h2>INI entries</h2>'."\r\n";
  207.     foreach($inis as $ini=>$vl) {
  208.         echo $ini.' = '.var_export($vl,true).'<br/>'."\r\n";
  209.     }
  210.     echo "\r\n";
  211. }
  212. elseif($action=='desc_class'&&$class!='') {
  213.     $cl=new ReflectionClass($class);
  214.     echo '<h2>'.$cl->getName().'</h2>'."\r\n";
  215.    
  216.     $ext=$cl->getExtension();
  217.     if($ext!==NULL) {
  218.         echo 'Defined in extension <a href="?action=desc_ext&ext='.$ext->getName().'">'.$ext->getName().'</a><br/>'."\r\n";
  219.     }
  220.    
  221.     $doc=$cl->getDocComment();
  222.     if($doc!==FALSE) echo substr_replace(array("\r\n"),array('<br/>'."\r\n"),$doc).'<br/>'."\r\n";
  223.    
  224.     echo '<h2>Traits</h2>'."\r\n";
  225.     $ts=$cl->getTraits();
  226.     foreach($ts as $t) {
  227.         //echo $t->getName().'<br/>'."\r\n";
  228.         echo '<a href="?action=desc_class&class='.$t->getName().'">'.$t->getName().'</a><br/>'."\r\n";
  229.     }
  230.    
  231.     echo '<h2>Trait Aliases</h2>'."\r\n";
  232.     $tas=$cl->getTraitAliases();
  233.     foreach($tas as $ta_new => $ta_old) {
  234.         //echo $ta_new->getName().' from '.$ta_old->getName().'<br/>'."\r\n";
  235.         echo $ta_new->getName().' from '.'<a href="?action=desc_class&class='.$ta_old->getName().'">'.$ta_old->getName().'</a><br/>'."\r\n";
  236.     }
  237.    
  238.     echo '<h2>Interfaces</h2>'."\r\n";
  239.     $is=$cl->getInterfaces();
  240.     foreach($is as $i) {
  241.         //echo $i->getName().'<br/>'."\r\n";
  242.         echo '<a href="?action=desc_class&class='.$i->getName().'">'.$i->getName().'</a><br/>'."\r\n";
  243.     }
  244.    
  245.     echo '<h2>Parent classes</h2>'."\r\n";
  246.     //$pc=$cl->getParentClass();
  247.     $pc=$cl;
  248.     while(($pc=$pc->getParentClass())!=NULL) {
  249.     //do {
  250.         echo '<a href="?action=desc_class&class='.$pc->getName().'">'.$pc->getName().'</a>, '."\r\n";
  251.     }
  252.     //while(($pc=$pc->getParentClass())!=NULL);
  253.    
  254.     $cts=$cl->getConstants();//NOTE : Don't sort (constants are grouped)
  255.     echo '<h2>Constants</h2>'."\r\n";
  256.     foreach($cts as $ct=>$vl) {
  257.         echo $ct.' = '.var_export($vl,true).'<br/>'."\r\n";
  258.     }
  259.     echo "\r\n";
  260.    
  261.     echo '<h3>Methods</h3>'."\r\n";
  262.     //$ct=$cl->getConstructor();//NOTE : useless
  263.     $mths=$cl->getMethods();
  264.     natcasesort($mths);
  265.     //usort($mths, 'cmp_name');
  266.     //uasort($mths, 'cmp_name');
  267.     $mths_c=count($mths);
  268.     $inherit=false;
  269.     for($i=0;$i<$mths_c;++$i) {
  270.         $mth=&$mths[$i];
  271.         if(!$inherit && $mth->getDeclaringClass()->getName()!=$cl->getName()) {
  272.             echo '<h3>Inherited Methods</h3>'."\r\n";
  273.             $inherit=true;
  274.         }
  275.         print_class_method($mth);
  276.     }
  277.    
  278.     echo '<h3>Properties</h3>'."\r\n";
  279.     $props=$cl->getProperties();
  280.     $props_c=count($props);
  281.     for($i=0;$i<$props_c;++$i) {
  282.         $prop=&$props[$i];
  283.         print_class_properties($prop);
  284.     }
  285.    
  286.     echo '<h3>Static Properties</h3>'."\r\n";
  287.     $sprops=$cl->getStaticProperties();
  288.     foreach($sprops as $sprop=>$sprop_v) {
  289.         //$sprop_i=new ReflectionProperty($cl->getName(), $sprop);
  290.         //print_class_properties($sprop_i);
  291.         var_dump($sprop);
  292.         //print_r($sprop_v);
  293.         var_export($sprop_v);
  294.     }
  295.    
  296.     echo '<h3>Default Properties</h3>'."\r\n";
  297.     $dps=$cl->getDefaultProperties();
  298.     echo '<pre>';print_r($dps);echo '</pre>'."\r\n";
  299. }
  300. else {
  301.     echo '[ERROR]';
  302. }
  303. ?>
Add Comment
Please, Sign In to add comment