3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * Synchronous line-oriented communication session.
18 * $session = new class {
19 * use LineSessionTrait;
20 * protected function onRequest(string $requestLine): ?string {
23 * protected function onException(string $requestLine, \Throwable $t): ?string {
27 * $session->setIO(STDIN, STDOUT)->run();
30 trait LineSessionTrait
{
33 * The onConnect() method is called when a new session is opened.
36 * Header/welcome line, or NULL if none.
38 protected function onConnect(): ?
string {
43 * The onRequest() method is called after receiving one line of text.
45 * @param string $requestLine
46 * The received line of text.
48 * The line to send back, or NULL if none.
50 abstract protected function onRequest(string $requestLine): ?
string;
53 * The onRequest() method is called after receiving one line of text.
55 * @param string $requestLine
56 * The received line of text - which led to the unhandled exception.
57 * @param \Throwable $t
58 * The unhandled exception.
60 * The line to send back, or NULL if none.
62 abstract protected function onException(string $requestLine, \Throwable
$t): ?
string;
81 protected $delimiter = "\n";
84 * Maximum size of the buffer for reading lines.
86 * Clients may need to set this if they submit large requests.
90 protected $bufferSize = 524288;
93 * A value to display immediately before the response lines.
95 * Clients may set this is if they want to detect and skip buggy noise.
100 protected $responsePrefix = NULL;
103 * @param resource|null $input
104 * @param resource|null $output
106 public function __construct($input = NULL, $output = NULL) {
107 $this->input
= $input;
108 $this->output
= $output;
112 * Run the main loop. Poll for commands on $input and write responses to $output.
114 public function run() {
115 $this->write($this->onConnect());
117 while (FALSE !== ($line = stream_get_line($this->input
, $this->bufferSize
, $this->delimiter
))) {
118 $line = rtrim($line, $this->delimiter
);
124 $response = $this->onRequest($line);
126 catch (\Throwable
$t) {
127 $response = $this->onException($line, $t);
129 $this->write($response);
136 public function getBufferSize(): int {
137 return $this->bufferSize
;
141 * @param int $bufferSize
144 public function setBufferSize(int $bufferSize) {
145 $this->bufferSize
= $bufferSize;
150 * @return string|null
152 public function getResponsePrefix(): ?
string {
153 return $this->responsePrefix
;
157 * @param string|null $responsePrefix
160 public function setResponsePrefix(?
string $responsePrefix) {
161 $this->responsePrefix
= $responsePrefix;
166 * @param resource $input
167 * @param resource $output
170 public function setIO($input, $output) {
171 $this->input
= $input;
172 $this->output
= $output;
176 protected function write(?
string $response): void
{
177 if ($response === NULL) {
180 if ($this->responsePrefix
!== NULL) {
181 fwrite($this->output
, $this->responsePrefix
);
183 fwrite($this->output
, $response);
184 fwrite($this->output
, $this->delimiter
);