FactoryFile.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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) . PHP_EOL;
  44. $code .= $this->generateDeclaration($call->getName(), $method);
  45. // $code .= $this->generateImport($method);
  46. $code .= $this->generateCall($method);
  47. $code .= $this->generateClosing();
  48. return $code;
  49. }
  50. public function generateDeclaration($name, FactoryMethod $method)
  51. {
  52. $code = $this->indent . $this->getDeclarationModifiers()
  53. . 'function ' . $name . '('
  54. . $this->generateDeclarationArguments($method)
  55. . ')' . PHP_EOL . $this->indent . '{' . PHP_EOL;
  56. return $code;
  57. }
  58. public function getDeclarationModifiers()
  59. {
  60. return '';
  61. }
  62. public function generateDeclarationArguments(FactoryMethod $method)
  63. {
  64. if ($method->acceptsVariableArguments()) {
  65. return '/* args... */';
  66. } else {
  67. return $method->getParameterDeclarations();
  68. }
  69. }
  70. public function generateImport(FactoryMethod $method)
  71. {
  72. return $this->indent . self::INDENT . "require_once '" . $method->getClass()->getFile() . "';" . PHP_EOL;
  73. }
  74. public function generateCall(FactoryMethod $method)
  75. {
  76. $code = '';
  77. if ($method->acceptsVariableArguments()) {
  78. $code .= $this->indent . self::INDENT . '$args = func_get_args();' . PHP_EOL;
  79. }
  80. $code .= $this->indent . self::INDENT . 'return ';
  81. if ($method->acceptsVariableArguments()) {
  82. $code .= 'call_user_func_array(array(\''
  83. . '\\' . $method->getClassName() . '\', \''
  84. . $method->getName() . '\'), $args);' . PHP_EOL;
  85. } else {
  86. $code .= '\\' . $method->getClassName() . '::'
  87. . $method->getName() . '('
  88. . $method->getParameterInvocations() . ');' . PHP_EOL;
  89. }
  90. return $code;
  91. }
  92. public function generateClosing()
  93. {
  94. return $this->indent . '}' . PHP_EOL;
  95. }
  96. public function write()
  97. {
  98. file_put_contents($this->file, $this->code);
  99. }
  100. }