Passed
Pull Request — master (#38)
by giu
04:13
created

Application::bootstrap()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13
 * @link      https://cakephp.org CakePHP(tm) Project
14
 * @since     3.3.0
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 */
17
namespace App;
18
19
use Cake\Core\Configure;
20
use Cake\Core\Exception\MissingPluginException;
21
use Cake\Error\Middleware\ErrorHandlerMiddleware;
22
use Cake\Http\BaseApplication;
23
use Cake\Http\Middleware\BodyParserMiddleware;
24
use Cake\Http\Middleware\CsrfProtectionMiddleware;
25
use Cake\Http\MiddlewareQueue;
26
use Cake\Routing\Middleware\AssetMiddleware;
27
use Cake\Routing\Middleware\RoutingMiddleware;
28
29
/**
30
 * Application setup class.
31
 *
32
 * This defines the bootstrapping logic and middleware layers you
33
 * want to use in your application.
34
 */
35
class Application extends BaseApplication
36
{
37
    /**
38
     * Load all the application configuration and bootstrap logic.
39
     *
40
     * @return void
41
     */
42 19
    public function bootstrap(): void
43
    {
44
        // Call parent to load bootstrap from files.
45 19
        parent::bootstrap();
46
47 19
        if (PHP_SAPI === 'cli') {
48 19
            $this->bootstrapCli();
49
        }
50
51
        /*
52
         * Only try to load DebugKit in development mode
53
         * Debug Kit should not be installed on a production system
54
         */
55 18
        if (Configure::read('debug')) {
56 17
            $this->addPlugin('DebugKit');
57
        }
58
59
        // Load more plugins here
60 18
    }
61
62
    /**
63
     * Setup the middleware queue your application will use.
64
     *
65
     * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
66
     * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
67
     */
68 18
    public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
69
    {
70
        $middlewareQueue
71
            // Catch any exceptions in the lower layers,
72
            // and make an error page/response
73 18
            ->add(new ErrorHandlerMiddleware(Configure::read('Error')))
74
75
            // Handle plugin/theme assets like CakePHP normally does.
76 18
            ->add(new AssetMiddleware([
77 18
                'cacheTime' => Configure::read('Asset.cacheTime'),
78
            ]))
79
80
            // Add routing middleware.
81
            // If you have a large number of routes connected, turning on routes
82
            // caching in production could improve performance. For that when
83
            // creating the middleware instance specify the cache config name by
84
            // using it's second constructor argument:
85
            // `new RoutingMiddleware($this, '_cake_routes_')`
86 18
            ->add(new RoutingMiddleware($this))
87
88
            // Parse various types of encoded request bodies so that they are
89
            // available as array through $request->getData()
90
            // https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware
91 18
            ->add(new BodyParserMiddleware())
92
93
            // Cross Site Request Forgery (CSRF) Protection Middleware
94
            // https://book.cakephp.org/4/en/controllers/middleware.html#cross-site-request-forgery-csrf-middleware
95 18
            ->add(new CsrfProtectionMiddleware([
96 18
                'httponly' => true,
97
            ]));
98
99 18
        return $middlewareQueue;
100
    }
101
102
    /**
103
     * Bootrapping for CLI application.
104
     *
105
     * That is when running commands.
106
     *
107
     * @return void
108
     */
109 19
    protected function bootstrapCli(): void
110
    {
111
        try {
112 19
            $this->addPlugin('Bake');
113 1
        } catch (MissingPluginException $e) {
114
            // Do not halt if the plugin is missing
115
        }
116
117 18
        $this->addPlugin('Migrations');
118
119
        // Load more plugins here
120 18
    }
121
}
122