Class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. class PHPParser_Builder_Class extends PHPParser_BuilderAbstract
  3. {
  4. protected $name;
  5. protected $extends;
  6. protected $implements;
  7. protected $type;
  8. protected $uses;
  9. protected $constants;
  10. protected $properties;
  11. protected $methods;
  12. /**
  13. * Creates a class builder.
  14. *
  15. * @param string $name Name of the class
  16. */
  17. public function __construct($name) {
  18. $this->name = $name;
  19. $this->type = 0;
  20. $this->extends = null;
  21. $this->implements = array();
  22. $this->uses = $this->constants = $this->properties = $this->methods = array();
  23. }
  24. /**
  25. * Extends a class.
  26. *
  27. * @param PHPParser_Node_Name|string $class Name of class to extend
  28. *
  29. * @return PHPParser_Builder_Class 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 PHPParser_Node_Name|string $interface Name of interface to implement
  39. * @param PHPParser_Node_Name|string $... More interfaces to implement
  40. *
  41. * @return PHPParser_Builder_Class 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 PHPParser_Builder_Class The builder instance (for fluid interface)
  53. */
  54. public function makeAbstract() {
  55. $this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_ABSTRACT);
  56. return $this;
  57. }
  58. /**
  59. * Makes the class final.
  60. *
  61. * @return PHPParser_Builder_Class The builder instance (for fluid interface)
  62. */
  63. public function makeFinal() {
  64. $this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_FINAL);
  65. return $this;
  66. }
  67. /**
  68. * Adds a statement.
  69. *
  70. * @param PHPParser_Node_Stmt|PHPParser_Builder $stmt The statement to add
  71. *
  72. * @return PHPParser_Builder_Class 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. * Adds multiple statements.
  91. *
  92. * @param array $stmts The statements to add
  93. *
  94. * @return PHPParser_Builder_Class The builder instance (for fluid interface)
  95. */
  96. public function addStmts(array $stmts) {
  97. foreach ($stmts as $stmt) {
  98. $this->addStmt($stmt);
  99. }
  100. return $this;
  101. }
  102. /**
  103. * Returns the built class node.
  104. *
  105. * @return PHPParser_Node_Stmt_Class The built class node
  106. */
  107. public function getNode() {
  108. return new PHPParser_Node_Stmt_Class($this->name, array(
  109. 'type' => $this->type,
  110. 'extends' => $this->extends,
  111. 'implements' => $this->implements,
  112. 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
  113. ));
  114. }
  115. }