Skip to content

Commit 86a5ffe

Browse files
committed
Add service class to add all routes to the router.
1 parent 775b53f commit 86a5ffe

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/Service/RouterService.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid\Service;
4+
5+
use League\Container\Container;
6+
use League\Route\Router;
7+
use League\Route\Strategy\ApplicationStrategy;
8+
use Pdsinterop\Solid\Controller\AddSlashToPathController;
9+
use Pdsinterop\Solid\Controller\HelloWorldController;
10+
use Pdsinterop\Solid\Controller\HttpToHttpsController;
11+
use Pdsinterop\Solid\Controller\Profile\CardController;
12+
use Pdsinterop\Solid\Controller\Profile\ProfileController;
13+
14+
class RouterService
15+
{
16+
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
17+
18+
/** @var Container */
19+
private $container;
20+
/** @var Router */
21+
private $router;
22+
23+
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
24+
25+
final public function __construct(Container $container, Router $router)
26+
{
27+
$this->container = $container;
28+
$this->router = $router;
29+
}
30+
31+
final public function populate() : Router
32+
{
33+
$container = $this->container;
34+
$router = $this->router;
35+
36+
/*/ Default output is HTML, routes should return a Response object /*/
37+
$strategy = new ApplicationStrategy();
38+
$strategy->setContainer($container);
39+
$router->setStrategy($strategy);
40+
41+
/*/ Make sure HTTPS is always used in production /*/
42+
$scheme = 'http';
43+
if (getenv('ENVIRONMENT') !== 'development') {
44+
$router->map('GET', '/{page:(?:.|/)*}', HttpToHttpsController::class)->setScheme($scheme);
45+
$scheme = 'https';
46+
}
47+
48+
/*/ Map routes and groups /*/
49+
$router->map('GET', '/', HelloWorldController::class)->setScheme($scheme);
50+
$this->mapProfile($router, $scheme);
51+
52+
return $router;
53+
}
54+
55+
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
56+
57+
private function mapProfile(Router $router, string $scheme) : void
58+
{
59+
$router->map('GET', '/profile', AddSlashToPathController::class)->setScheme($scheme);
60+
$router->map('GET', '/profile/', ProfileController::class)->setScheme($scheme);
61+
$router->map('GET', '/profile/card', CardController::class)->setScheme($scheme);
62+
$router->map('GET', '/profile/card{extension}', CardController::class)->setScheme($scheme);
63+
}
64+
}

0 commit comments

Comments
 (0)