getType() == FCGI::PARAMS) {
20 | /* @var $record Params */
21 | $fcgiParams = array_merge($fcgiParams, $record->getValues());
22 | } elseif ($record->getType() == FCGI::STDIN) {
23 | /* @var $record Stdin */
24 | if ($record->getContentLength() > 0) {
25 | $fcgiStdin .= $record->getContentData();
26 | }
27 | }
28 | }
29 |
30 | // request headers & body
31 | return ['params' => $fcgiParams, 'stdin' => $fcgiStdin,];
32 | }
33 |
34 | public static function parseResponse($fcgiResp)
35 | {
36 | $resp = '';
37 | while (FrameParser::hasFrame($fcgiResp)) {
38 | $record = FrameParser::parseFrame($fcgiResp);
39 | if ($record->getType() == FCGI::STDOUT) {
40 | $resp .= $record->getContentData();
41 | }
42 | }
43 |
44 | return $resp;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Parser/ParseHTTP.php:
--------------------------------------------------------------------------------
1 | $method,
36 | 'url' => $url,
37 | 'uri' => $uri,
38 | 'version' => trim($version),
39 | 'continue100' => $continue100,
40 | 'body' => $aReq[1] ?? '',
41 | ];
42 | }
43 |
44 | public static function parseResp($response)
45 | {
46 | $lineIdx = strpos($response, "\n");
47 | $responseLine = substr($response, 0, $lineIdx);
48 | list($version, $httpCode, $httpDesc) = explode(' ', $responseLine);
49 |
50 | $aResp = explode("\r\n\r\n", $response);
51 | $aHeader = explode("\r\n", $aResp[0]);
52 | $sBody = $aResp[1];
53 |
54 | return ['version' => $version, 'httpCode' => intval($httpCode), 'header' => $aHeader, 'body' => trim($sBody),];
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Parser/ParseInterface.php:
--------------------------------------------------------------------------------
1 | pushIncoming($request) as $command) {
37 | /* @var $command \Clue\Redis\Protocol\Model\Request */
38 | $commands[] = ['command' => $command->getCommand(), 'args' => $command->getArgs(),];
39 | }
40 | return $commands;
41 | }
42 | }
--------------------------------------------------------------------------------
/php/midi/src/Midi/Parser/ParseReplayedInterface.php:
--------------------------------------------------------------------------------
1 | readMessageBegin($methodName, $messageType, $messageSeqId);
27 | } catch (\Exception $e) {
28 | return false;
29 | }
30 |
31 | if (preg_match('~(\w+).*?(\w+).*?(\w+).{3}(\S{30})~', $request, $matches)) {
32 | list($_, $_, $name, $id, $detail) = $matches;
33 | $content = "$name $id - $detail";
34 | } else {
35 | $content = substr($request, 0, 40);
36 | }
37 |
38 | return [
39 | 'methodName' => $methodName,
40 | 'messageType' => $messageType,
41 | 'messageSeqId' => $messageSeqId,
42 | 'content' => $content,
43 | ];
44 | }
45 | }
46 |
47 |
48 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Parser/ParseThrift.php:
--------------------------------------------------------------------------------
1 | readMessageBegin($methodName, $messageType, $messageSeqId);
27 | } catch (\Exception $e) {
28 | return false;
29 | }
30 |
31 | return ['methodName' => $methodName, 'messageType' => $messageType, 'messageSeqId' => $messageSeqId,];
32 | }
33 | }
34 |
35 |
36 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/Event/CommandConfigureEvent.php:
--------------------------------------------------------------------------------
1 | command = $command;
22 | }
23 |
24 | public function addArgument($name, $mode = null, $description = '', $default = null)
25 | {
26 | $this->command->addArgument($name, $mode, $description, $default);
27 | return $this;
28 | }
29 |
30 | public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
31 | {
32 | $this->command->addOption($name, $shortcut, $mode, $description, $default);
33 | return $this;
34 | }
35 |
36 | public function getName()
37 | {
38 | return $this->command->getName();
39 | }
40 |
41 | public function setDescription($description)
42 | {
43 | $this->command->setDescription($description);
44 | return $this;
45 | }
46 | }
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/Event/CommandEvent.php:
--------------------------------------------------------------------------------
1 | commandName = $commandName;
41 | $this->input = $input;
42 | $this->output = $output;
43 | }
44 |
45 | /**
46 | * Returns the command input interface
47 | *
48 | * @return InputInterface
49 | */
50 | public function getInput()
51 | {
52 | return $this->input;
53 | }
54 |
55 | /**
56 | * Retrieves the command output interface
57 | *
58 | * @return OutputInterface
59 | */
60 | public function getOutput()
61 | {
62 | return $this->output;
63 | }
64 |
65 | /**
66 | * Retrieves the name of the command being run
67 | *
68 | * @return string
69 | */
70 | public function getCommandName()
71 | {
72 | return $this->commandName;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/Event/PostCommandEvent.php:
--------------------------------------------------------------------------------
1 | callFromInbound = $callFromIndbound;
45 | $this->returnInbound = $returnInbound;
46 | $this->actions = $actions;
47 | $this->mockFiles = $mockFiles ?? [];
48 | $this->redirectDirs = $redirectDirs ?? [];
49 | }
50 |
51 | /**
52 | * @return CallFromInbound
53 | */
54 | public function getCallFromInbound()
55 | {
56 | return $this->callFromInbound;
57 | }
58 |
59 | public function setCallFromInbound($callFromInbound)
60 | {
61 | $this->callFromInbound = $callFromInbound;
62 | }
63 |
64 | public function getReturnInbound()
65 | {
66 | return $this->returnInbound;
67 | }
68 |
69 | public function setReturnInbound($returnInbound)
70 | {
71 | $this->returnInbound = $returnInbound;
72 | }
73 |
74 | public function getActions()
75 | {
76 | return $this->actions;
77 | }
78 |
79 | public function setActions($actions)
80 | {
81 | $this->actions = $actions;
82 | }
83 |
84 | public function getMockFiles()
85 | {
86 | return $this->mockFiles;
87 | }
88 |
89 | public function setMockFiles($mockFiles)
90 | {
91 | $this->mockFiles = $mockFiles;
92 | }
93 |
94 | public function getRedirectDirs()
95 | {
96 | return $this->redirectDirs;
97 | }
98 |
99 | public function setRedirectDirs($redirectDirs)
100 | {
101 | $this->redirectDirs = $redirectDirs;
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/Event/PostReplaySessionEvent.php:
--------------------------------------------------------------------------------
1 | replayedSession = $replayedSession;
33 | $this->args = $args;
34 | }
35 |
36 | /**
37 | * Returns the replayed session
38 | *
39 | * @return ReplayedSession
40 | */
41 | public function getReplayedSession()
42 | {
43 | return $this->replayedSession;
44 | }
45 |
46 | /**
47 | * @param ReplayedSession $replayedSession
48 | */
49 | public function setReplayedSession(ReplayedSession $replayedSession)
50 | {
51 | $this->replayedSession = $replayedSession;
52 | }
53 |
54 | /**
55 | * @return array
56 | */
57 | public function getArgs()
58 | {
59 | return $this->args;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/Event/PreCommandEvent.php:
--------------------------------------------------------------------------------
1 | app = $app;
36 | $this->options = $options;
37 | $this->output = $output;
38 | }
39 |
40 | /**
41 | * @return Command
42 | */
43 | public function getCommand($name)
44 | {
45 | return $this->app->find($name);
46 | }
47 |
48 | public function getOptions()
49 | {
50 | return $this->options;
51 | }
52 |
53 | public function setOptions($options)
54 | {
55 | $this->options = $options;
56 | }
57 |
58 | public function getOutput()
59 | {
60 | return $this->output;
61 | }
62 | }
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/Event/PreReplaySessionEvent.php:
--------------------------------------------------------------------------------
1 | replayingSession = $replayingSession;
27 | }
28 |
29 | /**
30 | * Returns the replaying session
31 | *
32 | * @return ReplayingSession
33 | */
34 | public function getReplayingSession()
35 | {
36 | return $this->replayingSession;
37 | }
38 |
39 | public function setReplayingSession($replayingSession)
40 | {
41 | $this->replayingSession = $replayingSession;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/Event/SessionsSolvingEvent.php:
--------------------------------------------------------------------------------
1 | input = $input;
35 | $this->output = $output;
36 | }
37 |
38 | /**
39 | * Returns the command input interface
40 | *
41 | * @return InputInterface
42 | */
43 | public function getInput()
44 | {
45 | return $this->input;
46 | }
47 |
48 | /**
49 | * Retrieves the command output interface
50 | *
51 | * @return OutputInterface
52 | */
53 | public function getOutput()
54 | {
55 | return $this->output;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Plugin/PluginEvents.php:
--------------------------------------------------------------------------------
1 | getOption('file');
28 | if (empty($files)) {
29 | throw new ResolveInvalidParam(Message::RUN_COMMAND_INVALID_PARAMS);
30 | }
31 |
32 | $sessions = [];
33 | foreach ($files as $file) {
34 | $file = trim($file);
35 | if (!file_exists($file)) {
36 | $output->writeln("Session file '$file' not found!");
37 | continue;
38 | }
39 | $session = json_decode(file_get_contents($file), true);
40 | if (json_last_error() !== JSON_ERROR_NONE) {
41 | $output->writeln("Session file '$file' not invalid json!");
42 | continue;
43 | }
44 | $sessions[] = $session;
45 | }
46 |
47 | return $sessions;
48 | }
49 | }
--------------------------------------------------------------------------------
/php/midi/src/Midi/Resolver/ResolverInterface.php:
--------------------------------------------------------------------------------
1 | start($name);
39 | }
40 |
41 | public static function stop($name, $verbosity = OutputInterface::VERBOSITY_VERBOSE)
42 | {
43 | self::init();
44 | $event = self::$stopWatch->stop($name);
45 | $duration = $event->getDuration();
46 | self::$output->writeln("$name spent $duration ms ", $verbosity);
47 | return $duration;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/php/midi/src/Midi/Util/OS.php:
--------------------------------------------------------------------------------
1 | assertSame($config->get('koala', 'inbound-port'), 5514);
14 | $config->merge([
15 | 'koala' => [
16 | 'inbound-port' => 6515,
17 | ],
18 | ]);
19 | $this->assertSame($config->get('koala', 'inbound-port'), 6515);
20 | }
21 |
22 | public function testConfigMerge()
23 | {
24 | $config = new Config();
25 | $this->assertSame($config->get('php', 'preload-plugins'), ['Midi\ElasticPlugin']);
26 |
27 | $config->merge([
28 | 'php' => [
29 | 'preload-plugins' => [
30 | 'Midi\Plugin',
31 | ],
32 | ],
33 | ]);
34 | $this->assertSame($config->get('php', 'preload-plugins'), ['Midi\ElasticPlugin', 'Midi\Plugin']);
35 | }
36 |
37 | public function testMergeUniq()
38 | {
39 | $config = new Config();
40 | $config->merge([
41 | 'php' => [
42 | 'preload-plugins' => [
43 | 'Midi\Plugin',
44 | 'Midi\Plugin',
45 | ],
46 | ],
47 | ]);
48 | $this->assertSame($config->get('php', 'preload-plugins'), ['Midi\ElasticPlugin', 'Midi\Plugin']);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/php/midi/tests/Midi/Test/ContainerTest.php:
--------------------------------------------------------------------------------
1 | assertInstanceOf(Container::class, $c);
15 | }
16 |
17 | /**
18 | * @expectedException \Midi\Exception\ContainerValueNotFoundException
19 | */
20 | public function testNotFound()
21 | {
22 | Container::make('abc');
23 | }
24 |
25 | public function testBind()
26 | {
27 | Container::bind('abc', 'abc');
28 | $this->assertSame(Container::make('abc'), 'abc');
29 | }
30 |
31 | public function testNotHas()
32 | {
33 | $this->assertNotTrue(Container::ins()->has('bar'));
34 | }
35 |
36 | public function testHas()
37 | {
38 | Container::bind('foo', 'abc');
39 | $this->assertTrue(Container::ins()->has('foo'));
40 | }
41 |
42 | public function testSingleton()
43 | {
44 | $obj = new \stdClass();
45 | Container::bind('abc', $obj);
46 |
47 | $a1 = Container::make('abc');
48 | $a2 = Container::make('abc');
49 | $this->assertSame(spl_object_hash($a1), spl_object_hash($a2));
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/php/midi/tests/Midi/Test/Koala/MatcherTest.php:
--------------------------------------------------------------------------------
1 | assertSame(Matcher::cosineSimilarity([], []), 0);
13 | }
14 |
15 | public function test50Similarity()
16 | {
17 | $a = [1 => 1, 2 => 1, 3 => 1, 4 => 1,];
18 | $b = [1 => 1, 3 => 1, 5 => 1, 7 => 1,];
19 | $this->assertSame(Matcher::cosineSimilarity($a, $b), 0.5);
20 | }
21 |
22 | public function test100Similarity()
23 | {
24 | $a = [1 => 1, 2 => 1, 3 => 1, 4 => 1,];
25 | $b = [1 => 1, 2 => 1, 3 => 1, 4 => 1,];
26 | $this->assertSame(Matcher::cosineSimilarity($a, $b), 1.0);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/php/midi/tests/Midi/Test/Parser/ParseHTTPTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(ParseHTTP::match($request), true);
23 | $this->assertEquals(ParseHTTP::parse($request),
24 | [
25 | 'method' => "POST",
26 | 'url' => "/service/wsgsigCheck",
27 | 'uri' => "/service/wsgsigCheck",
28 | 'version' => "HTTP/1.1",
29 | 'continue100' => 1,
30 | 'body' => '',
31 | ]
32 | );
33 | }
34 |
35 | public function testResponse() {
36 | $response = <<assertSame(ParseHTTP::parseResp($response),
41 | [
42 | 'version' => 'HTTP/1.1',
43 | 'httpCode' => 100,
44 | 'header' => [
45 | "HTTP/1.1 100 Continue\n"
46 | ],
47 | 'body' => '',
48 | ]
49 | );
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/php/midi/tests/Midi/Test/Parser/ParseMySQLTest.php:
--------------------------------------------------------------------------------
1 | assertSame(ParseMySQL::match($request), ParseMySQL::TYPE_C_S_COM);
14 | }
15 |
16 | public function testAuthPass() {
17 | $response = "\x07\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00";
18 | $this->assertSame(ParseMySQL::isAuthOk($response), true);
19 | }
20 |
21 | public function testUnpack() {
22 | $request = "\x07\x00\x00\x00\x02gs_abc";
23 | $this->assertSame(ParseMySQL::match($request), ParseMySQL::TYPE_C_S_COM);
24 | $this->assertSame(ParseMySQL::getMessageLen($request), 7);
25 | $this->assertSame(ParseMySQL::validMessageLen($request), true);
26 | $this->assertSame(ParseMySQL::getReqMessageType($request), "\x02");
27 | $this->assertSame(ParseMySQL::toString($request), 'USE gs_abc');
28 |
29 | $request = "\x0f\x00\x00\x00\x03SET NAMES utf8";
30 | $this->assertSame(ParseMySQL::getMessageLen($request), 15);
31 | $this->assertSame(ParseMySQL::validMessageLen($request), true);
32 | $this->assertSame(ParseMySQL::getReqMessageType($request), "\x03");
33 | $this->assertSame(ParseMySQL::toString($request), "SET NAMES utf8");
34 | }
35 |
36 | public function testHand() {
37 | $this->markTestSkipped("need set body");
38 | // add request to $req
39 | $req = "";
40 | $this->assertSame(ParseMySQL::isLoginAuth($req), true);
41 | }
42 |
43 | public function testisHandInit() {
44 | $this->markTestSkipped("need set body");
45 | // add response to $response
46 | $response = "";
47 | $this->assertSame(ParseMySQL::isHandShake($response), true);
48 |
49 | $protocol = unpack("C", $response[4]);
50 | $this->assertSame($protocol[1], 10);
51 |
52 | $response = substr($response, 5);
53 | $versionPos = strpos($response, "\x00");
54 | $serverVersionOriginal = substr($response, 0, $versionPos);
55 |
56 | $response = substr($response, $versionPos + 1);
57 | $threadId = substr($response, 0, 4);
58 |
59 | $authData1 = substr($response, 4, 8);
60 |
61 | $pad = $response[12];
62 | $this->assertSame($pad, "\x00");
63 |
64 | $response = substr($response, 13);
65 | $charset = unpack("C", $response[3]);
66 |
67 | $this->assertSame(substr($response, 8, 10), "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/php/midi/tests/bootstrap.php:
--------------------------------------------------------------------------------
1 |