1 | <?php |
||
7 | trait Scripting |
||
8 | { |
||
9 | /* |
||
10 | * Available Script Commands |
||
11 | */ |
||
12 | protected $SCRIPT_COMMANDS = ['LOAD', 'FLUSH', 'KILL', 'EXISTS']; |
||
13 | |||
14 | /** |
||
15 | * Evaluate a LUA script serverside. |
||
16 | * |
||
17 | * See: https://redis.io/commands/eval. |
||
18 | * |
||
19 | * @param string $script |
||
20 | * @param array $arguments |
||
21 | * @param int|integer $numKeys |
||
22 | * |
||
23 | * @return mixed What is returned depends on what the LUA script |
||
24 | * itself returns, which could be a scalar value |
||
25 | * (int/string), or an array. Arrays that are returned |
||
26 | * can also contain other arrays, if that's how it was |
||
27 | * set up in your LUA script. If there is an error |
||
28 | * executing the LUA script, the getLastError() |
||
29 | * function can tell you the message that came back |
||
30 | * from Redis (e.g. compile error). |
||
31 | */ |
||
32 | public function eval(string $script, ?array $arguments = null, ?int $numKeys = null) |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Evaluate a LUA script serverside, from the SHA1 hash of the script instead |
||
44 | * of the script itself. |
||
45 | * |
||
46 | * In order to run this command Redis will have to have already loaded the |
||
47 | * script, either by running it or via the SCRIPT LOAD command. |
||
48 | * |
||
49 | * See: https://redis.io/commands/evalsha. |
||
50 | * |
||
51 | * @param string $sha1 The sha1 encoded hash of the script you want to run. |
||
52 | * @param array|null $arguments Arguments to pass to the LUA script. |
||
53 | * @param int|null $numKeys The number of arguments that should go into the |
||
54 | * KEYS array, vs. the ARGV array when Redis spins |
||
55 | * the script (optional). |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function evalSha(string $sha1, ?array $arguments = null, ?int $numKeys = null) |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Execute the Redis SCRIPT command to perform various operations on the |
||
71 | * scripting subsystem. |
||
72 | * |
||
73 | * See: https://redis.io/commands/script-load. |
||
74 | * See: https://redis.io/commands/script-flush. |
||
75 | * |
||
76 | * @param string $command |
||
77 | * @param splat $scripts |
||
78 | * |
||
79 | * @return mixed SCRIPT LOAD will return the SHA1 hash of the |
||
80 | * passed script on success, and FALSE on |
||
81 | * failure. |
||
82 | * SCRIPT FLUSH should always return TRUE |
||
83 | * SCRIPT KILL will return true if a script was |
||
84 | * able to be killed and false if not. |
||
85 | * SCRIPT EXISTS will return an array with TRUE |
||
86 | * or FALSE for each passed script. |
||
87 | */ |
||
88 | public function script(string $command, ...$scripts) |
||
110 | |||
111 | /** |
||
112 | * The last error message (if any) |
||
113 | * |
||
114 | * |
||
115 | * @return mixed|string|null A string with the last returned script |
||
116 | * based error message, or NULL if there |
||
117 | * is no error. |
||
118 | */ |
||
119 | public function getLastError(): ?string |
||
123 | |||
124 | public function clearLastError(): bool |
||
128 | |||
129 | public function prefix(): bool |
||
133 | |||
134 | public function unserialize(): bool |
||
138 | |||
139 | public function serialize(): bool |
||
143 | } |
||
144 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: