You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.0 KiB

3 years ago
  1. <?php
  2. /*
  3. Copyright (c) 2009 hamcrest.org
  4. */
  5. abstract class FactoryFile
  6. {
  7. /**
  8. * Hamcrest standard is two spaces for each level of indentation.
  9. *
  10. * @var string
  11. */
  12. const INDENT = ' ';
  13. private $indent;
  14. private $file;
  15. private $code;
  16. public function __construct($file, $indent)
  17. {
  18. $this->file = $file;
  19. $this->indent = $indent;
  20. }
  21. abstract public function addCall(FactoryCall $call);
  22. abstract public function build();
  23. public function addFileHeader()
  24. {
  25. $this->code = '';
  26. $this->addPart('file_header');
  27. }
  28. public function addPart($name)
  29. {
  30. $this->addCode($this->readPart($name));
  31. }
  32. public function addCode($code)
  33. {
  34. $this->code .= $code;
  35. }
  36. public function readPart($name)
  37. {
  38. return file_get_contents(__DIR__ . "/parts/$name.txt");
  39. }
  40. public function generateFactoryCall(FactoryCall $call)
  41. {
  42. $method = $call->getMethod();
  43. $code = $method->getComment($this->indent) . "\n";
  44. $code .= $this->generateDeclaration($call->getName(), $method);
  45. $code .= $this->generateCall($method);
  46. $code .= $this->generateClosing();
  47. return $code;
  48. }
  49. public function generateDeclaration($name, FactoryMethod $method)
  50. {
  51. $code = $this->indent . $this->getDeclarationModifiers()
  52. . 'function ' . $name . '('
  53. . $this->generateDeclarationArguments($method)
  54. . ')' . "\n" . $this->indent . '{' . "\n";
  55. return $code;
  56. }
  57. public function getDeclarationModifiers()
  58. {
  59. return '';
  60. }
  61. public function generateDeclarationArguments(FactoryMethod $method)
  62. {
  63. if ($method->acceptsVariableArguments()) {
  64. return '/* args... */';
  65. } else {
  66. return $method->getParameterDeclarations();
  67. }
  68. }
  69. public function generateImport(FactoryMethod $method)
  70. {
  71. return $this->indent . self::INDENT . "require_once '" . $method->getClass()->getFile() . "';" . "\n";
  72. }
  73. public function generateCall(FactoryMethod $method)
  74. {
  75. $code = '';
  76. if ($method->acceptsVariableArguments()) {
  77. $code .= $this->indent . self::INDENT . '$args = func_get_args();' . "\n";
  78. }
  79. $code .= $this->indent . self::INDENT . 'return ';
  80. if ($method->acceptsVariableArguments()) {
  81. $code .= 'call_user_func_array(array(\''
  82. . '\\' . $method->getClassName() . '\', \''
  83. . $method->getName() . '\'), $args);' . "\n";
  84. } else {
  85. $code .= '\\' . $method->getClassName() . '::'
  86. . $method->getName() . '('
  87. . $method->getParameterInvocations() . ');' . "\n";
  88. }
  89. return $code;
  90. }
  91. public function generateClosing()
  92. {
  93. return $this->indent . '}' . "\n";
  94. }
  95. public function write()
  96. {
  97. file_put_contents($this->file, $this->code);
  98. }
  99. }