Class_.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\Node\Name;
  5. use PhpParser\Node\Stmt;
  6. class Class_ extends Declaration
  7. {
  8. protected $name;
  9. protected $extends = null;
  10. protected $implements = array();
  11. protected $flags = 0;
  12. protected $uses = array();
  13. protected $constants = array();
  14. protected $properties = array();
  15. protected $methods = array();
  16. /**
  17. * Creates a class builder.
  18. *
  19. * @param string $name Name of the class
  20. */
  21. public function __construct($name) {
  22. $this->name = $name;
  23. }
  24. /**
  25. * Extends a class.
  26. *
  27. * @param Name|string $class Name of class to extend
  28. *
  29. * @return $this The builder instance (for fluid interface)
  30. */
  31. public function extend($class) {
  32. $this->extends = $this->normalizeName($class);
  33. return $this;
  34. }
  35. /**
  36. * Implements one or more interfaces.
  37. *
  38. * @param Name|string ...$interfaces Names of interfaces to implement
  39. *
  40. * @return $this The builder instance (for fluid interface)
  41. */
  42. public function implement() {
  43. foreach (func_get_args() as $interface) {
  44. $this->implements[] = $this->normalizeName($interface);
  45. }
  46. return $this;
  47. }
  48. /**
  49. * Makes the class abstract.
  50. *
  51. * @return $this The builder instance (for fluid interface)
  52. */
  53. public function makeAbstract() {
  54. $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
  55. return $this;
  56. }
  57. /**
  58. * Makes the class final.
  59. *
  60. * @return $this The builder instance (for fluid interface)
  61. */
  62. public function makeFinal() {
  63. $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
  64. return $this;
  65. }
  66. /**
  67. * Adds a statement.
  68. *
  69. * @param Stmt|PhpParser\Builder $stmt The statement to add
  70. *
  71. * @return $this The builder instance (for fluid interface)
  72. */
  73. public function addStmt($stmt) {
  74. $stmt = $this->normalizeNode($stmt);
  75. $targets = array(
  76. 'Stmt_TraitUse' => &$this->uses,
  77. 'Stmt_ClassConst' => &$this->constants,
  78. 'Stmt_Property' => &$this->properties,
  79. 'Stmt_ClassMethod' => &$this->methods,
  80. );
  81. $type = $stmt->getType();
  82. if (!isset($targets[$type])) {
  83. throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
  84. }
  85. $targets[$type][] = $stmt;
  86. return $this;
  87. }
  88. /**
  89. * Returns the built class node.
  90. *
  91. * @return Stmt\Class_ The built class node
  92. */
  93. public function getNode() {
  94. return new Stmt\Class_($this->name, array(
  95. 'flags' => $this->flags,
  96. 'extends' => $this->extends,
  97. 'implements' => $this->implements,
  98. 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
  99. ), $this->attributes);
  100. }
  101. }