Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1.     /**
  2.      * Internal method used by compile(). Get PHP code from a closure of function as string.
  3.      *
  4.      * @param object $closure Closure object
  5.      *
  6.      * @return string
  7.      * @expect 'function($a) {return;}' when input function ($a) {return;}
  8.      * @expect 'function($a) {return;}' when input    function ($a) {return;}
  9.      * @expect '' when input 'Directory::close'
  10.      */
  11.     protected static function getPHPCode($closure) {
  12.         if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) {
  13.             $ref = new ReflectionMethod($matched[1], $matched[2]);
  14.         } else {
  15.             $ref = new ReflectionFunction($closure);
  16.         }
  17.         $fname = $ref->getFileName();
  18.  
  19.         // This never happened, only for Unit testing.
  20.         if (!is_file($fname)) {
  21.             return '';
  22.         }
  23.  
  24.         $lines = file_get_contents($fname);
  25.         $file = new SplFileObject($fname);
  26.         $file->seek($ref->getStartLine() - 2);
  27.         $spos = $file->ftell();
  28.         $file->seek($ref->getEndLine() - 1);
  29.         $epos = $file->ftell();
  30.  
  31.         return preg_replace('/^.*?function\s.*?\\((.+?)\\}[,\\s]*;?$/s', 'function($1}', substr($lines, $spos, $epos - $spos));
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement