Skip to content

Commit 544a056

Browse files
committed
Added request bypassing section, updates configuration, request input data, routing, uploaded files section
1 parent 2b63dcc commit 544a056

File tree

5 files changed

+182
-8
lines changed

5 files changed

+182
-8
lines changed

Contents/docs/icehawk/configuration.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,70 @@ Please see our [routing section](@baseUrl@/docs/icehawk/routing.html) for more d
244244

245245
<hr class="blockspace">
246246

247+
## Configure request bypasses
248+
249+
This example shows a simple request bypass:
250+
251+
```php
252+
<?php declare(strict_types=1);
253+
254+
namespace YourVendor\YourProject;
255+
256+
use IceHawk\IceHawk\Interfaces\ConfiguresIceHawk;
257+
use IceHawk\IceHawk\Interfaces\RoutesToWriteHandler;
258+
use IceHawk\IceHawk\Routing\WriteRoute;
259+
use IceHawk\IceHawk\Routing\RequestBypass;
260+
use IceHawk\IceHawk\Constants\HttpMethod;
261+
use IceHawk\IceHawk\Routing\Patterns\Literal;
262+
use IceHawk\IceHawk\Routing\Patterns\RegExp;
263+
use IceHawk\IceHawk\Routing\Patterns\NamedRegExp;
264+
use IceHawk\IceHawk\Defaults;
265+
266+
class IceHawkConfig implements ConfiguresIceHawk
267+
{
268+
use Defaults\Traits\DefaultRequestProviding;
269+
use Defaults\Traits\DefaultReadRouting;
270+
use Defaults\Traits\DefaultEventSubscribing;
271+
use Defaults\Traits\DefaultCookieProviding;
272+
use Defaults\Traits\DefaultFinalReadResponding;
273+
use Defaults\Traits\DefaultFinalWriteResponding;
274+
275+
/**
276+
* @return array|\Traversable|BypassesRequest[]
277+
*/
278+
public function getRequestBypasses()
279+
{
280+
return [
281+
new RequestBypass(
282+
new Literal('/payment/success'),
283+
'/payment/mark-succeeded',
284+
HttpMethod::POST
285+
),
286+
];
287+
}
288+
289+
/**
290+
* @return array|\Traversable|RoutesToWriteHandler[]
291+
*/
292+
public function getWriteRoutes()
293+
{
294+
return [
295+
new WriteRoute(
296+
new Literal('/payment/mark-succeeded'),
297+
new MarkPaymentSucceededRequestHandler()
298+
),
299+
];
300+
}
301+
}
302+
```
303+
304+
As you may noticed, we omitted the return type for this method, because we wanted to allow building a `\Generator` and `yield`ing the routes.
305+
In the next major release targeting PHP 7.1 we'll add the [iterable](https://github.com/php/php-src/pull/1941) return type.
306+
307+
Please see our [request bypassing section](@baseUrl@/docs/icehawk/request-bypassing.html) for more details.
308+
309+
<hr class="blockspace">
310+
247311
## Configure event subscribers
248312

249313
IceHawk publishes some events you can subscribe to. The following example shows how to register such event subscribers:
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Request bypassing
2+
3+
One of IceHawk's main goals is to help you keep your application divided into a read and a write side. But when interacting with external systems, like e.g.
4+
external payment providers, you probably be confronted with a situation where you receive a GET request that should trigger a write operation in your
5+
application. Since a GET request is bound to the read side this would go against the previous mentioned goal.
6+
7+
In order to process such requests properly on the write side of your application the IceHawk component offers "request bypassing" to internally
8+
re-route such a request (without HTTP redirection) to the write side of your application.
9+
10+
To make that behaviour a conscious decision a separate configuration is needed.
11+
12+
This section shows how to set up request bypassing.
13+
14+
---
15+
16+
## Configure a request bypass
17+
18+
As you can see in the [configuration section](@baseUrl@/docs/icehawk/configuration.html) there is a method called `getRequestBypasses()`, which returns an empty array by default:
19+
20+
```php
21+
/**
22+
* @return array|\Traversable|BypassesRequest[]
23+
*/
24+
public function getRequestBypasses()
25+
{
26+
return [];
27+
}
28+
```
29+
30+
Assuming the following use-case:
31+
32+
* Your application receives a
33+
```http
34+
HTTP/1.1 GET: /payment/success?token=12345
35+
```
36+
* Your application shall process this request like
37+
```http
38+
HTTP/1.1 POST: /payment/mark-succeeded
39+
token=12345
40+
```
41+
42+
The configuration could look like this:
43+
44+
```php
45+
<?php declare(strict_types=1);
46+
47+
namespace YourVendor\YourProject;
48+
49+
use IceHawk\IceHawk\Interfaces\ConfiguresIceHawk;
50+
use IceHawk\IceHawk\Interfaces\RoutesToWriteHandler;
51+
use IceHawk\IceHawk\Interfaces\BypassesRequest;
52+
use IceHawk\IceHawk\Routing\WriteRoute;
53+
use IceHawk\IceHawk\Routing\RequestBypass;
54+
use IceHawk\IceHawk\Routing\Patterns\Literal;
55+
use IceHawk\IceHawk\Defaults;
56+
57+
class IceHawkConfig implements ConfiguresIceHawk
58+
{
59+
use Defaults\Traits\DefaultRequestProviding;
60+
use Defaults\Traits\DefaultReadRouting;
61+
use Defaults\Traits\DefaultEventSubscribing;
62+
use Defaults\Traits\DefaultCookieProviding;
63+
use Defaults\Traits\DefaultFinalReadResponding;
64+
use Defaults\Traits\DefaultFinalWriteResponding;
65+
66+
/**
67+
* @return array|\Traversable|BypassesRequest[]
68+
*/
69+
public function getRequestBypasses()
70+
{
71+
return [
72+
new RequestBypass(
73+
new Literal('/payment/success'),
74+
'/payment/mark-succeeded',
75+
HttpMethod::POST
76+
),
77+
];
78+
}
79+
80+
/**
81+
* @return array|\Traversable|RoutesToWriteHandler[]
82+
*/
83+
public function getWriteRoutes()
84+
{
85+
return [
86+
new WriteRoute(
87+
new Literal('/payment/mark-succeeded'),
88+
new MarkPaymentSucceededRequestHandler()
89+
),
90+
];
91+
}
92+
}
93+
```
94+
95+
**Please note:** The `MarkPaymentSucceededRequestHandler` has to implement the `IceHawk\IceHawk\Interfaces\HandlesPostRequest` interface.
96+
97+
Of course you can use any of the [URI pattern classes](@baseUrl@/docs/icehawk/routing.html#uri-pattern-classes) for matching in the request bypass.

Contents/docs/icehawk/request-input-data.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,28 @@ var_dump( $notUploadedFile );
410410
This prints:
411411

412412
```php
413+
# $uploadedFile0
413414
object(IceHawk\IceHawk\Requests\UploadedFile)[17]
414415
private 'name' => string '...' (length=24)
415416
private 'tmpName' => string '...' (length=66)
416417
private 'error' => int 0
417418
private 'size' => int 40506
418419
private 'type' => string 'image/png' (length=9)
419420

421+
# $uploadedFile4
420422
object(IceHawk\IceHawk\Requests\UploadedFile)[19]
421423
private 'name' => string '...' (length=24)
422424
private 'tmpName' => string '...' (length=66)
423425
private 'error' => int 0
424426
private 'size' => int 40506
425427
private 'type' => string 'image/png' (length=9)
426428

427-
NULL
429+
# $notUploadedFile
430+
# empty UploadedFile object with error UPLOAD_ERR_NO_FILE
431+
object(IceHawk\IceHawk\Requests\UploadedFile)[19]
432+
private 'name' => string '' (length=0)
433+
private 'tmpName' => string '' (length=0)
434+
private 'error' => int 4
435+
private 'size' => int 0
436+
private 'type' => string '' (length=0)
428437
```

Contents/docs/icehawk/routing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ The advantage of this approach is that not all routes, patterns and request hand
207207

208208
<hr class="blockspace">
209209

210-
## What patterns to use for matching?
210+
## What patterns to use for matching? <a name="uri-pattern-classes"></a>
211211

212212
As already mentioned above, the IceHawk component provides 3 ready-to-use pattern classes. We'd like to show you examples for each of them.
213213

Contents/docs/icehawk/uploaded-files.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ var_dump( $notUploadedFile );
263263
This prints:
264264

265265
```php
266+
# $uploadedFile0
266267
# file at default index 0
267268
object(IceHawk\IceHawk\Requests\UploadedFile)[17]
268269
private 'name' => string 'branding-circle.png' (length=19)
@@ -271,6 +272,7 @@ object(IceHawk\IceHawk\Requests\UploadedFile)[17]
271272
private 'size' => int 14273
272273
private 'type' => string 'image/png' (length=9)
273274

275+
# $uploadedFile4
274276
# file at a string index "file4"
275277
object(IceHawk\IceHawk\Requests\UploadedFile)[19]
276278
private 'name' => string 'branding-circle.png' (length=19)
@@ -279,14 +281,16 @@ object(IceHawk\IceHawk\Requests\UploadedFile)[19]
279281
private 'size' => int 14273
280282
private 'type' => string 'image/png' (length=9)
281283

282-
# not existing file
283-
NULL
284+
# $notUploadedFile
285+
# not existing file, empty UploadedFile object with error UPLOAD_ERR_NO_FILE
286+
object(IceHawk\IceHawk\Requests\UploadedFile)[19]
287+
private 'name' => string '' (length=0)
288+
private 'tmpName' => string '' (length=0)
289+
private 'error' => int 4
290+
private 'size' => int 0
291+
private 'type' => string '' (length=0)
284292
```
285293

286-
**Please note:**
287-
[We will change the return value for not-existing files in `v2.1.0` to an empty `UploadedFile` object with the error code `UPLOAD_ERR_NO_FILE`.](https://github.com/icehawk/icehawk/issues/22)
288-
And will then add a return type declaration to the `getOneFile()` method.
289-
290294
<hr class="blockspace">
291295

292296
## The UploadedFile object

0 commit comments

Comments
 (0)