BuilderFactory.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace PhpParser;
  3. use PhpParser\Builder;
  4. use PhpParser\Node\Stmt\Use_;
  5. /**
  6. * The following methods use reserved keywords, so their implementation is defined with an underscore and made available
  7. * with the reserved name through __call() magic.
  8. *
  9. * @method Builder\Namespace_ namespace(string $name) Creates a namespace builder.
  10. * @method Builder\Class_ class(string $name) Creates a class builder.
  11. * @method Builder\Interface_ interface(string $name) Creates an interface builder.
  12. * @method Builder\Trait_ trait(string $name) Creates a trait builder.
  13. * @method Builder\Function_ function(string $name) Creates a function builder.
  14. * @method Builder\Use_ use(string $name) Creates a namespace/class use builder.
  15. */
  16. class BuilderFactory
  17. {
  18. /**
  19. * Creates a namespace builder.
  20. *
  21. * @param null|string|Node\Name $name Name of the namespace
  22. *
  23. * @return Builder\Namespace_ The created namespace builder
  24. */
  25. protected function _namespace($name) {
  26. return new Builder\Namespace_($name);
  27. }
  28. /**
  29. * Creates a class builder.
  30. *
  31. * @param string $name Name of the class
  32. *
  33. * @return Builder\Class_ The created class builder
  34. */
  35. protected function _class($name) {
  36. return new Builder\Class_($name);
  37. }
  38. /**
  39. * Creates an interface builder.
  40. *
  41. * @param string $name Name of the interface
  42. *
  43. * @return Builder\Interface_ The created interface builder
  44. */
  45. protected function _interface($name) {
  46. return new Builder\Interface_($name);
  47. }
  48. /**
  49. * Creates a trait builder.
  50. *
  51. * @param string $name Name of the trait
  52. *
  53. * @return Builder\Trait_ The created trait builder
  54. */
  55. protected function _trait($name) {
  56. return new Builder\Trait_($name);
  57. }
  58. /**
  59. * Creates a method builder.
  60. *
  61. * @param string $name Name of the method
  62. *
  63. * @return Builder\Method The created method builder
  64. */
  65. public function method($name) {
  66. return new Builder\Method($name);
  67. }
  68. /**
  69. * Creates a parameter builder.
  70. *
  71. * @param string $name Name of the parameter
  72. *
  73. * @return Builder\Param The created parameter builder
  74. */
  75. public function param($name) {
  76. return new Builder\Param($name);
  77. }
  78. /**
  79. * Creates a property builder.
  80. *
  81. * @param string $name Name of the property
  82. *
  83. * @return Builder\Property The created property builder
  84. */
  85. public function property($name) {
  86. return new Builder\Property($name);
  87. }
  88. /**
  89. * Creates a function builder.
  90. *
  91. * @param string $name Name of the function
  92. *
  93. * @return Builder\Function_ The created function builder
  94. */
  95. protected function _function($name) {
  96. return new Builder\Function_($name);
  97. }
  98. /**
  99. * Creates a namespace/class use builder.
  100. *
  101. * @param string|Node\Name Name to alias
  102. *
  103. * @return Builder\Use_ The create use builder
  104. */
  105. protected function _use($name) {
  106. return new Builder\Use_($name, Use_::TYPE_NORMAL);
  107. }
  108. public function __call($name, array $args) {
  109. if (method_exists($this, '_' . $name)) {
  110. return call_user_func_array(array($this, '_' . $name), $args);
  111. }
  112. throw new \LogicException(sprintf('Method "%s" does not exist', $name));
  113. }
  114. }