Skip to content

Commit 80961d4

Browse files
committed
wip
1 parent 8844cec commit 80961d4

File tree

3 files changed

+93
-87
lines changed

3 files changed

+93
-87
lines changed

src/Concerns/HandlesParameters.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Concerns;
4+
5+
trait HandlesParameters
6+
{
7+
public function parameterNames()
8+
{
9+
preg_match_all('/\{(.*?)\}/', $this->pattern, $matches);
10+
11+
return array_map(function ($m) {
12+
return trim($m, '?');
13+
}, $matches[1]);
14+
}
15+
16+
public function parameters()
17+
{
18+
return $this->matchToKeys(array_slice($this->matches, 1));
19+
}
20+
21+
public function parametersWithoutNulls()
22+
{
23+
return array_filter($this->parameters(), function ($p) {
24+
return ! is_null($p);
25+
});
26+
}
27+
28+
protected function matchToKeys(array $matches)
29+
{
30+
if (empty($parameterNames = $this->parameterNames())) {
31+
return [];
32+
}
33+
34+
$parameters = array_intersect_key($matches, array_flip($parameterNames));
35+
36+
return array_filter($parameters, function ($value) {
37+
return is_string($value) && strlen($value) > 0;
38+
});
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Concerns;
4+
5+
use Symfony\Component\Routing\Route;
6+
7+
trait HandlesRegularExpressions
8+
{
9+
protected function matchesRegularExpression(string $subject)
10+
{
11+
return (bool) preg_match($this->getRegularExpression(), $subject, $this->matches);
12+
}
13+
14+
/**
15+
* We do not want to create the regular expression on our own,
16+
* so we just use Symfonys Route for this.
17+
*
18+
* @return string
19+
*/
20+
protected function getRegularExpression(): string
21+
{
22+
$route = new Route($this->pattern);
23+
$route->setRequirements($this->wheres);
24+
25+
$regex = $route->compile()->getRegex();
26+
27+
$regex = preg_replace('/^#\^\/(.*)/', '#^$1', $regex);
28+
29+
$regex = str_replace('>[^/]+)', '>.+)', $regex);
30+
31+
$regex = str_replace('$#sD', '$#sDi', $regex);
32+
33+
return $regex;
34+
}
35+
36+
public function where($name, $expression = null)
37+
{
38+
foreach ($this->parseWhere($name, $expression) as $name => $expression) {
39+
$this->wheres[$name] = $expression;
40+
}
41+
42+
return $this;
43+
}
44+
45+
protected function parseWhere($name, $expression)
46+
{
47+
return is_array($name) ? $name : [$name => $expression];
48+
}
49+
}

src/MailboxRoute.php

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
namespace BeyondCode\Mailbox;
44

5+
use BeyondCode\Mailbox\Concerns\HandlesParameters;
6+
use BeyondCode\Mailbox\Concerns\HandlesRegularExpressions;
57
use Illuminate\Container\Container;
6-
use Illuminate\Routing\ControllerDispatcher;
78
use Illuminate\Routing\RouteDependencyResolverTrait;
89
use Illuminate\Support\Collection;
910
use Illuminate\Support\Str;
1011
use ReflectionFunction;
11-
use Symfony\Component\Routing\Route;
1212
use ZBateson\MailMimeParser\Header\Part\AddressPart;
13-
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
1413

1514
class MailboxRoute
1615
{
16+
use HandlesParameters;
17+
use HandlesRegularExpressions;
1718
use RouteDependencyResolverTrait;
1819

1920
const FROM = 'from';
@@ -64,40 +65,6 @@ public function pattern()
6465
return $this->pattern;
6566
}
6667

67-
public function parameterNames()
68-
{
69-
preg_match_all('/\{(.*?)\}/', $this->pattern, $matches);
70-
71-
return array_map(function ($m) {
72-
return trim($m, '?');
73-
}, $matches[1]);
74-
}
75-
76-
public function parameters()
77-
{
78-
return $this->matchToKeys(array_slice($this->matches, 1));
79-
}
80-
81-
public function parametersWithoutNulls()
82-
{
83-
return array_filter($this->parameters(), function ($p) {
84-
return ! is_null($p);
85-
});
86-
}
87-
88-
protected function matchToKeys(array $matches)
89-
{
90-
if (empty($parameterNames = $this->parameterNames())) {
91-
return [];
92-
}
93-
94-
$parameters = array_intersect_key($matches, array_flip($parameterNames));
95-
96-
return array_filter($parameters, function ($value) {
97-
return is_string($value) && strlen($value) > 0;
98-
});
99-
}
100-
10168
public function matches(InboundEmail $message): bool
10269
{
10370
$subjects = $this->gatherMatchSubjectsFromMessage($message);
@@ -107,33 +74,6 @@ public function matches(InboundEmail $message): bool
10774
}) !== null;
10875
}
10976

110-
protected function matchesRegularExpression(string $subject)
111-
{
112-
return (bool) preg_match($this->getRegularExpression(), $subject, $this->matches);
113-
}
114-
115-
/**
116-
* We do not want to create the regular expression on our own,
117-
* so we just use Symfonys Route for this.
118-
*
119-
* @return string
120-
*/
121-
protected function getRegularExpression(): string
122-
{
123-
$route = new Route($this->pattern);
124-
$route->setRequirements($this->wheres);
125-
126-
$regex = $route->compile()->getRegex();
127-
128-
$regex = preg_replace('/^#\^\/(.*)/', '#^$1', $regex);
129-
130-
$regex = str_replace('>[^/]+)', '>.+)', $regex);
131-
132-
$regex = str_replace('$#sD', '$#sDi', $regex);
133-
134-
return $regex;
135-
}
136-
13777
protected function gatherMatchSubjectsFromMessage(InboundEmail $message)
13878
{
13979
switch ($this->subject) {
@@ -164,20 +104,6 @@ protected function convertMessageAddresses($addresses): array
164104
})->toArray();
165105
}
166106

167-
public function where($name, $expression = null)
168-
{
169-
foreach ($this->parseWhere($name, $expression) as $name => $expression) {
170-
$this->wheres[$name] = $expression;
171-
}
172-
173-
return $this;
174-
}
175-
176-
protected function parseWhere($name, $expression)
177-
{
178-
return is_array($name) ? $name : [$name => $expression];
179-
}
180-
181107
public function run(InboundEmail $email)
182108
{
183109
$this->container = $this->container ?: new Container;
@@ -214,15 +140,6 @@ protected function runCallable(InboundEmail $email)
214140
)));
215141
}
216142

217-
public function mailboxDispatcher()
218-
{
219-
if ($this->container->bound(ControllerDispatcherContract::class)) {
220-
return $this->container->make(ControllerDispatcherContract::class);
221-
}
222-
223-
return new ControllerDispatcher($this->container);
224-
}
225-
226143
public function getMailbox()
227144
{
228145
if (! $this->mailbox) {

0 commit comments

Comments
 (0)