update-commands 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This scripts is used to update the PHPdoc annotations for redis commands in the Connection.php class.
  5. */
  6. use yii\helpers\Inflector;
  7. use yii\helpers\Console;
  8. foreach([
  9. __DIR__ . '/../vendor/yiisoft/yii2/Yii.php',
  10. __DIR__ . '/../../../framework/Yii.php',
  11. ] as $yii) {
  12. if (is_file($yii)) {
  13. include($yii);
  14. break;
  15. }
  16. }
  17. $commandsHtml = file_get_contents('https://redis.io/commands');
  18. //echo $commandsHtml;
  19. preg_match_all('~<li.+? data-name=\'([\w ]+)\'>\s*<a href=\'([\w /-]+)\'>.*?<span class=\'args\'>(.*?)</span>.*?<span class=\'summary\'>(.*?)</span>~si', $commandsHtml, $matches);
  20. //print_r($matches);
  21. $commands = [];
  22. $n = count($matches[0]);
  23. for($i = 0; $i < $n; $i++) {
  24. $name = trim($matches[1][$i]);
  25. $url = 'https://redis.io' . $matches[2][$i];
  26. $args = array_map('trim', explode("\n", trim($matches[3][$i])));
  27. $descr = trim($matches[4][$i]);
  28. $commands[$name] = [
  29. 'url' => $url,
  30. 'args' => $args,
  31. 'descr' => $descr,
  32. ];
  33. }
  34. // print_r($commands);
  35. $commandArray = [];
  36. foreach($commands as $name => $info) {
  37. $name = strtoupper($name);
  38. $commandArray[] = " '$name', // " . $info['descr'];
  39. }
  40. print_r($commandArray);
  41. // command args that are hard to transform into php automatically
  42. $specialArgs = [
  43. 'bitfield' => '$key, ...$operations',
  44. 'clientKill' => '...$filters',
  45. 'clientReply' => '$option',
  46. 'clusterFailover' => '$option = null',
  47. 'clusterReset' => '$resetType = "SOFT"',
  48. 'clusterSetslot' => '$slot, $type, $nodeid = null',
  49. 'geoadd' => '$key, $longitude, $latitude, $member, ...$more',
  50. 'georadius' => '$key, $longitude, $latitude, $radius, $metric, ...$options',
  51. 'georadiusbymember' => '$key, $member, $radius, $metric, ...$options',
  52. 'hmset' => '$key, $field, $value, ...$more',
  53. 'linsert' => '$key, $where, $pivot, $value',
  54. 'migrate' => '$host, $port, $key, $destinationDb, $timeout, ...$options',
  55. 'mset' => '...$keyValuePairs',
  56. 'msetnx' => '...$keyValuePairs',
  57. 'scriptDebug' => '$option',
  58. 'set' => '$key, $value, ...$options',
  59. 'shutdown' => '$saveOption = null',
  60. 'sort' => '$key, ...$options',
  61. 'zadd' => '$key, ...$options',
  62. 'zinterstore' => '$destination, $numkeys, $key, ...$options',
  63. 'zunionstore' => '$destination, $numkeys, $key, ...$options',
  64. ];
  65. $methods = [];
  66. foreach($commands as $name => $info) {
  67. $name = lcfirst(Inflector::camelize($name));
  68. if (isset($specialArgs[$name])) {
  69. $arglist = $specialArgs[$name];
  70. } else {
  71. $arglist = implode(', ', array_map(function($arg) {
  72. if (empty($arg)) {
  73. return '';
  74. }
  75. if (strpos($arg, '[') === 0 && strpos($arg, '...') === false) {
  76. // optional arguments
  77. $arg = substr($arg, 1, -1);
  78. return implode(', ', array_map(function($a) {return '$'.$a.' = null'; }, explode(' ', $arg)));
  79. } elseif (preg_match('~^(\[?)([\w-]+) \[\2 ...\](\]?)~', $arg, $m)) {
  80. return '...$'. lcfirst(Inflector::camelize($m[2])) . 's';
  81. }
  82. return '$' . lcfirst(Inflector::camelize($arg));
  83. }, $info['args']));
  84. }
  85. $methods[] = " * @method mixed $name($arglist) " . $info['descr'] . '. <' . $info['url'] . '>';
  86. }
  87. print_r($methods);
  88. if (!Console::confirm('Update redis\Connection class now?')) {
  89. Console::stderr("aborting...\n");
  90. exit(1);
  91. }
  92. Console::stderr('Updating class docs...');
  93. $file = __DIR__.'/../src/Connection.php';
  94. $content = file_get_contents($file);
  95. if ($content === false) {
  96. Console::stderr("failed to read file $file !\n");
  97. exit(1);
  98. }
  99. $newContent = $content;
  100. $newContent = preg_replace('~( \* @method.*?\n)+~si', implode("\n", $methods) . "\n", $newContent);
  101. $newContent = preg_replace('~\$redisCommands = \[.+?\];~si', "\$redisCommands = [\n" . implode("\n", $commandArray) . "\n ];", $newContent);
  102. if ($content === $newContent) {
  103. Console::stderr("no changes!\n");
  104. } else {
  105. file_put_contents($file, $newContent);
  106. Console::stderr("done.\n");
  107. }