HosipLan

Untitled

Jan 20th, 2012
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. <?php
  2.  
  3. umask(0);
  4. $file = tempnam(sys_get_temp_dir(), 'PHP-Test');
  5. chmod($file, 0777);
  6.  
  7. $class = <<<FILE
  8. <?php
  9.  
  10. class MyClass
  11. {
  12.    
  13.     public function testMyAss()
  14.     {
  15.         echo "some content";
  16.         echo "some other content";
  17.     }
  18.  
  19. }
  20.  
  21. FILE;
  22.  
  23. echo highlight_string($class, TRUE), "<hr>\n";
  24.  
  25. if (file_put_contents($file, $class) === FALSE) {
  26.     die('shit maaan :(');
  27. }
  28.  
  29. require_once $file; // create class
  30.  
  31. function appendToMethod(ReflectionMethod $method, $content)
  32. {
  33.     $file = fopen($method->getFilename(), 'rb+');
  34.     echo seekLine($file, $method->getEndLine());
  35.     fwrite($file, $content);
  36.     fclose($file);
  37. }
  38.  
  39. /**
  40.  * @return string|FALSE
  41.  */
  42. function seekLine($resource, $lineNumber)
  43. {
  44.     rewind($resource);
  45.     for ($i = 1; !feof($resource) ;$i++) {
  46.         $line = fgets($resource);
  47.         if ($i === $lineNumber) {
  48.             fseek($resource, -strlen($line), SEEK_CUR);
  49.             return $line;
  50.         }
  51.     }
  52.     return FALSE;
  53. }
  54.  
  55.  
  56. appendToMethod(new ReflectionMethod('MyClass', 'testMyAss'), "echo \"my ass\";");
  57. echo "<hr>", highlight_file($file, TRUE);
Advertisement
Add Comment
Please, Sign In to add comment