Skip to content

Commit 50c06f1

Browse files
committed
WIP
1 parent a92efe5 commit 50c06f1

File tree

5 files changed

+53
-69
lines changed

5 files changed

+53
-69
lines changed

src/Connection/Connection.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function __construct(StreamInterface $stream = new ImapStream)
6969
public function __destruct()
7070
{
7171
$this->logout();
72-
$this->close();
7372
}
7473

7574
/**
@@ -343,14 +342,14 @@ public function nextReply(): Response
343342
/**
344343
* Send an IMAP command.
345344
*/
346-
public function send(string $name, array|string $tokens = [], ?string &$tag = null): void
345+
public function send(string $name, array $tokens = [], ?string &$tag = null): void
347346
{
348347
if (! $tag) {
349348
$this->sequence++;
350349
$tag = 'TAG'.$this->sequence;
351350
}
352351

353-
$command = new ImapCommand($name, $tag, (array) $tokens);
352+
$command = new ImapCommand($tag, $name, $tokens);
354353

355354
// After every command, we'll overwrite any previous result
356355
// with the new command and its responses, so that we can
@@ -394,12 +393,4 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, Im
394393

395394
return $this->result->responses()->untagged();
396395
}
397-
398-
/**
399-
* Escape a list of literals.
400-
*/
401-
protected function escapeList(array $list): string
402-
{
403-
return Str::list($list);
404-
}
405396
}

src/Connection/FakeStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function feed(array|string $lines): self
5656
// as this is the expected behavior of the IMAP a stream.
5757
$lines = array_map(fn (string $line) => (
5858
rtrim($line, "\r\n")."\r\n"
59-
), Arr::wrap($lines));
59+
), (array) $lines);
6060

61-
array_push($this->buffer, ...(array) $lines);
61+
array_push($this->buffer, ...$lines);
6262

6363
return $this;
6464
}

src/Connection/ImapConnection.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use DirectoryTree\ImapEngine\Exceptions\ConnectionFailedException;
1212
use DirectoryTree\ImapEngine\Exceptions\Exception;
1313
use DirectoryTree\ImapEngine\Support\Str;
14-
use Illuminate\Support\Arr;
1514

1615
class ImapConnection extends Connection
1716
{
@@ -69,8 +68,6 @@ public function logout(): ?TaggedResponse
6968

7069
try {
7170
$this->send('LOGOUT', tag: $tag);
72-
73-
return $this->nextTaggedResponse($tag);
7471
} catch (Exception) {
7572
// Do nothing.
7673
}
@@ -101,7 +98,7 @@ public function examine(string $folder = 'INBOX'): ResponseCollection
10198
*/
10299
protected function examineOrSelect(string $command = 'EXAMINE', string $folder = 'INBOX'): ResponseCollection
103100
{
104-
$this->send($command, Str::literal($folder), $tag);
101+
$this->send($command, [Str::literal($folder)], $tag);
105102

106103
$this->assertTaggedResponse($tag);
107104

@@ -130,7 +127,7 @@ public function status(string $folder = 'INBOX', array $arguments = ['MESSAGES',
130127
*/
131128
public function create(string $folder): ResponseCollection
132129
{
133-
$this->send('CREATE', Str::literal($folder), $tag);
130+
$this->send('CREATE', [Str::literal($folder)], $tag);
134131

135132
$this->assertTaggedResponse($tag);
136133

@@ -144,7 +141,7 @@ public function create(string $folder): ResponseCollection
144141
*/
145142
public function delete(string $folder): TaggedResponse
146143
{
147-
$this->send('DELETE', Str::literal($folder), tag: $tag);
144+
$this->send('DELETE', [Str::literal($folder)], tag: $tag);
148145

149146
return $this->assertTaggedResponse($tag);
150147
}
@@ -164,7 +161,7 @@ public function rename(string $oldPath, string $newPath): TaggedResponse
164161
*/
165162
public function subscribe(string $folder): TaggedResponse
166163
{
167-
$this->send('SUBSCRIBE', Str::literal($folder), tag: $tag);
164+
$this->send('SUBSCRIBE', [Str::literal($folder)], tag: $tag);
168165

169166
return $this->assertTaggedResponse($tag);
170167
}
@@ -174,7 +171,7 @@ public function subscribe(string $folder): TaggedResponse
174171
*/
175172
public function unsubscribe(string $folder): TaggedResponse
176173
{
177-
$this->send('UNSUBSCRIBE', Str::literal($folder), tag: $tag);
174+
$this->send('UNSUBSCRIBE', [Str::literal($folder)], tag: $tag);
178175

179176
return $this->assertTaggedResponse($tag);
180177
}
@@ -203,7 +200,7 @@ public function append(string $folder, string $message, ?array $flags = null, ?s
203200
$tokens[] = Str::literal($folder);
204201

205202
if ($flags) {
206-
$tokens[] = $this->escapeList($flags);
203+
$tokens[] = Str::list($flags);
207204
}
208205

209206
if ($date) {
@@ -254,7 +251,7 @@ public function store(array|string $flags, array|int $from, ?int $to = null, ?st
254251
{
255252
$set = Str::set($from, $to);
256253

257-
$flags = $this->escapeList(Arr::wrap($flags));
254+
$flags = Str::list((array) $flags);
258255

259256
$item = ($mode == '-' ? '-' : '+').(is_null($item) ? 'FLAGS' : $item).($silent ? '.SILENT' : '');
260257

src/Support/Str.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,46 @@
44

55
class Str
66
{
7+
/**
8+
* Make a list with literals or nested lists.
9+
*/
10+
public static function list(array $list): string
11+
{
12+
$values = [];
13+
14+
foreach ($list as $value) {
15+
if (is_array($value)) {
16+
$values[] = static::list($value);
17+
} else {
18+
$values[] = $value;
19+
}
20+
}
21+
22+
return sprintf('(%s)', implode(' ', $values));
23+
}
24+
25+
/**
26+
* Make one or more literals.
27+
*/
28+
public static function literal(array|string $string): array|string
29+
{
30+
if (is_array($string)) {
31+
$result = [];
32+
33+
foreach ($string as $value) {
34+
$result[] = static::literal($value);
35+
}
36+
37+
return $result;
38+
}
39+
40+
if (str_contains($string, "\n")) {
41+
return ['{'.strlen($string).'}', $string];
42+
}
43+
44+
return '"'.static::escape($string).'"';
45+
}
46+
747
/**
848
* Make a range set for use in a search command.
949
*/
@@ -39,44 +79,4 @@ public static function escape(string $string): string
3979
// Escape backslashes first to avoid double-escaping and then escape double quotes.
4080
return str_replace(['\\', '"'], ['\\\\', '\\"'], $string);
4181
}
42-
43-
/**
44-
* Make one or more literals.
45-
*/
46-
public static function literal(array|string $string): array|string
47-
{
48-
if (func_num_args() >= 2) {
49-
$result = [];
50-
51-
foreach (func_get_args() as $string) {
52-
$result[] = static::literal($string);
53-
}
54-
55-
return $result;
56-
}
57-
58-
if (str_contains($string, "\n")) {
59-
return ['{'.strlen($string).'}', $string];
60-
}
61-
62-
return '"'.static::escape($string).'"';
63-
}
64-
65-
/**
66-
* Make a list with literals or nested lists.
67-
*/
68-
public static function list(array $list): string
69-
{
70-
$values = [];
71-
72-
foreach ($list as $value) {
73-
if (is_array($value)) {
74-
$values[] = static::list($value);
75-
} else {
76-
$values[] = $value;
77-
}
78-
}
79-
80-
return sprintf('(%s)', implode(' ', $values));
81-
}
8282
}

tests/Support/StrTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@
3434
expect(Str::literal($input))->toBe($expected);
3535
});
3636

37-
test('literal handles multiple arguments by returning an array of literals', function () {
38-
expect(Str::literal('first', 'second'))
39-
->toBe([
40-
'"first"',
41-
'"second"',
42-
]);
37+
test('literal handles an array of literals', function () {
38+
expect(Str::literal(['first', 'second']))->toBe(['"first"', '"second"']);
4339
});
4440

4541
test('list returns a properly formatted parenthesized list for a flat array', function () {

0 commit comments

Comments
 (0)