1
0

InterfaceInstance.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Osinet\ClassGrapher;
  3. /**
  4. * Representation of an "interface" symbol in source code.
  5. */
  6. class InterfaceInstance {
  7. const ORIGIN_UNKNOWN = NULL;
  8. const ORIGIN_PHP = 'php';
  9. const ORIGIN_EXTENSION = 'extension';
  10. const ORIGIN_SCRIPT = 'script';
  11. /**
  12. * Was instance created just because it was referenced by another symbol ?
  13. *
  14. * @var int
  15. * 0 (No) or 1 (Yes)
  16. */
  17. public $implicit;
  18. /**
  19. * Origin of symbol. Use the self::ORIGIN_* constants.
  20. *
  21. * @var int
  22. */
  23. public $origin;
  24. /**
  25. * @var Logger
  26. */
  27. public $logger = NULL;
  28. public $name = '(none)';
  29. public $parent = NULL;
  30. /**
  31. * @param \PGPClass $symbol
  32. * The interface symbole from grammar_parser.
  33. * @param int $implicit
  34. * @param Logger $logger
  35. *
  36. * @throws \Exception
  37. */
  38. public function __construct(\PGPClass $symbol, $implicit = 0, Logger $logger = NULL) {
  39. $this->implicit = $implicit;
  40. $this->logger = $logger;
  41. $this->name = $symbol->name;
  42. $this->parent = reset($symbol->extends);
  43. if (empty($this->logger)) {
  44. throw new \Exception('No logger in constructor.\n');
  45. }
  46. $this->origin = $this->getSymbolOrigin($symbol);
  47. }
  48. /**
  49. * Shortcut for logger->message().
  50. *
  51. * @param string $message
  52. * @param int $level
  53. */
  54. public function debug($message, $level = LOG_INFO) {
  55. $this->logger->legacyLog($message, $level);
  56. }
  57. public function getSymbolOrigin() {
  58. $ret = self::ORIGIN_UNKNOWN;
  59. try {
  60. $r = new \ReflectionClass($this->name);
  61. $this->debug("$this->name is introspectable.\n");
  62. } catch (\ReflectionException $e) {
  63. $this->debug("{$this->name} is userland, unshared with parser.\n");
  64. $ret = self::ORIGIN_SCRIPT;
  65. }
  66. if (!isset($ret)) {
  67. $extension = $r->getExtensionName();
  68. if ($extension === FALSE) {
  69. $ret = self::ORIGIN_SCRIPT;
  70. }
  71. else {
  72. $this->implicit = 0;
  73. $ret = ($extension == 'Core')
  74. ? self::ORIGIN_PHP
  75. : self::ORIGIN_EXTENSION . ':' . $extension;
  76. }
  77. }
  78. return $ret;
  79. }
  80. public function basicAttributes() {
  81. $ret = array(
  82. 'shape' => 'box',
  83. 'style' => 'rounded',
  84. );
  85. return $ret;
  86. }
  87. public function attributes() {
  88. $ret = $this->basicAttributes();
  89. if ($this->implicit) {
  90. $ret['style'] = 'filled';
  91. $ret['fillcolor'] = '#e0e0e0';
  92. }
  93. if ($this->origin != self::ORIGIN_SCRIPT) {
  94. $ret['label'] = $this->name ."\n". $this->origin;
  95. }
  96. return $ret;
  97. }
  98. }