|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Handler; |
| 6 | + |
| 7 | +use Laminas\Diactoros\Response\HtmlResponse; |
| 8 | +use Laminas\Diactoros\Response\JsonResponse; |
| 9 | +use Mezzio\LaminasView\LaminasViewRenderer; |
| 10 | +use Mezzio\Plates\PlatesRenderer; |
| 11 | +use Mezzio\Router; |
| 12 | +use Mezzio\Template\TemplateRendererInterface; |
| 13 | +use Mezzio\Twig\TwigRenderer; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | +use Psr\Http\Message\ServerRequestInterface; |
| 16 | +use Psr\Http\Server\RequestHandlerInterface; |
| 17 | + |
| 18 | +class HomePageHandler implements RequestHandlerInterface |
| 19 | +{ |
| 20 | + /** @var string */ |
| 21 | + private $containerName; |
| 22 | + |
| 23 | + /** @var Router\RouterInterface */ |
| 24 | + private $router; |
| 25 | + |
| 26 | + /** @var null|TemplateRendererInterface */ |
| 27 | + private $template; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + string $containerName, |
| 31 | + Router\RouterInterface $router, |
| 32 | + ?TemplateRendererInterface $template = null |
| 33 | + ) { |
| 34 | + $this->containerName = $containerName; |
| 35 | + $this->router = $router; |
| 36 | + $this->template = $template; |
| 37 | + } |
| 38 | + |
| 39 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
| 40 | + { |
| 41 | + if ($this->template === null) { |
| 42 | + return new JsonResponse([ |
| 43 | + 'welcome' => 'Congratulations! You have installed the mezzio skeleton application.', |
| 44 | + 'docsUrl' => 'https://docs.mezzio.dev/mezzio/', |
| 45 | + ]); |
| 46 | + } |
| 47 | + |
| 48 | + $data = []; |
| 49 | + |
| 50 | + switch ($this->containerName) { |
| 51 | + case 'Aura\Di\Container': |
| 52 | + $data['containerName'] = 'Aura.Di'; |
| 53 | + $data['containerDocs'] = 'http://auraphp.com/packages/2.x/Di.html'; |
| 54 | + break; |
| 55 | + case 'Pimple\Container': |
| 56 | + $data['containerName'] = 'Pimple'; |
| 57 | + $data['containerDocs'] = 'https://pimple.symfony.com/'; |
| 58 | + break; |
| 59 | + case 'Laminas\ServiceManager\ServiceManager': |
| 60 | + $data['containerName'] = 'Laminas Servicemanager'; |
| 61 | + $data['containerDocs'] = 'https://docs.laminas.dev/laminas-servicemanager/'; |
| 62 | + break; |
| 63 | + case 'Auryn\Injector': |
| 64 | + $data['containerName'] = 'Auryn'; |
| 65 | + $data['containerDocs'] = 'https://github.com/rdlowrey/Auryn'; |
| 66 | + break; |
| 67 | + case 'Symfony\Component\DependencyInjection\ContainerBuilder': |
| 68 | + $data['containerName'] = 'Symfony DI Container'; |
| 69 | + $data['containerDocs'] = 'https://symfony.com/doc/current/service_container.html'; |
| 70 | + break; |
| 71 | + case 'Zend\DI\Config\ContainerWrapper': |
| 72 | + case 'DI\Container': |
| 73 | + $data['containerName'] = 'PHP-DI'; |
| 74 | + $data['containerDocs'] = 'http://php-di.org'; |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + if ($this->router instanceof Router\AuraRouter) { |
| 79 | + $data['routerName'] = 'Aura.Router'; |
| 80 | + $data['routerDocs'] = 'http://auraphp.com/packages/2.x/Router.html'; |
| 81 | + } elseif ($this->router instanceof Router\FastRouteRouter) { |
| 82 | + $data['routerName'] = 'FastRoute'; |
| 83 | + $data['routerDocs'] = 'https://github.com/nikic/FastRoute'; |
| 84 | + } elseif ($this->router instanceof Router\LaminasRouter) { |
| 85 | + $data['routerName'] = 'Laminas Router'; |
| 86 | + $data['routerDocs'] = 'https://docs.laminas.dev/laminas-router/'; |
| 87 | + } |
| 88 | + |
| 89 | + if ($this->template instanceof PlatesRenderer) { |
| 90 | + $data['templateName'] = 'Plates'; |
| 91 | + $data['templateDocs'] = 'http://platesphp.com/'; |
| 92 | + } elseif ($this->template instanceof TwigRenderer) { |
| 93 | + $data['templateName'] = 'Twig'; |
| 94 | + $data['templateDocs'] = 'http://twig.sensiolabs.org/documentation'; |
| 95 | + } elseif ($this->template instanceof LaminasViewRenderer) { |
| 96 | + $data['templateName'] = 'Laminas View'; |
| 97 | + $data['templateDocs'] = 'https://docs.laminas.dev/laminas-view/'; |
| 98 | + } |
| 99 | + |
| 100 | + return new HtmlResponse($this->template->render('app::home-page', $data)); |
| 101 | + } |
| 102 | +} |
0 commit comments