1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types=1); |
3
|
|
|
define('APP_DIR', __DIR__); |
4
|
|
|
$_appLang = 'en'; |
5
|
|
|
if (strpos($_SERVER['REQUEST_URI'], '/tr') === 0) { |
6
|
|
|
$_appLang = 'tr'; |
7
|
|
|
} |
8
|
|
|
define('RUNTIME_LANG', $_appLang); |
9
|
|
|
require dirname(__DIR__) . '/bootstrap.php'; |
10
|
|
|
|
11
|
|
|
use Zend\Diactoros\Response; |
12
|
|
|
use Zend\Diactoros\Server; |
13
|
|
|
use Zend\Stratigility\MiddlewarePipe; |
14
|
|
|
use Zend\Diactoros\Uri; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use function Zend\Stratigility\middleware; |
19
|
|
|
use function Zend\Stratigility\path; |
20
|
|
|
use function Zend\Stratigility\doublePassMiddleware; |
21
|
|
|
|
22
|
|
|
use Selami\Middleware\Authorization\Middleware as AuthorizationMiddleware; |
23
|
|
|
use Selami\Middleware\Authentication\Middleware as AuthenticationMiddleware; |
24
|
|
|
use Selami\Middleware\App\Middleware as ApplicationMiddleware; |
25
|
|
|
|
26
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
27
|
|
|
|
28
|
|
|
$container = include SYS_DIR . '/config/container.php'; |
29
|
|
|
|
30
|
|
|
$app = new MiddlewarePipe(); |
31
|
|
|
$server = Server::createServer([$app, 'handle'], $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); |
32
|
|
|
|
33
|
|
|
// Check Authorization |
34
|
|
|
$app->pipe(doublePassMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, $next) use ($container) { |
35
|
|
|
/** |
36
|
|
|
* @var $session Session |
37
|
|
|
*/ |
38
|
|
|
$session = $container->get(Session::class); |
39
|
|
|
$loggedUserId = $session->get('logged_user_id'); |
40
|
|
|
$urlPath = $request->getUri()->getPath(); |
41
|
|
|
if ($loggedUserId === null && strpos($urlPath, '/auth') !== 0) { |
42
|
|
|
return $response->withHeader('Location', '/auth')->withStatus(301); |
43
|
|
|
} |
44
|
|
|
if ($loggedUserId !== null) { |
45
|
|
|
$request = $request->withAttribute('loggedUserId', $loggedUserId); |
46
|
|
|
} |
47
|
|
|
$response = $next($request, $response); |
48
|
|
|
return $response; |
49
|
|
|
}, new Response())); |
50
|
|
|
|
51
|
|
|
// Authentication Middleware |
52
|
|
|
$app->pipe(path('/auth', new AuthenticationMiddleware($container))); |
53
|
|
|
|
54
|
|
|
// APP |
55
|
|
|
$app->pipe(new ApplicationMiddleware($container)); |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$server->listen(); |
62
|
|
|
|
63
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.