code_utilities.class.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Description of code_utilities
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. */
  8. class CodeUtilities
  9. {
  10. const CLASS_PATTERN = '/(?:\s+class\s+[a-zA-Z_0-9\x7f-\xff]+\s*{)|(?:\s+class\s+[a-zA-Z_0-9\x7f-\xff]+\s*extends)|(?:\s+class\s+[a-zA-Z_0-9\x7f-\xff]+\s*implements)|(?:\s+interface\s+[a-zA-Z_0-9\x7f-\xff]+\s*{)|(?:\s+interface\s+[a-zA-Z_0-9\x7f-\xff]+\s*extends)/mi';
  11. const INLINE_COMMENT_PATTERN = '#//.*$#m';
  12. const MULTILINE_COMMENT_PATTERN = '#/\*.*?\*/#ms';
  13. const NAMESPACE_PATTERN = '/namespace\s*(.*);/';
  14. const IDENTIFIER_PATTERN = '/^[a-zA-Z_][a-zA-Z0-9_]*$/';
  15. static function remove_comments($content)
  16. {
  17. $content = preg_replace(self::INLINE_COMMENT_PATTERN, '', $content);
  18. $content = preg_replace(self::MULTILINE_COMMENT_PATTERN, '', $content);
  19. return $content;
  20. }
  21. /**
  22. * Returns the name of classes and interfaces contained in content.
  23. *
  24. * @param text $content
  25. * @return array
  26. */
  27. static function get_classes($content)
  28. {
  29. $result = array();
  30. $cls_pattern = self::CLASS_PATTERN;
  31. $content = self::remove_comments($content); //comments may contains class declaration we don't want to capture.
  32. $matches = array();
  33. if (preg_match_all($cls_pattern, $content, $matches)) {
  34. $matches = reset($matches);
  35. foreach ($matches as $match) {
  36. $match = str_replace("\n", ' ', $match);
  37. $match = str_replace('{', ' ', $match);
  38. $words = explode(' ', $match);
  39. foreach ($words as $word) {
  40. $word = trim($word);
  41. //we capture the interface/class name with the current pattern
  42. if (strtolower($word) != 'class' && strtolower($word) != 'interface' && strtolower($word) != 'implements' && strtolower($word) != 'extends' && !empty($word)) {
  43. $result[] = $word;
  44. break; //we only take the first name as we don't want to capture the name of the interface or of the parent class name
  45. }
  46. }
  47. }
  48. }
  49. return $result;
  50. }
  51. static function get_namespace($content)
  52. {
  53. $namespace_pattern = self::NAMESPACE_PATTERN;
  54. if (preg_match($namespace_pattern, $content, $matches)) {
  55. $result = end($matches);
  56. return trim($result);
  57. } else {
  58. return false;
  59. }
  60. }
  61. static function is_valid_identifier($name)
  62. {
  63. $pattern = self::IDENTIFIER_PATTERN;
  64. $r = preg_match($pattern, $name);
  65. return $r;
  66. }
  67. /**
  68. * Make path relative to root.
  69. *
  70. * @param string $root
  71. * @param string $path
  72. * @return string
  73. */
  74. static function relative_path($root, $path)
  75. {
  76. $path = realpath($path);
  77. $root = realpath($root);
  78. $path = str_ireplace($root, '', $path);
  79. $path = str_ireplace('\\', '/', $path);
  80. return $path;
  81. }
  82. }