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

Commit 5641800

Browse files
committed
A lot of incomplete changes
1 parent 20f0065 commit 5641800

File tree

9 files changed

+199
-26
lines changed

9 files changed

+199
-26
lines changed

src/Actions/ExecuteAction.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
use Litipk\JupyterPHP\JupyterBroker;
8+
use Psy\Shell;
89
use React\ZMQ\SocketWrapper;
910

1011

@@ -19,18 +20,25 @@ final class ExecuteAction implements Action
1920
/** @var SocketWrapper */
2021
private $shellSocket;
2122

23+
/** @var Shell */
24+
private $shellSoul;
25+
2226

2327
/**
2428
* ExecuteAction constructor.
2529
* @param JupyterBroker $broker
2630
* @param SocketWrapper $iopubSocket
2731
* @param SocketWrapper $shellSocket
32+
* @param Shell $shellSoul
2833
*/
29-
public function __construct(JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket)
34+
public function __construct(
35+
JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket, Shell $shellSoul
36+
)
3037
{
3138
$this->broker = $broker;
3239
$this->iopubSocket = $iopubSocket;
3340
$this->shellSocket = $shellSocket;
41+
$this->shellSoul = $shellSoul;
3442
}
3543

3644
public function call(array $header, array $content)
@@ -41,6 +49,9 @@ public function call(array $header, array $content)
4149

4250
$execCount = isset($content->execution_count) ? $content->execution_count : 0;
4351

52+
$this->shellSoul->addCode($content['code']);
53+
$this->shellSoul->flushCode();
54+
4455
// TODO: Here is where PsySH goes
4556
$vars_before = get_defined_vars();
4657
ob_start();
@@ -55,7 +66,7 @@ public function call(array $header, array $content)
5566
$this->broker->send(
5667
$this->iopubSocket,
5768
'execute_result',
58-
['execution_count' => $execCount + 1, 'data' => $result, 'metadata' => []],
69+
['execution_count' => $execCount + 1, 'data' => $result, 'metadata' => new S],
5970
$header
6071
);
6172
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'idle'], $header);

src/Actions/KernelInfoAction.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,61 @@
44
namespace Litipk\JupyterPHP\Actions;
55

66

7+
use Litipk\JupyterPHP\JupyterBroker;
8+
use React\ZMQ\SocketWrapper;
9+
10+
711
final class KernelInfoAction implements Action
812
{
13+
/** @var JupyterBroker */
14+
private $broker;
15+
16+
/** @var SocketWrapper */
17+
private $shellSocket;
18+
19+
/** @var SocketWrapper */
20+
private $iopubSocket;
21+
22+
/**
23+
* ExecuteAction constructor.
24+
* @param JupyterBroker $broker
25+
* @param SocketWrapper $shellSocket
26+
* @param SocketWrapper $iopubSocket
27+
*/
28+
public function __construct(JupyterBroker $broker, SocketWrapper $shellSocket, SocketWrapper $iopubSocket)
29+
{
30+
$this->broker = $broker;
31+
$this->shellSocket = $shellSocket;
32+
$this->iopubSocket = $iopubSocket;
33+
}
34+
935
public function call(array $header, array $content)
1036
{
1137
// TODO: Implement call() method.
38+
39+
$this->broker->send(
40+
$this->iopubSocket, 'status', ['execution_state' => 'busy'], $header
41+
);
42+
43+
$this->broker->send(
44+
$this->shellSocket,
45+
'kernel_info_reply',
46+
[
47+
'protocol_version' => '5.0.0',
48+
'implementation' => 'jupyter-php',
49+
'implementation_version' => '0.1.0',
50+
'banner' => 'Jupyter-PHP Kernel',
51+
'language' => 'PHP',
52+
'language_version' => phpversion(),
53+
'language_info' => [
54+
'name' => 'PHP',
55+
'version' => phpversion(),
56+
'mimetype' => 'text/x-php',
57+
'file_extension' => '.php'
58+
]
59+
]
60+
);
61+
62+
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'idle'], $header);
1263
}
1364
}

src/Handlers/HbErrorHandler.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
namespace Litipk\JupyterPHP\Handlers;
44

55

6+
use Monolog\Logger;
7+
8+
69
class HbErrorHandler
710
{
11+
/** @var Logger */
12+
private $logger;
13+
14+
public function __construct(Logger $logger)
15+
{
16+
$this->logger = $logger;
17+
}
18+
819
public function __invoke($e)
920
{
21+
$this->logger->debug('Received message', ['processId' => getmypid(), 'error' => $e]);
1022
}
1123
}

src/Handlers/HbMessagesHandler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
namespace Litipk\JupyterPHP\Handlers;
55

66

