/scripts/add_command_methods.php

https://github.com/netweaver/Rediska · PHP · 116 lines · 80 code · 22 blank · 14 comment · 7 complexity · ccb274ae9b0d431b4741341f03e2fef4 MD5 · raw file

  1. <?php
  2. /**
  3. * Add command methods to Rediska, Rediska_Transaction, Rediska_Pipeline and Rediska_Connection_Specified
  4. */
  5. define('REDISKA', __DIR__ . '/../library/Rediska.php');
  6. define('DOCBLOCK', "/**
  7. * Generated command methods by 'scripts/add_command_methods.php'
  8. */");
  9. function getReturn($classReflection, $method)
  10. {
  11. $createReflection = $classReflection->getMethod($method);
  12. $methodDocBlock = $createReflection->getDocComment();
  13. preg_match('/@return\s+(\S+)/i', $methodDocBlock, $matches);
  14. return $matches[1];
  15. }
  16. function getParams($classReflection, $class)
  17. {
  18. $command = file_get_contents(__DIR__ . '/../library/Rediska/Command/' . substr($class, 16) . '.php');
  19. preg_match('/public function create\((.+)/i', $command, $matches);
  20. if (isset($matches[1])) {
  21. return trim($matches[1]);
  22. } else {
  23. if ($classReflection->getParentClass()) {
  24. return getParams($classReflection, $classReflection->getParentClass()->getName());
  25. } else {
  26. die("$class doesnot have a create method");
  27. }
  28. }
  29. }
  30. function updateMethods($file, $methods)
  31. {
  32. $content = file_get_contents($file);
  33. $newContent = substr($content, 0, strpos($content, DOCBLOCK));
  34. $newContent .= DOCBLOCK . "\n" . $methods . "\n}";
  35. file_put_contents($file, $newContent);
  36. }
  37. // Start!
  38. require_once REDISKA;
  39. $rediskaMethods = '';
  40. $transactionMethods = '';
  41. $connectionMethods = '';
  42. $pipelineMethods = '';
  43. $count = 0;
  44. foreach(Rediska_Commands::getList() as $name => $class) {
  45. // Get name
  46. $name = lcfirst(substr($class, 16));
  47. // Get description
  48. $classReflection = new ReflectionClass($class);
  49. $docBlockLines = explode("\n", $classReflection->getDocComment());
  50. array_shift($docBlockLines);
  51. $description = '';
  52. foreach($docBlockLines as $line) {
  53. if (strpos($line, ' * @') !== false) {
  54. break;
  55. }
  56. $description .= ' ' . $line . "\n";
  57. }
  58. $createReflection = $classReflection->getMethod('create');
  59. $createDocBlockLines = explode("\n", $createReflection->getDocComment());
  60. $createDocBlockLines[1] = rtrim($description);
  61. unset($createDocBlockLines[2]);
  62. $createDocBlock = implode("\n", $createDocBlockLines);
  63. // Get params
  64. $params = getParams($classReflection, $class);
  65. $params = str_replace('self::', $class . '::', $params);
  66. // Get return
  67. $return = getReturn($classReflection, 'parseResponse');
  68. if ($return == 'mixed') {
  69. getReturn($classReflection, 'parseResponses');
  70. }
  71. // Rediska
  72. $createDocBlock = preg_replace('/@return .+/', "@return $return", $createDocBlock);
  73. $rediskaMethods .= "\n $createDocBlock
  74. public function $name($params { \$args = func_get_args(); return \$this->_executeCommand('$name', \$args); }\n";
  75. // Connection
  76. $createDocBlock = preg_replace('/@return .+/', "@return $return", $createDocBlock);
  77. $connectionMethods .= "\n $createDocBlock
  78. public function $name($params { \$args = func_get_args(); return \$this->_executeCommand('$name', \$args); }\n";
  79. // Transaction
  80. $createDocBlock = preg_replace('/@return .+/', "@return Rediska_Transaction", $createDocBlock);
  81. $transactionMethods .= "\n $createDocBlock
  82. public function $name($params { \$args = func_get_args(); return \$this->_addCommand('$name', \$args); }\n";
  83. // Pipeline
  84. $createDocBlock = preg_replace('/@return .+/', "@return Rediska_Pipeline", $createDocBlock);
  85. $pipelineMethods .= "\n $createDocBlock
  86. public function $name($params { \$args = func_get_args(); return \$this->_addCommand('$name', \$args); }\n";
  87. $count++;
  88. }
  89. updateMethods(REDISKA, $rediskaMethods);
  90. updateMethods(__DIR__ . '/../library/Rediska/Transaction.php', $transactionMethods);
  91. updateMethods(__DIR__ . '/../library/Rediska/Pipeline.php', $pipelineMethods);
  92. updateMethods(__DIR__ . '/../library/Rediska/Connection/Specified.php', $connectionMethods);
  93. // End :)
  94. print "$count methods added.\n";