Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 1d9e04c

Browse files
committed
A lot of wiring code
1 parent 298d460 commit 1d9e04c

File tree

7 files changed

+184
-9
lines changed

7 files changed

+184
-9
lines changed

src/Actions/Action.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\Actions;
5+
6+
7+
interface Action
8+
{
9+
public function call($header, $content);
10+
}

src/Actions/ExecuteAction.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\Actions;
5+
6+
7+
use Litipk\JupyterPHP\JupyterBroker;
8+
use React\ZMQ\SocketWrapper;
9+
10+
11+
final class ExecuteAction implements Action
12+
{
13+
/** @var JupyterBroker */
14+
private $broker;
15+
16+
/** @var SocketWrapper */
17+
private $iopubSocket;
18+
19+
/** @var SocketWrapper */
20+
private $shellSocket;
21+
22+
23+
/**
24+
* ExecuteAction constructor.
25+
* @param JupyterBroker $broker
26+
* @param SocketWrapper $iopubSocket
27+
* @param SocketWrapper $shellSocket
28+
*/
29+
public function __construct(JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket)
30+
{
31+
$this->broker = $broker;
32+
$this->iopubSocket = $iopubSocket;
33+
$this->shellSocket = $shellSocket;
34+
}
35+
36+
public function call($header, $content)
37+
{
38+
$this->broker->send(
39+
$this->iopubSocket, 'status', ['execution_state' => 'busy'], $header
40+
);
41+
42+
$execCount = isset($content->execution_count) ? $content->execution_count : 0;
43+
44+
// TODO: Here is where PsySH goes
45+
$vars_before = get_defined_vars();
46+
ob_start();
47+
$result = eval($content->code);
48+
$stdOut = ob_get_contents();
49+
ob_end_clean();
50+
$vars_after = get_defined_vars();
51+
// TODO
52+
53+
$this->broker->send($this->shellSocket, 'execute_reply', ['status' => 'ok'], $header);
54+
$this->broker->send($this->iopubSocket, 'stream', ['name' => 'stdout', 'data' => $stdOut], $header);
55+
$this->broker->send(
56+
$this->iopubSocket,
57+
'execute_result',
58+
['execution_count' => $execCount + 1, 'data' => $result, 'metadata' => []],
59+
$header
60+
);
61+
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'idle'], $header);
62+
}
63+
}

src/Actions/HistoryAction.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\Actions;
5+
6+
7+
use Litipk\JupyterPHP\JupyterBroker;
8+
use React\ZMQ\SocketWrapper;
9+
10+
11+
final class HistoryAction implements Action
12+
{
13+
/** @var JupyterBroker */
14+
private $broker;
15+
16+
/** @var SocketWrapper */
17+
private $shellSocket;
18+
19+
20+
/**
21+
* ExecuteAction constructor.
22+
* @param JupyterBroker $broker
23+
* @param SocketWrapper $shellSocket
24+
*/
25+
public function __construct(JupyterBroker $broker, SocketWrapper $shellSocket)
26+
{
27+
$this->broker = $broker;
28+
$this->shellSocket = $shellSocket;
29+
}
30+
31+
public function call($header, $content)
32+
{
33+
$this->broker->send($this->shellSocket, 'history_reply', ['history' => array()], $header);
34+
}
35+
}

src/Actions/KernelInfoAction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\Actions;
5+
6+
7+
final class KernelInfoAction implements Action
8+
{
9+
public function call($header, $content)
10+
{
11+
// TODO: Implement call() method.
12+
}
13+
}

src/Actions/ShutdownAction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\Actions;
5+
6+
7+
final class ShutdownAction implements Action
8+
{
9+
public function call($header, $content)
10+
{
11+
// TODO: Implement call() method.
12+
}
13+
}

src/Handlers/ShellMessagesHandler.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,61 @@
44
namespace Litipk\JupyterPHP\Handlers;
55

66

7+
use Litipk\JupyterPHP\Actions\ExecuteAction;
8+
use Litipk\JupyterPHP\Actions\HistoryAction;
9+
use Litipk\JupyterPHP\Actions\KernelInfoAction;
10+
use Litipk\JupyterPHP\Actions\ShutdownAction;
11+
use Litipk\JupyterPHP\JupyterBroker;
12+
13+
use React\ZMQ\SocketWrapper;
14+
15+
716
final class ShellMessagesHandler
817
{
9-
public function __invoke($msg)
18+
/** @var ExecuteAction */
19+
private $executeAction;
20+
21+
/** @var HistoryAction */
22+
private $historyAction;
23+
24+
/** @var KernelInfoAction */
25+
private $kernelInfoAction;
26+
27+
/** @var ShutdownAction */
28+
private $shutdownAction;
29+
30+
/**
31+
* ShellMessagesHandler constructor.
32+
* @param JupyterBroker $broker
33+
* @param SocketWrapper $iopubSocket
34+
* @param SocketWrapper $shellSocket
35+
*/
36+
public function __construct(JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket)
1037
{
11-
list($zmqId, $delim, $hmac, $header, $parent_header, $metadata, $content) = $msg;
38+
$this->executeAction = new ExecuteAction($broker, $iopubSocket, $shellSocket);
39+
$this->historyAction = new HistoryAction($broker, $shellSocket);
40+
$this->kernelInfoAction = new KernelInfoAction($broker, $shellSocket);
41+
$this->shutdownAction = new ShutdownAction($broker, $shellSocket);
42+
}
43+
44+
/**
45+
* @param $msg
46+
*/
47+
public function __invoke(array $msg)
48+
{
49+
list($zmqId, $delim, $hmac, $header, $parentHeader, $metadata, $content) = $msg;
1250

1351
$header = json_decode($header);
1452
$content = json_decode($content);
1553

1654
if ('kernel_info_request' === $header->msg_type) {
17-
55+
$this->kernelInfoAction->call($header, $content);
1856
} elseif ('execute_request' === $header->msg_type) {
19-
57+
$this->executeAction->call($header, $content);
2058
} elseif ('history_request' === $header->msg_type) {
21-
59+
$this->historyAction->call($header, $content);
2260
} elseif ('shutdown_request' === $header->msg_type) {
23-
61+
$this->shutdownAction->call($header, $content);
2462
} else {
2563
// TODO: Add logger!
2664
}

src/KernelCore.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
final class KernelCore
2222
{
2323
/** @var JupyterBroker */
24-
private $jupyterBroker;
24+
private $broker;
2525

2626
/** @var \React\EventLoop\ExtEventLoop|\React\EventLoop\LibEventLoop|\React\EventLoop\LibEvLoop|\React\EventLoop\StreamSelectLoop */
2727
private $reactLoop;
@@ -44,7 +44,7 @@ final class KernelCore
4444
*/
4545
public function __construct(JupyterBroker $jupyterBroker, array $connUris)
4646
{
47-
$this->jupyterBroker = $jupyterBroker;
47+
$this->broker = $jupyterBroker;
4848

4949
$this->initSockets($connUris);
5050
$this->registerHandlers();
@@ -93,6 +93,9 @@ private function registerHandlers()
9393
$this->hbSocket->on('error', new HbErrorHandler());
9494
$this->hbSocket->on('messages', new HbMessagesHandler());
9595
$this->iopubSocket->on('messages', new IOPubMessagesHandler());
96-
$this->shellSocket->on('messages', new ShellMessagesHandler());
96+
$this->shellSocket->on(
97+
'messages',
98+
new ShellMessagesHandler($this->broker, $this->iopubSocket, $this->shellSocket)
99+
);
97100
}
98101
}

0 commit comments

Comments
 (0)