7+
use Monolog\Logger;
8+
9+
710
final class HbMessagesHandler
811
{
9-
public function __invoke($msg)
12+
/** @var Logger */
13+
private $logger;
14+
15+
public function __construct(Logger $logger)
1016
{
17+
$this->logger = $logger;
18+
}
1119

20+
public function __invoke($msg)
21+
{
22+
$this->logger->debug('Received message', ['processId' => getmypid(), 'msg' => $msg]);
1223
}
1324
}

src/Handlers/IOPubMessagesHandler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
namespace Litipk\JupyterPHP\Handlers;
55

66

7+
use Monolog\Logger;
8+
9+
710
final class IOPubMessagesHandler
811
{
12+
/** @var Logger */
13+
private $logger;
14+
15+
public function __construct(Logger $logger)
16+
{
17+
$this->logger = $logger;
18+
}
19+
920
public function __invoke($msg)
1021
{
11-
22+
$this->logger->debug('Received message', ['processId' => getmypid(), 'msg' => $msg]);
1223
}
1324
}

src/Handlers/ShellMessagesHandler.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Litipk\JupyterPHP\Actions\ShutdownAction;
1111
use Litipk\JupyterPHP\JupyterBroker;
1212

13+
use Monolog\Logger;
14+
use Psy\Shell;
1315
use React\ZMQ\SocketWrapper;
1416

1517

@@ -27,18 +29,35 @@ final class ShellMessagesHandler
2729
/** @var ShutdownAction */
2830
private $shutdownAction;
2931

32+
/** @var Shell */
33+
private $shellSoul;
34+
35+
/** @var Logger */
36+
private $logger;
37+
3038
/**
3139
* ShellMessagesHandler constructor.
3240
* @param JupyterBroker $broker
3341
* @param SocketWrapper $iopubSocket
3442
* @param SocketWrapper $shellSocket
43+
* @param Shell $shellSoul
44+
* @param Logger $logger
3545
*/
36-
public function __construct(JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket)
46+
public function __construct(
47+
JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket, Shell $shellSoul, Logger $logger
48+
)
3749
{
38-
$this->executeAction = new ExecuteAction($broker, $iopubSocket, $shellSocket);
50+
$this->executeAction = new ExecuteAction($broker, $iopubSocket, $shellSocket, $shellSoul);
3951
$this->historyAction = new HistoryAction($broker, $shellSocket);
40-
$this->kernelInfoAction = new KernelInfoAction($broker, $shellSocket);
52+
$this->kernelInfoAction = new KernelInfoAction($broker, $shellSocket, $iopubSocket);
4153
$this->shutdownAction = new ShutdownAction($broker, $shellSocket);
54+
55+
$this->shellSoul = $shellSoul;
56+
$this->logger = $logger;
57+
58+
$broker->send(
59+
$iopubSocket, 'status', ['execution_state' => 'starting'], []
60+
);
4261
}
4362

4463
/**
@@ -51,6 +70,17 @@ public function __invoke(array $msg)
5170
$header = json_decode($header, true);
5271
$content = json_decode($content, true);
5372

73+
$this->logger->debug('Received message', [
74+
'processId' => getmypid(),
75+
'zmqId' => $zmqId,
76+
'delim' => $delim,
77+
'hmac' => $hmac,
78+
'header' => $header,
79+
'parentHeader' => $parentHeader,
80+
'metadata' => $metadata,
81+
'content' => $content
82+
]);
83+
5484
if ('kernel_info_request' === $header['msg_type']) {
5585
$this->kernelInfoAction->call($header, $content);
5686
} elseif ('execute_request' === $header['msg_type']) {
@@ -60,7 +90,7 @@ public function __invoke(array $msg)
6090
} elseif ('shutdown_request' === $header['msg_type']) {
6191
$this->shutdownAction->call($header, $content);
6292
} else {
63-
// TODO: Add logger!
93+
$this->logger->error('Unknown message type', ['processId' => getmypid(), 'header' => $header]);
6494
}
6595
}
6696
}

src/JupyterBroker.php

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

66

7+
use Psr\Log\LoggerInterface;
78
use Ramsey\Uuid\Uuid;
89
use Ramsey\Uuid\UuidInterface;
910
use React\ZMQ\SocketWrapper;
@@ -23,18 +24,23 @@ final class JupyterBroker
2324
/** @var UuidInterface */
2425
private $sesssionId;
2526

