No Description

Class_.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 $type = 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 $interface Name of interface to implement
  39. * @param Name|string $... More interfaces to implement
  40. *
  41. * @return $this The builder instance (for fluid interface)
  42. */
  43. public function implement() {
  44. foreach (func_get_args() as $interface) {
  45. $this->implements[] = $this->normalizeName($interface);
  46. }
  47. return $this;
  48. }
  49. /**
  50. * Makes the class abstract.
  51. *
  52. * @return $this The builder instance (for fluid interface)
  53. */
  54. public function makeAbstract() {
  55. $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
  56. return $this;
  57. }
  58. /**
  59. * Makes the class final.
  60. *
  61. * @return $this The builder instance (for fluid interface)
  62. */
  63. public function makeFinal() {
  64. $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
  65. return $this;
  66. }
  67. /**
  68. * Adds a statement.
  69. *
  70. * @param Stmt|PhpParser\Builder $stmt The statement to add
  71. *
  72. * @return $this The builder instance (for fluid interface)
  73. */
  74. public function addStmt($stmt) {
  75. $stmt = $this->normalizeNode($stmt);
  76. $targets = array(
  77. 'Stmt_TraitUse' => &$this->uses,
  78. 'Stmt_ClassConst' => &$this->constants,
  79. 'Stmt_Property' => &$this->properties,
  80. 'Stmt_ClassMethod' => &$this->methods,
  81. );
  82. $type = $stmt->getType();
  83. if (!isset($targets[$type])) {
  84. throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
  85. }
  86. $targets[$type][] = $stmt;
  87. return $this;
  88. }
  89. /**
  90. * Returns the built class node.
  91. *
  92. * @return Stmt\Class_ The built class node
  93. */
  94. public function getNode() {
  95. return new Stmt\Class_($this->name, array(
  96. 'type' => $this->type,
  97. 'extends' => $this->extends,
  98. 'implements' => $this->implements,
  99. 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
  100. ), $this->attributes);
  101. }
  102. }