Passed
Push — master ( ecaee8...5e3cc7 )
by Stefano
03:14 queued 23s
created

ProjectMiddleware::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2020 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Middleware;
14
15
use App\Application;
16
use Cake\Http\ServerRequest;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
/**
23
 * Project middleware.
24
 *
25
 * Multi projects support (optional): detect current `_project` name in session and try to load matching config file from `config/projects` folder.
26
 * After that app plugins are loaded via configuration.
27
 */
28
class ProjectMiddleware implements MiddlewareInterface
29
{
30
    /**
31
     * Application instance
32
     *
33
     * @var \App\Application
34
     */
35
    protected $Application;
36
37
    /**
38
     * Projects config base path
39
     *
40
     * @var string
41
     */
42
    protected $projectsConfigPath = CONFIG . 'projects' . DS;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param \App\Application $app The application instance.
48
     * @param string|null $configPath Projects config path.
49
     */
50
    public function __construct(Application $app, ?string $configPath = null)
51
    {
52
        $this->Application = $app;
53
        if (!empty($configPath)) {
54
            $this->projectsConfigPath = $configPath;
55
        }
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
62
    {
63
        $project = $this->detectProject($request);
64
        Application::loadProjectConfig((string)$project, $this->projectsConfigPath);
65
        $this->Application->loadPluginsFromConfig();
66
67
        return $handler->handle($request);
68
    }
69
70
    /**
71
     * Detect project in use from session or request, if any.
72
     * On empty session or request, or missing project name, `null` is returned.
73
     *
74
     * @param \Cake\Http\ServerRequest $request The request.
75
     * @return string|null
76
     */
77
    protected function detectProject(ServerRequest $request): ?string
78
    {
79
        $session = $request->getSession();
80
        if (empty($session) || !$session->check('_project')) {
81
            $project = $request->getData('project');
82
            if (!empty($project)) {
83
                return $project;
84
            }
85
86
            return null;
87
        }
88
89
        return (string)$session->read('_project');
90
    }
91
}
92