Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

Application   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A bootstrap() 0 45 3
A middleware() 0 31 1
A loadDefaultThemePlugin() 0 13 3
A bootstrapCli() 0 10 2
1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11
 * @link      https://cakephp.org CakePHP(tm) Project
12
 * @since     3.3.0
13
 * @license   https://opensource.org/licenses/mit-license.php MIT License
14
 */
15
namespace App;
16
17
use App\Middleware\SaitoBootstrapMiddleware;
18
use Cake\Core\Configure;
19
use Cake\Core\Exception\MissingPluginException;
20
use Cake\Core\Plugin;
21
use Cake\Error\Middleware\ErrorHandlerMiddleware;
22
use Cake\Event\EventManagerInterface;
23
use Cake\Http\BaseApplication;
24
use Cake\Http\Middleware\EncryptedCookieMiddleware;
25
use Cake\Http\Middleware\SecurityHeadersMiddleware;
26
use Cake\Routing\Middleware\AssetMiddleware;
27
use Cake\Routing\Middleware\RoutingMiddleware;
28
use Cron\Lib\Cron;
29
use Saito\App\Registry;
30
use Stopwatch\Lib\Stopwatch;
31
32
/**
33
 * Application setup class.
34
 *
35
 * This defines the bootstrapping logic and middleware layers you
36
 * want to use in your application.
37
 */
38
class Application extends BaseApplication
39
{
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function __construct($configDir, EventManagerInterface $eventManager = null)
44
    {
45
        Stopwatch::init();
46
        Stopwatch::enable();
47
        Stopwatch::start('Application::__construct');
48
        parent::__construct($configDir, $eventManager);
49
        Stopwatch::stop('Application::__construct');
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function bootstrap()
56
    {
57
        Stopwatch::start('Application::bootstrap');
58
59
        parent::bootstrap();
60
61
        if (PHP_SAPI === 'cli') {
62
            $this->bootstrapCli();
63
        }
64
        /*
65
         * Only try to load DebugKit in development mode
66
         * Debug Kit should not be installed on a production system
67
         */
68
        if (Configure::read('debug')) {
69
            // $this->addPlugin(\DebugKit\Plugin::class);
70
        }
71
        // Load more plugins here
72
73
        Registry::initialize();
74
75
        $this->addPlugin(\Admin\Plugin::class, ['routes' => true]);
76
        $this->addPlugin(\Api\Plugin::class, ['bootstrap' => true, 'routes' => true]);
77
        $this->addPlugin(\Bookmarks\Plugin::class, ['routes' => true]);
78
        $this->addPlugin(\BbcodeParser\Plugin::class);
79
        $this->addPlugin(\Feeds\Plugin::class, ['routes' => true]);
80
        $this->addPlugin(\Installer\Plugin::class);
81
        $this->addPlugin(\SaitoHelp\Plugin::class, ['routes' => true]);
82
        $this->addPlugin(\SaitoSearch\Plugin::class, ['routes' => true]);
83
        $this->addPlugin(\Sitemap\Plugin::class, ['bootstrap' => true, 'routes' => true]);
84
        $this->addPlugin(\ImageUploader\Plugin::class, ['routes' => true]);
85
86
        $this->addPlugin(\Cron\Plugin::class);
87
        $this->addPlugin(\Commonmark\Plugin::class);
88
        $this->addPlugin(\Detectors\Plugin::class);
89
        $this->addPlugin(\MailObfuscator\Plugin::class);
90
        $this->addPlugin(\SpectrumColorpicker\Plugin::class);
91
        $this->addPlugin(\Stopwatch\Plugin::class);
92
93
        $this->addPlugin('ADmad/JwtAuth');
94
        $this->addPlugin('Proffer');
95
96
        $this->loadDefaultThemePlugin();
97
98
        Stopwatch::stop('Application::bootstrap');
99
    }
100
101
    /**
102
     * Setup the middleware queue your application will use.
103
     *
104
     * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
105
     * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
106
     */
107
    public function middleware($middlewareQueue)
108
    {
109
        $middlewareQueue
110
            // Catch any exceptions in the lower layers,
111
            // and make an error page/response
112
            ->add(ErrorHandlerMiddleware::class)
113
114
            // Handle plugin/theme assets like CakePHP normally does.
115
            ->add(AssetMiddleware::class)
116
117
            // Add routing middleware.
118
            // Routes collection cache enabled by default, to disable route caching
119
            // pass null as cacheConfig, example: `new RoutingMiddleware($this)`
120
            // you might want to disable this cache in case your routing is extremely simple
121
            ->add(new RoutingMiddleware($this, '_cake_routes_'));
122
123
        $cookies = new EncryptedCookieMiddleware(
124
            // Names of cookies to protect
125
            [Configure::read('Security.cookieAuthName')],
126
            Configure::read('Security.cookieSalt')
127
        );
128
        $middlewareQueue->add($cookies);
129
130
        $middlewareQueue->insertAfter(RoutingMiddleware::class, new SaitoBootstrapMiddleware());
131
132
        $security = (new SecurityHeadersMiddleware())
133
            ->setXFrameOptions(strtolower(Configure::read('Saito.X-Frame-Options')));
134
        $middlewareQueue->add($security);
135
136
        return $middlewareQueue;
137
    }
138
139
    /**
140
     * Load the plugin for Saito's default theme
141
     *
142
     * @return void
143
     */
144
    private function loadDefaultThemePlugin()
145
    {
146
        $defaultTheme = Configure::read('Saito.themes.default');
147
        if (empty($defaultTheme)) {
148
            throw new \RuntimeException(
149
                'Could not resolve default theme for plugin loading.',
150
                1556562215
151
            );
152
        }
153
        if (Plugin::isLoaded($defaultTheme) !== true) {
154
            $this->addPlugin($defaultTheme);
155
        }
156
    }
157
158
    /**
159
     * @return void
160
     */
161
    protected function bootstrapCli()
162
    {
163
        try {
164
            $this->addPlugin('Bake');
165
        } catch (MissingPluginException $e) {
166
            // Do not halt if the plugin is missing
167
        }
168
        $this->addPlugin('Migrations');
169
        // Load more plugins here
170
    }
171
}
172