2
2
3
3
namespace BeyondCode \Mailbox ;
4
4
5
+ use BeyondCode \Mailbox \Concerns \HandlesParameters ;
6
+ use BeyondCode \Mailbox \Concerns \HandlesRegularExpressions ;
5
7
use Illuminate \Container \Container ;
6
- use Illuminate \Routing \ControllerDispatcher ;
7
8
use Illuminate \Routing \RouteDependencyResolverTrait ;
8
9
use Illuminate \Support \Collection ;
9
10
use Illuminate \Support \Str ;
10
11
use ReflectionFunction ;
11
- use Symfony \Component \Routing \Route ;
12
12
use ZBateson \MailMimeParser \Header \Part \AddressPart ;
13
- use Illuminate \Routing \Contracts \ControllerDispatcher as ControllerDispatcherContract ;
14
13
15
14
class MailboxRoute
16
15
{
16
+ use HandlesParameters;
17
+ use HandlesRegularExpressions;
17
18
use RouteDependencyResolverTrait;
18
19
19
20
const FROM = 'from ' ;
@@ -64,40 +65,6 @@ public function pattern()
64
65
return $ this ->pattern ;
65
66
}
66
67
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
-
101
68
public function matches (InboundEmail $ message ): bool
102
69
{
103
70
$ subjects = $ this ->gatherMatchSubjectsFromMessage ($ message );
@@ -107,33 +74,6 @@ public function matches(InboundEmail $message): bool
107
74
}) !== null ;
108
75
}
109
76
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
-
137
77
protected function gatherMatchSubjectsFromMessage (InboundEmail $ message )
138
78
{
139
79
switch ($ this ->subject ) {
@@ -164,20 +104,6 @@ protected function convertMessageAddresses($addresses): array
164
104
})->toArray ();
165
105
}
166
106
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
-
181
107
public function run (InboundEmail $ email )
182
108
{
183
109
$ this ->container = $ this ->container ?: new Container ;
@@ -214,15 +140,6 @@ protected function runCallable(InboundEmail $email)
214
140
)));
215
141
}
216
142
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
-
226
143
public function getMailbox ()
227
144
{
228
145
if (! $ this ->mailbox ) {
0 commit comments