Rename plugins to libs and update all file references

This commit is contained in:
johnnyq
2026-07-10 13:24:20 -04:00
parent 7ba1571400
commit 8da3a107fb
3410 changed files with 140 additions and 140 deletions

View File

@@ -0,0 +1,232 @@
<?php
namespace DirectoryTree\ImapEngine\Connection\Streams;
use PHPUnit\Framework\Assert;
use RuntimeException;
class FakeStream implements StreamInterface
{
/**
* Lines queued for testing; each call to fgets() pops the next line.
*
* @var string[]
*/
protected array $buffer = [];
/**
* Data that has been "written" to this fake stream (for assertion).
*
* @var string[]
*/
protected array $written = [];
/**
* The connection info.
*/
protected ?array $connection = null;
/**
* The mock meta info.
*/
protected array $meta = [
'crypto' => [
'protocol' => '',
'cipher_name' => '',
'cipher_bits' => 0,
'cipher_version' => '',
],
'mode' => 'c',
'eof' => false,
'blocked' => false,
'timed_out' => false,
'seekable' => false,
'unread_bytes' => 0,
'stream_type' => 'tcp_socket/unknown',
];
/**
* Feed a line to the stream buffer with a newline character.
*/
public function feed(array|string $lines): self
{
// We'll ensure that each line ends with a CRLF,
// as this is the expected behavior of every
// reply that comes from an IMAP server.
$lines = array_map(fn (string $line) => (
rtrim($line, "\r\n")."\r\n"
), (array) $lines);
array_push($this->buffer, ...$lines);
return $this;
}
/**
* Feed a raw line to the stream buffer.
*/
public function feedRaw(array|string $lines): self
{
array_push($this->buffer, ...(array) $lines);
return $this;
}
/**
* Set the timed out status.
*/
public function setMeta(string $attribute, mixed $value): self
{
if (! isset($this->meta[$attribute])) {
throw new RuntimeException(
"Unknown metadata attribute: {$attribute}"
);
}
if (gettype($this->meta[$attribute]) !== gettype($value)) {
throw new RuntimeException(
"Metadata attribute {$attribute} must be of type ".gettype($this->meta[$attribute])
);
}
$this->meta[$attribute] = $value;
return $this;
}
/**
* {@inheritDoc}
*/
public function open(?string $transport = null, ?string $host = null, ?int $port = null, ?int $timeout = null, array $options = []): bool
{
$this->connection = compact('transport', 'host', 'port', 'timeout', 'options');
return true;
}
/**
* {@inheritDoc}
*/
public function close(): void
{
$this->buffer = [];
$this->connection = null;
}
/**
* {@inheritDoc}
*/
public function read(int $length): string|false
{
if (! $this->opened()) {
return false;
}
if ($this->meta['eof'] && empty($this->buffer)) {
return false; // EOF and no data left. Indicate end of stream.
}
$data = implode('', $this->buffer);
$availableLength = strlen($data);
if ($availableLength === 0) {
// No data available right now (but not EOF).
// Simulate non-blocking behavior.
return '';
}
$bytesToRead = min($length, $availableLength);
$result = substr($data, 0, $bytesToRead);
$remainingData = substr($data, $bytesToRead);
$this->buffer = $remainingData !== '' ? [$remainingData] : [];
return $result;
}
/**
* {@inheritDoc}
*/
public function fgets(): string|false
{
if (! $this->opened()) {
return false;
}
// Simulate timeout/eof checks.
if ($this->meta['timed_out'] || $this->meta['eof']) {
return false;
}
return array_shift($this->buffer) ?? false;
}
/**
* {@inheritDoc}
*/
public function fwrite(string $data): int|false
{
if (! $this->opened()) {
return false;
}
$this->written[] = $data;
return strlen($data);
}
/**
* {@inheritDoc}
*/
public function meta(): array
{
return $this->meta;
}
/**
* {@inheritDoc}
*/
public function opened(): bool
{
return (bool) $this->connection;
}
/**
* {@inheritDoc}
*/
public function setTimeout(int $seconds): bool
{
return true;
}
/**
* {@inheritDoc}
*/
public function setSocketSetCrypto(bool $enabled, ?int $method): bool|int
{
return true;
}
/**
* Assert that the given data was written to the stream.
*/
public function assertWritten(string $string): void
{
$found = false;
foreach ($this->written as $index => $written) {
if (str_contains($written, $string)) {
unset($this->written[$index]);
$found = true;
break;
}
}
Assert::assertTrue($found, "Failed asserting that the string '{$string}' was written to the stream.");
}
}

View File

@@ -0,0 +1,120 @@
<?php
namespace DirectoryTree\ImapEngine\Connection\Streams;
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionFailedException;
class ImapStream implements StreamInterface
{
/**
* The underlying PHP stream resource.
*
* @var resource|null
*/
protected mixed $stream = null;
/**
* {@inheritDoc}
*/
public function open(string $transport, string $host, int $port, int $timeout, array $options = []): bool
{
$this->stream = @stream_socket_client(
$address = "{$transport}://{$host}:{$port}",
$errno,
$errstr,
$timeout,
STREAM_CLIENT_CONNECT,
stream_context_create($options)
);
if (! $this->stream) {
throw new ImapConnectionFailedException("Unable to connect to {$address} ({$errstr})", $errno);
}
return true;
}
/**
* {@inheritDoc}
*/
public function close(): void
{
if ($this->opened()) {
fclose($this->stream);
}
$this->stream = null;
}
/**
* {@inheritDoc}
*/
public function read(int $length): string|false
{
if (! $this->opened()) {
return false;
}
$data = '';
while (strlen($data) < $length && ! feof($this->stream)) {
$chunk = fread($this->stream, $length - strlen($data));
if ($chunk === false) {
return false;
}
$data .= $chunk;
}
return $data;
}
/**
* {@inheritDoc}
*/
public function fgets(): string|false
{
return $this->opened() ? fgets($this->stream) : false;
}
/**
* {@inheritDoc}
*/
public function fwrite(string $data): int|false
{
return $this->opened() ? fwrite($this->stream, $data) : false;
}
/**
* {@inheritDoc}
*/
public function meta(): array
{
return $this->opened() ? stream_get_meta_data($this->stream) : [];
}
/**
* {@inheritDoc}
*/
public function opened(): bool
{
return is_resource($this->stream);
}
/**
* {@inheritDoc}
*/
public function setTimeout(int $seconds): bool
{
return stream_set_timeout($this->stream, $seconds);
}
/**
* {@inheritDoc}
*/
public function setSocketSetCrypto(bool $enabled, ?int $method): bool|int
{
return stream_socket_enable_crypto($this->stream, $enabled, $method);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace DirectoryTree\ImapEngine\Connection\Streams;
interface StreamInterface
{
/**
* Open the underlying stream.
*/
public function open(string $transport, string $host, int $port, int $timeout, array $options = []): bool;
/**
* Close the underlying stream.
*/
public function close(): void;
/**
* Read data from the stream.
*/
public function read(int $length): string|false;
/**
* Read a single line from the stream.
*/
public function fgets(): string|false;
/**
* Write data to the stream.
*/
public function fwrite(string $data): int|false;
/**
* Return meta info (like stream_get_meta_data).
*/
public function meta(): array;
/**
* Determine if the stream is open.
*/
public function opened(): bool;
/**
* Set the timeout on the stream.
*/
public function setTimeout(int $seconds): bool;
/**
* Set encryption state on an already connected socked.
*/
public function setSocketSetCrypto(bool $enabled, ?int $method): bool|int;
}