BuilderFactory.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * "class", "interface" and "function" are reserved keywords, so the methods are defined as _class(),
  4. * _interface() and _function() in the class and are made available as class(), interface() and function()
  5. * through __call() magic.
  6. *
  7. * @method PHPParser_Builder_Class class(string $name) Creates a class builder.
  8. * @method PHPParser_Builder_Function function(string $name) Creates a function builder
  9. * @method PHPParser_Builder_Interface interface(string $name) Creates an interface builder.
  10. */
  11. class PHPParser_BuilderFactory
  12. {
  13. /**
  14. * Creates a class builder.
  15. *
  16. * @param string $name Name of the class
  17. *
  18. * @return PHPParser_Builder_Class The created class builder
  19. */
  20. protected function _class($name) {
  21. return new PHPParser_Builder_Class($name);
  22. }
  23. /**
  24. * Creates a interface builder.
  25. *
  26. * @param string $name Name of the interface
  27. *
  28. * @return PHPParser_Builder_Class The created interface builder
  29. */
  30. protected function _interface($name) {
  31. return new PHPParser_Builder_Interface($name);
  32. }
  33. /**
  34. * Creates a method builder.
  35. *
  36. * @param string $name Name of the method
  37. *
  38. * @return PHPParser_Builder_Method The created method builder
  39. */
  40. public function method($name) {
  41. return new PHPParser_Builder_Method($name);
  42. }
  43. /**
  44. * Creates a parameter builder.
  45. *
  46. * @param string $name Name of the parameter
  47. *
  48. * @return PHPParser_Builder_Param The created parameter builder
  49. */
  50. public function param($name) {
  51. return new PHPParser_Builder_Param($name);
  52. }
  53. /**
  54. * Creates a property builder.
  55. *
  56. * @param string $name Name of the property
  57. *
  58. * @return PHPParser_Builder_Property The created property builder
  59. */
  60. public function property($name) {
  61. return new PHPParser_Builder_Property($name);
  62. }
  63. /**
  64. * Creates a function builder.
  65. *
  66. * @param string $name Name of the function
  67. *
  68. * @return PHPParser_Builder_Property The created function builder
  69. */
  70. protected function _function($name) {
  71. return new PHPParser_Builder_Function($name);
  72. }
  73. public function __call($name, array $args) {
  74. if (method_exists($this, '_' . $name)) {
  75. return call_user_func_array(array($this, '_' . $name), $args);
  76. }
  77. throw new LogicException(sprintf('Method "%s" does not exist', $name));
  78. }
  79. }