質問編集履歴

1

追記

2019/12/29 06:57

投稿

creative_09
creative_09

スコア80

test CHANGED
File without changes
test CHANGED
@@ -235,3 +235,269 @@
235
235
 
236
236
 
237
237
  以上、よろしくおねがいします
238
+
239
+
240
+
241
+ 追記
242
+
243
+ TwigMiddleware.phpの中身です
244
+
245
+ ```ここに言語を入力
246
+
247
+ <?php
248
+
249
+ /**
250
+
251
+ * Slim Framework (http://slimframework.com)
252
+
253
+ *
254
+
255
+ * @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
256
+
257
+ */
258
+
259
+
260
+
261
+ declare(strict_types=1);
262
+
263
+
264
+
265
+ namespace Slim\Views;
266
+
267
+
268
+
269
+ use Psr\Container\ContainerInterface;
270
+
271
+ use Psr\Http\Message\ResponseInterface;
272
+
273
+ use Psr\Http\Message\ServerRequestInterface;
274
+
275
+ use Psr\Http\Server\MiddlewareInterface;
276
+
277
+ use Psr\Http\Server\RequestHandlerInterface;
278
+
279
+ use RuntimeException;
280
+
281
+ use Slim\App;
282
+
283
+ use Slim\Interfaces\RouteParserInterface;
284
+
285
+
286
+
287
+ class TwigMiddleware implements MiddlewareInterface
288
+
289
+ {
290
+
291
+ /**
292
+
293
+ * @var Twig
294
+
295
+ */
296
+
297
+ protected $twig;
298
+
299
+
300
+
301
+ /**
302
+
303
+ * @var RouteParserInterface
304
+
305
+ */
306
+
307
+ protected $routeParser;
308
+
309
+
310
+
311
+ /**
312
+
313
+ * @var string
314
+
315
+ */
316
+
317
+ protected $basePath;
318
+
319
+
320
+
321
+ /**
322
+
323
+ * @var string|null
324
+
325
+ */
326
+
327
+ protected $attributeName;
328
+
329
+
330
+
331
+ /**
332
+
333
+ * @param App $app
334
+
335
+ * @param string $containerKey
336
+
337
+ *
338
+
339
+ * @return TwigMiddleware
340
+
341
+ */
342
+
343
+ public static function createFromContainer(App $app, string $containerKey = 'view'): self
344
+
345
+ {
346
+
347
+ $container = $app->getContainer();
348
+
349
+ if ($container === null) {
350
+
351
+ throw new RuntimeException('The app does not have a container.');
352
+
353
+ }
354
+
355
+ if (!$container->has($containerKey)) {
356
+
357
+ throw new RuntimeException(
358
+
359
+ "The specified container key does not exist: $containerKey"
360
+
361
+ );
362
+
363
+ }
364
+
365
+
366
+
367
+ $twig = $container->get($containerKey);
368
+
369
+ if (!($twig instanceof Twig)) {
370
+
371
+ throw new RuntimeException(
372
+
373
+ "Twig instance could not be resolved via container key: $containerKey"
374
+
375
+ );
376
+
377
+ }
378
+
379
+
380
+
381
+ return new self(
382
+
383
+ $twig,
384
+
385
+ $app->getRouteCollector()->getRouteParser(),
386
+
387
+ $app->getBasePath()
388
+
389
+ );
390
+
391
+ }
392
+
393
+
394
+
395
+ /**
396
+
397
+ * @param App $app
398
+
399
+ * @param Twig $twig
400
+
401
+ * @param string $attributeName
402
+
403
+ *
404
+
405
+ * @return TwigMiddleware
406
+
407
+ */
408
+
409
+ public static function create(App $app, Twig $twig, string $attributeName = 'view'): self
410
+
411
+ {
412
+
413
+ return new self(
414
+
415
+ $twig,
416
+
417
+ $app->getRouteCollector()->getRouteParser(),
418
+
419
+ $app->getBasePath(),
420
+
421
+ $attributeName
422
+
423
+ );
424
+
425
+ }
426
+
427
+
428
+
429
+ /**
430
+
431
+ * @param Twig $twig
432
+
433
+ * @param RouteParserInterface $routeParser
434
+
435
+ * @param string $basePath
436
+
437
+ * @param string|null $attributeName
438
+
439
+ */
440
+
441
+ public function __construct(
442
+
443
+ Twig $twig,
444
+
445
+ RouteParserInterface $routeParser,
446
+
447
+ string $basePath = '',
448
+
449
+ ?string $attributeName = null
450
+
451
+ ) {
452
+
453
+ $this->twig = $twig;
454
+
455
+ $this->routeParser = $routeParser;
456
+
457
+ $this->basePath = $basePath;
458
+
459
+ $this->attributeName = $attributeName;
460
+
461
+ }
462
+
463
+
464
+
465
+ /**
466
+
467
+ * {@inheritdoc}
468
+
469
+ */
470
+
471
+ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
472
+
473
+ {
474
+
475
+ $runtimeLoader = new TwigRuntimeLoader($this->routeParser, $request->getUri(), $this->basePath);
476
+
477
+ $this->twig->addRuntimeLoader($runtimeLoader);
478
+
479
+
480
+
481
+ $extension = new TwigExtension();
482
+
483
+ $this->twig->addExtension($extension);
484
+
485
+
486
+
487
+ if ($this->attributeName !== null) {
488
+
489
+ $request = $request->withAttribute($this->attributeName, $this->twig);
490
+
491
+ }
492
+
493
+
494
+
495
+ return $handler->handle($request);
496
+
497
+ }
498
+
499
+ }
500
+
501
+
502
+
503
+ ```