PluginProbe
Media Cloud Sync / trunk
Media Cloud Sync vtrunk
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / JmesPath / DebugRuntime.php

DebugRuntime.php in Media Cloud Sync trunk, at includes/sdk/s3/JmesPath/DebugRuntime.php

86 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\JmesPath;
4
5 /**
6 * Provides CLI debugging information for the AST and Compiler runtimes.
7 */
8 class DebugRuntime
9 {
10 private $runtime;
11 private $out;
12 private $lexer;
13 private $parser;
14 public function __construct(callable $runtime, $output = null)
15 {
16 $this->runtime = $runtime;
17 $this->out = $output ?: \STDOUT;
18 $this->lexer = new Lexer();
19 $this->parser = new Parser($this->lexer);
20 }
21 public function __invoke($expression, $data)
22 {
23 if ($this->runtime instanceof CompilerRuntime) {
24 return $this->debugCompiled($expression, $data);
25 }
26 return $this->debugInterpreted($expression, $data);
27 }
28 private function debugInterpreted($expression, $data)
29 {
30 return $this->debugCallback(function () use($expression, $data) {
31 $runtime = $this->runtime;
32 return $runtime($expression, $data);
33 }, $expression, $data);
34 }
35 private function debugCompiled($expression, $data)
36 {
37 $result = $this->debugCallback(function () use($expression, $data) {
38 $runtime = $this->runtime;
39 return $runtime($expression, $data);
40 }, $expression, $data);
41 $this->dumpCompiledCode($expression);
42 return $result;
43 }
44 private function dumpTokens($expression)
45 {
46 $lexer = new Lexer();
47 \fwrite($this->out, "Tokens\n======\n\n");
48 $tokens = $lexer->tokenize($expression);
49 foreach ($tokens as $t) {
50 \fprintf($this->out, "%3d %-13s %s\n", $t['pos'], $t['type'], \json_encode($t['value']));
51 }
52 \fwrite($this->out, "\n");
53 }
54 private function dumpAst($expression)
55 {
56 $parser = new Parser();
57 $ast = $parser->parse($expression);
58 \fwrite($this->out, "AST\n========\n\n");
59 \fwrite($this->out, \json_encode($ast, \JSON_PRETTY_PRINT) . "\n");
60 }
61 private function dumpCompiledCode($expression)
62 {
63 \fwrite($this->out, "Code\n========\n\n");
64 $dir = \sys_get_temp_dir();
65 $hash = \md5($expression);
66 $functionName = "jmespath_{$hash}";
67 $filename = "{$dir}/{$functionName}.php";
68 \fwrite($this->out, "File: {$filename}\n\n");
69 \fprintf($this->out, \file_get_contents($filename));
70 }
71 private function debugCallback(callable $debugFn, $expression, $data)
72 {
73 \fprintf($this->out, "Expression\n==========\n\n%s\n\n", $expression);
74 $this->dumpTokens($expression);
75 $this->dumpAst($expression);
76 \fprintf($this->out, "\nData\n====\n\n%s\n\n", \json_encode($data, \JSON_PRETTY_PRINT));
77 $startTime = \microtime(\true);
78 $result = $debugFn();
79 $total = \microtime(\true) - $startTime;
80 \fprintf($this->out, "\nResult\n======\n\n%s\n\n", \json_encode($result, \JSON_PRETTY_PRINT));
81 \fwrite($this->out, "Time\n====\n\n");
82 \fprintf($this->out, "Total time: %f ms\n\n", $total);
83 return $result;
84 }
85 }
86