27+
/** @var null|LoggerInterface */
28+
private $logger;
29+
2630
/**
2731
* JupyterBroker constructor.
2832
* @param string $key
2933
* @param string $signatureScheme
3034
* @param UuidInterface $sessionId
35+
* @param null|LoggerInterface $logger
3136
*/
32-
public function __construct($key, $signatureScheme, UuidInterface $sessionId)
37+
public function __construct($key, $signatureScheme, UuidInterface $sessionId, LoggerInterface $logger = null)
3338
{
3439
$this->key = $key;
3540
$this->signatureScheme = $signatureScheme;
3641
$this->hashAlgorithm = preg_split('/-/', $signatureScheme)[1];
3742
$this->sesssionId = $sessionId;
43+
$this->logger = $logger;
3844
}
3945

4046
/**
@@ -51,16 +57,22 @@ public function send(
5157
$header = $this->createHeader($msgType);
5258

5359
$msgDef = [
54-
json_encode(empty($header) ? new \StdClass : $header),
55-
json_encode(empty($parentHeader) ? new \StdClass : $parentHeader),
56-
json_encode(empty($metadata) ? new \StdClass : $metadata),
57-
json_encode(empty($content) ? new \StdClass : $content),
60+
json_encode(empty($header) ? new \stdClass : $header),
61+
json_encode(empty($parentHeader) ? new \stdClass : $parentHeader),
62+
json_encode(empty($metadata) ? new \stdClass : $metadata),
63+
json_encode(empty($content) ? new \stdClass : $content),
5864
];
5965

60-
$stream->send(array_merge(
66+
$finalMsg = array_merge(
6167
['<IDS|MSG>', $this->sign($msgDef)],
6268
$msgDef
63-
));
69+
);
70+
71+
if (null !== $this->logger) {
72+
$this->logger->debug('Sent message', ['processId' => posix_getpid(), 'message/' => $finalMsg]);
73+
}
74+
75+
$stream->send($finalMsg);
6476
}
6577

6678
/**

src/KernelCore.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Litipk\JupyterPHP\Handlers\IOPubMessagesHandler;
1010
use Litipk\JupyterPHP\Handlers\ShellMessagesHandler;
1111

12+
use Monolog\Logger;
13+
use Psy\Shell;
1214
use React\EventLoop\Factory as ReactFactory;
1315
use React\ZMQ\Context as ReactZmqContext;
1416
use React\ZMQ\SocketWrapper;
@@ -26,6 +28,9 @@ final class KernelCore
2628
/** @var \React\EventLoop\ExtEventLoop|\React\EventLoop\LibEventLoop|\React\EventLoop\LibEvLoop|\React\EventLoop\StreamSelectLoop */
2729
private $reactLoop;
2830

31+
/** @var Logger */
32+
private $logger;
33+
2934
/** @var SocketWrapper|\ZMQSocket */
3035
private $hbSocket;
3136
/** @var SocketWrapper|\ZMQSocket */
@@ -37,14 +42,21 @@ final class KernelCore
3742
/** @var SocketWrapper|\ZMQSocket */
3843
private $shellSocket;
3944

45+
/** @var Shell */
46+
private $shellSoul;
47+
4048
/**
4149
* KernelCore constructor.
4250
* @param JupyterBroker $jupyterBroker
4351
* @param array $connUris [string]string $connUris
52+
* @param Logger $logger
4453
*/
45-
public function __construct(JupyterBroker $jupyterBroker, array $connUris)
54+
public function __construct(JupyterBroker $jupyterBroker, array $connUris, Logger $logger)
4655
{
4756
$this->broker = $jupyterBroker;
57+
$this->logger = $logger;
58+
59+
$this->shellSoul = new Shell();
4860

4961
$this->initSockets($connUris);
5062
$this->registerHandlers();
@@ -90,12 +102,27 @@ private function initSockets(array $connUris)
90102
*/
91103
private function registerHandlers()
92104
{
93-
$this->hbSocket->on('error', new HbErrorHandler());
94-
$this->hbSocket->on('messages', new HbMessagesHandler());
95-
$this->iopubSocket->on('messages', new IOPubMessagesHandler());
105+
$this->hbSocket->on(
106+
'error',
107+
new HbErrorHandler($this->logger->withName('HbErrorHandler'))
108+
);
109+
$this->hbSocket->on(
110+
'messages',
111+
new HbMessagesHandler($this->logger->withName('HbMessagesHandler'))
112+
);
113+
$this->iopubSocket->on(
114+
'messages',
115+
new IOPubMessagesHandler($this->logger->withName('IOPubMessagesHandler'))
116+
);
96117
$this->shellSocket->on(
97118
'messages',
98-
new ShellMessagesHandler($this->broker, $this->iopubSocket, $this->shellSocket)
119+
new ShellMessagesHandler(
120+
$this->broker,
121+
$this->iopubSocket,
122+
$this->shellSocket,
123+
$this->shellSoul,
124+
$this->logger->withName('ShellMessagesHandler')
125+
)
99126
);
100127
}
101128
}

0 commit comments

Comments
 (0)