Plugin::middleware()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 6
c 2
b 1
f 1
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2022 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\DevTools;
17
18
use BEdita\DevTools\Middleware\HtmlMiddleware;
19
use Cake\Core\BasePlugin;
20
use Cake\Core\Configure;
21
use Cake\Core\PluginApplicationInterface;
22
use Cake\Http\MiddlewareQueue;
23
use Cake\Http\ServerRequest;
24
use Cake\Routing\Middleware\AssetMiddleware;
25
26
/**
27
 * Plugin class.
28
 */
29
class Plugin extends BasePlugin
30
{
31
    /**
32
     * @inheritDoc
33
     */
34
    public function bootstrap(PluginApplicationInterface $app): void
35
    {
36
        parent::bootstrap($app);
37
38
        ServerRequest::addDetector('html', [
39
            'accept' => ['text/html', 'application/xhtml+xml', 'application/xhtml', 'text/xhtml'],
40
        ]);
41
42
        // Configure DebugKit panels.
43
        $panels = ['BEdita/DevTools.Configuration'];
44
        $panels = array_merge((array)Configure::read('DebugKit.panels', []), $panels);
45
        Configure::write('DebugKit.panels', $panels);
46
        Configure::write('DebugKit.ignoreAuthorization', true);
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function middleware($middleware): MiddlewareQueue
53
    {
54
        $middleware = parent::middleware($middleware);
55
        if (!Configure::read('Accept.html', false)) {
56
            return $middleware;
57
        }
58
59
        return $middleware
60
            ->prepend(new HtmlMiddleware())
61
            ->prepend(new AssetMiddleware());
62
    }
63
}
64