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