No Description

FactoryFile.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }