-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·582 lines (488 loc) · 17.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Explorando o Poder dos Middlewares</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/night.css">
<link rel="stylesheet" href="css/custom.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Explorando o Poder dos <span class="highlight">Middlewares</span></h1>
</section>
<section>
<h2>Daniel Gimenes</h2>
<ul>
<li>7+ anos trabalhando com PHP</li>
<li>2+ anos usando middlewares</li>
<li>Engenheiro de Software na Maestrooo</li>
<li>Cursando Sistemas de Informação na UNISAL</li>
<li>Zend Framework <span class="red">❤</span></li>
</ul>
<p><a href="http://twitter.com/danizord">@Danizord</a></p>
</section>
<section>
<h2>Problemas</h2>
</section>
<section>
<ul>
<li>Tratamento de erros</li>
<li>Roteamento</li>
<li>Autenticação</li>
<li>Controle de acesso</li>
<li>Prevenção contra ataques</li>
<li>Validação de entrada</li>
<li>Tratamento de saída</li>
<li>Controle de sessão</li>
<li>Cache HTTP</li>
<li>Log de acessos</li>
<li>Negociação de conteúdo</li>
<li>...</li>
</ul>
</section>
<section>
<h2>Reutilização de código!</h2>
</section>
<section>
<h2>Protocolo HTTP</h2>
<ul class="fragment">
<li>Requisição e resposta</li>
<li class="fragment">Camada comum</li>
</ul>
</section>
<section>
<h2>Reutilização de código <span class="highlight">na camada HTTP</span></h2>
</section>
<section>
<h2>HTTP Request no PHP</h2>
<ul>
<li>$_GET</li>
<li>$_POST</li>
<li>$_SERVER</li>
<li>$_FILES</li>
<li>$_COOKIES</li>
<li>fopen('php://input');</li>
</ul>
</section>
<section>
<h2>HTTP Response no PHP</h2>
<pre><code class="hljs php" data-trim>
header('HTTP/1.1 404 Not Found');
header('Content-type: text/html');
echo 'Not found';
</code></pre>
</section>
<section>
<h2>Problemas</h2>
<ul class="fragment">
<li>Estado global</li>
<li>Efeitos colaterais</li>
<li>Dificil de debugar</li>
<li>Dificil de testar</li>
</ul>
</section>
<section>
<h2>Abstrações</h2>
<ul class="fragment">
<li>Aura\Http</li>
<li>Illuminate\Http</li>
<li>React\Http</li>
<li>Respect\Rest</li>
<li>Symfony\HttpFoundation</li>
<li>Zend\Http</li>
<li>...</li>
</ul>
</section>
<section>
<h2>Reutilização de código <span class="highlight">na camada HTTP</span></h2>
</section>
<section data-background="white">
<img src="image/lambda-middleware.png" class="plain">
</section>
<section>
<h2>HttpKernelInterface</h2>
<pre><code class="hljs php" data-trim>
namespace Symfony\Component\HttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
interface HttpKernelInterface
{
public function handle(Request $request): Response;
}
</code></pre>
</section>
<section>
<h2>Filosofia <span class="highlight">Unix</span></h2>
</section>
<section data-theme="white">
<h2>Middleware</h2>
<img src="image/onion.png" class="fragment">
</section>
<section>
<h2>Seus problemas <span class="highlight">acabaram!</span></h2>
<img src="image/seu-creysson.jpg">
</section>
<section>
<img src="image/stackphp.png" class="plain">
<ol>
<li>Implementar a HttpKernelInterface</li>
<li>Receber o próximo pelo construtor</li>
<li>Chamar o próximo</li>
</ol>
<p><a href="http://stackphp.com/">http://stackphp.com/</a></p>
</section>
<section>
<h2>Interoperabilidade?</h2>
</section>
<section>
<h2>PSR-7</h2>
<ul class="fragment">
<li>Psr\Http\Message\MessageInterface</li>
<li>Psr\Http\Message\RequestInterface</li>
<li><span class="highlight">Psr\Http\Message\ServerRequestInterface</span></li>
<li><span class="highlight">Psr\Http\Message\ResponseInterface</span></li>
<li>Psr\Http\Message\StreamInterface</li>
<li>Psr\Http\Message\UriInterface</li>
<li>Psr\Http\Message\UploadedFileInterface</li>
</ul>
</section>
<section>
<h2>Assinaturas de middlewares</h2>
<pre class="fragment"><code class="hljs php" data-trim>
function (ServerRequestInterface $request): ResponseInterface
</code></pre>
<pre class="fragment"><code class="hljs php" data-trim>
function (
ServerRequestInterface $request,
ResponseInterface $response
): ResponseInterface
</code></pre>
<pre class="fragment"><code class="hljs php" data-trim>
function (
ServerRequestInterface $request,
ResponseInterface $response,
callable $next
): ResponseInterface
</code></pre>
</section>
<section>
<h2>PSR-15</h2>
</section>
<section>
<pre><code class="hljs php" data-trim>
namespace Psr\Http\ServerMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
DelegateInterface $delegate
): ResponseInterface;
}
</code></pre>
<pre><code class="hljs php" data-trim>
namespace Psr\Http\ServerMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface DelegateInterface
{
public function process(
ServerRequestInterface $request
): ResponseInterface;
}
</code></pre>
</section>
<section>
<h2>Por que single-pass?</h2>
<pre><code class="hljs php" data-trim>
$a = function ($request, $response, $next) {
$response = $response->withHeader('X-Foo', 'foo');
return $next(
$request->withAttribute('yay', 'woo'),
$response
);
};
$b = function ($request, $response, $next) {
$yay = $request->getAttribute('yay'); // woo
return new Response();
}
$a(new Request(), new Response(), $b); // fail :(
</code></pre>
<a href="https://gist.github.com/asgrim/0d8b1cb7a4fa362db8fed06444d104ae">
https://gist.github.com/asgrim/0d8b1cb7a4fa362db8fed06444d104ae
</a>
</section>
<section>
<h1>Agora <span class="highlight">sim!</span></h1>
</section>
<section>
<h2>Zend Expressive <span class="highlight">2</span></h2>
</section>
<section>
<h2>Bootstrap</h2>
<pre><code class="hljs php" data-trim>
require __DIR__ . '/../vendor/autoload.php';
$container = require __DIR__ . '/../config/container.php';
$app = $container->get(Application::class);
$app->pipe(ErrorHandler::class);
$app->pipe(Session::class);
$app->pipe(Authentication::class);
$app->pipeRoutingMiddleware();
$app->pipe(Authorization::class);
$app->pipe(InputValidation::class);
$app->pipeDispatchMiddleware();
$app->pipe(NotFoundHandler::class);
$app->run();
</code></pre>
</section>
<section>
<h2>Imutabilidade</h2>
<pre class="fragment"><code class="hljs php" data-trim>
// ...Bootstrap $app
$before = serialize($app);
$app->run();
$after = serialize($app);
$this->assertSame($before, $after);
</code></pre>
</section>
<section>
<h2>Eliminando o overhead com <span class="highlight">FastCGI</span></h2>
<pre><code class="hljs php" data-trim>
use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
use PHPFastCGI\Adapter\Expressive\ApplicationWrapper;
// ...Bootstrap $app
// Create the kernel for FastCGIDaemon (from Expressive app)
$kernel = new ApplicationWrapper($app);
// Create the symfony console application
$daemon = (new ApplicationFactory)->createApplication($kernel);
// Run the symfony console application
$daemon->run();
</code></pre>
<a href="https://github.com/PHPFastCGI/ExpressiveAdapter">https://github.com/PHPFastCGI/ExpressiveAdapter</a>
</section>
<section>
<h2>Tratamento de erros</h2>
<pre><code class="hljs php" data-trim>
class ErrorHandler implements MiddlewareInterface
{
public function process($request, $delegate)
{
try {
return $delegate->process($request);
} catch (Throwable $e) {
return $this->createErrorResponse($request, $e);
}
}
}
</code></pre>
<a href="https://zendframework.github.io/zend-expressive/features/error-handling/" class="fragment">
https://zendframework.github.io/zend-expressive/features/error-handling/
</a>
</section>
<section>
<h2>Sessão</h2>
<pre><code class="hljs php" data-trim>
class Session implements MiddlewareInterface
{
public function process($request, $delegate)
{
$session = Session::fromRequest($request);
$response = $delegate->process(
$request->withAttribute('session', $session)
);
return $session->writeToResponse($response);
}
}
</code></pre>
<a href="https://github.com/psr7-sessions/storageless" class="fragment">
https://github.com/psr7-sessions/storageless
</a>
</section>
<section>
<h2>OAuth 2</h2>
<pre><code class="hljs php" data-trim>
class OAuth implements MiddlewareInterface
{
public function process($request, $delegate)
{
$user = $this->oAuthService->authenticate($request);
return $delegate->process(
$request->withAttribute('user', $user)
);
}
}
</code></pre>
<a href="https://github.com/zf-fr/zfr-oauth2-server" class="fragment">
https://github.com/zf-fr/zfr-oauth2-server
</a>
</section>
<section>
<h2>Roteamento</h2>
<pre><code class="hljs php" data-trim>
class Router implements MiddlewareInterface
{
public function process($request, $delegate)
{
$matchedRoute = $this->router->match($request);
return $delegate->process(
$request->withAttribute('route', $matchedRoute)
);
}
}
</code></pre>
<a href="https://zendframework.github.io/zend-expressive/features/router/intro/" class="fragment">
https://zendframework.github.io/zend-expressive/features/router/intro/
</a>
</section>
<section>
<h2>Autorização</h2>
<pre><code class="hljs php" data-trim>
class Authorization implements MiddlewareInterface
{
public function process($request, $delegate)
{
$user = $request->getAttribtue('user');
$route = $request->getAttribtue('route');
$isAuthorized = $this->isAuthorized($user, $route);
if ($isAuthorized) {
return $delegate->process($request);
}
return $this->createUnauthorizedResponse($user, $route);
}
}
</code></pre>
<a href="https://github.com/ZF-Commons/zfc-rbac" class="fragment">
https://github.com/ZF-Commons/zfc-rbac
</a>
</section>
<section>
<h2>Decodificação de JSON</h2>
<pre><code class="hljs php" data-trim>
class JsonParser implements MiddlewareInterface
{
public function process($request, $delegate)
{
$body = (string) $request->getBody();
$data = json_decode($body, true);
return $delegate->process(
$request->withParsedBody($data)
);
}
}
</code></pre>
<a href="https://zendframework.github.io/zend-expressive/features/helpers/body-parse/" class="fragment">
https://zendframework.github.io/zend-expressive/features/helpers/body-parse/
</a>
</section>
<section>
<h2>Validação de entrada</h2>
<pre><code class="hljs php" data-trim>
class JsonSchemaValidator implements MiddlewareInterface
{
public function process($request, $delegate)
{
$validator = new JsonSchema\Validator();
$data = $request->getParsedBody();
$validator->validate($data, '/my/schema.json');
if ($validator->isValid()) {
return $delegate->process($request);
}
return $this->createErrorResponse($validator);
}
</code></pre>
<a href="https://github.com/oscarotero/psr7-middlewares#jsonschema" class="fragment">
https://github.com/oscarotero/psr7-middlewares#jsonschema
</a>
</section>
<section>
<h2>Dispatch</h2>
<pre><code class="hljs php" data-trim>
class DispatchMiddleware implements MiddlewareInterface
{
public function process($request, $delegate)
{
$routeResult = $request->getAttribute(RouteResult::class);
if (null === $routeResult) {
return $delegate->process($request);
}
$middleware = $routeResult->getMatchedMiddleware();
return $middleware->process($request, $delegate);
}
}
</code></pre>
<a href="https://zendframework.github.io/zend-expressive/features/router/intro/" class="fragment">
https://zendframework.github.io/zend-expressive/features/router/intro/
</a>
</section>
<section>
<h2>Aplicação</h2>
</section>
<section>
<h2>Not Found</h2>
<pre><code class="hljs php" data-trim>
class NotFoundHandler implements MiddlewareInterface
{
public function process($request, $delegate)
{
return new Response(404);
}
}
</code></pre>
<a href="https://zendframework.github.io/zend-expressive/cookbook/custom-404-page-handling/" class="fragment">
https://zendframework.github.io/zend-expressive/cookbook/custom-404-page-handling/
</a>
</section>
<section>
<h2>Outros projetos interessantes</h2>
<ul>
<li class="fragment">https://github.com/oscarotero/psr7-middlewares</li>
<li class="fragment">https://github.com/middlewares/awesome-psr15-middlewares</li>
</ul>
</section>
<section>
<h1>Perguntas?</h1>
</section>
<section>
<h3>Obrigado!</h2>
<p>
Daniel Gimenes<br>
Twitter: <a href="http://twitter.com/danizord">@Danizord</a><br>
Github: <a href="http://github.com/danizord">@Danizord</a>
</p>
<p>Feedback: <a href="https://joind.in/talk/1bca0 ">https://joind.in/talk/1bca0</a></p>
<img src="image/joindin-qr-code.png">
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>