No Description

BuilderFactory.php 3.0KB

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