Completed
Branch develop (b4cf96)
by Schlaefer
08:59
created

Application::bootstrapCli()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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\Plugin;
20
use Cake\Error\Middleware\ErrorHandlerMiddleware;
21
use Cake\Event\EventManagerInterface;
22
use Cake\Http\BaseApplication;
23
use Cake\Http\Middleware\EncryptedCookieMiddleware;
24
use Cake\Http\Middleware\SecurityHeadersMiddleware;
25
use Cake\Routing\Middleware\AssetMiddleware;
26
use Cake\Routing\Middleware\RoutingMiddleware;
27
use Cron\Lib\Cron;
28
use Saito\App\Registry;
29
use Stopwatch\Lib\Stopwatch;
30
31
/**
32
 * Application setup class.
33
 *
34
 * This defines the bootstrapping logic and middleware layers you
35
 * want to use in your application.
36
 */
37
class Application extends BaseApplication
38
{
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function __construct($configDir, EventManagerInterface $eventManager = null)
43
    {
44
        Stopwatch::init();
45
        Stopwatch::enable();
46
        Stopwatch::start('Application::__construct');
47
        parent::__construct($configDir, $eventManager);
48
        Stopwatch::stop('Application::__construct');
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function bootstrap()
55
    {
56
        Stopwatch::start('Application::bootstrap');
57
58
        parent::bootstrap();
59
60
        if (PHP_SAPI === 'cli') {
61
            $this->bootstrapCli();
62
        }
63
        /*
64
         * Only try to load DebugKit in development mode
65
         * Debug Kit should not be installed on a production system
66
         */
67
        if (Configure::read('debug')) {
68
            // $this->addPlugin(\DebugKit\Plugin::class);
69
        }
70
        // Load more plugins here
71
72
        Registry::initialize();
73
74
        $this->addPlugin(\Admin\Plugin::class, ['routes' => true]);
75
        $this->addPlugin(\Api\Plugin::class, ['bootstrap' => true, 'routes' => true]);
76
        $this->addPlugin(\Bookmarks\Plugin::class, ['routes' => true]);
77
        $this->addPlugin(\BbcodeParser\Plugin::class);
78
        $this->addPlugin(\Feeds\Plugin::class, ['routes' => true]);
79
        $this->addPlugin(\Installer\Plugin::class);
80
        $this->addPlugin(\SaitoHelp\Plugin::class, ['routes' => true]);
81
        $this->addPlugin(\SaitoSearch\Plugin::class, ['routes' => true]);
82
        $this->addPlugin(\Sitemap\Plugin::class, ['bootstrap' => true, 'routes' => true]);
83
        $this->addPlugin(\ImageUploader\Plugin::class, ['routes' => true]);
84
85
        $this->addPlugin(\Cron\Plugin::class);
86
        $this->addPlugin(\Commonmark\Plugin::class);
87
        $this->addPlugin(\Detectors\Plugin::class);
88
        $this->addPlugin(\MailObfuscator\Plugin::class);
89
        $this->addPlugin(\SpectrumColorpicker\Plugin::class);
90
        $this->addPlugin(\Stopwatch\Plugin::class);
91
92
        $this->addPlugin('ADmad/JwtAuth');
93
        $this->addPlugin('Proffer');
94
95
        $this->loadDefaultThemePlugin();
96
97
        Stopwatch::stop('Application::bootstrap');
98
    }
99
100
    /**
101
     * Setup the middleware queue your application will use.
102
     *
103
     * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
104
     * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
105
     */
106
    public function middleware($middlewareQueue)
107
    {
108
        $middlewareQueue
109
            // Catch any exceptions in the lower layers,
110
            // and make an error page/response
111
            ->add(ErrorHandlerMiddleware::class)
112
113
            // Handle plugin/theme assets like CakePHP normally does.
114
            ->add(AssetMiddleware::class)
115
116
            // Add routing middleware.
117
            // Routes collection cache enabled by default, to disable route caching
118
            // pass null as cacheConfig, example: `new RoutingMiddleware($this)`
119
            // you might want to disable this cache in case your routing is extremely simple
120
            ->add(new RoutingMiddleware($this, '_cake_routes_'));
121
122
        $cookies = new EncryptedCookieMiddleware(
123
            // Names of cookies to protect
124
            [Configure::read('Security.cookieAuthName')],
125
            Configure::read('Security.cookieSalt')
126
        );
127
        $middlewareQueue->add($cookies);
128
129
        $middlewareQueue->insertAfter(RoutingMiddleware::class, new SaitoBootstrapMiddleware());
130
131
        $security = (new SecurityHeadersMiddleware())
132
            ->setXFrameOptions(strtolower(Configure::read('Saito.X-Frame-Options')));
133
        $middlewareQueue->add($security);
134
135
        return $middlewareQueue;
136
    }
137
138
    /**
139
     * Load the plugin for Saito's default theme
140
     *
141
     * @return void
142
     */
143
    private function loadDefaultThemePlugin()
144
    {
145
        $defaultTheme = Configure::read('Saito.themes.default');
146
        if (empty($defaultTheme)) {
147
            throw new \RuntimeException(
148
                'Could not resolve default theme for plugin loading.',
149
                1556562215
150
            );
151
        }
152
        if (Plugin::isLoaded($defaultTheme) !== true) {
153
            $this->addPlugin($defaultTheme);
154
        }
155
    }
156
157
    /**
158
     * @return void
159
     */
160
    protected function bootstrapCli()
161
    {
162
        try {
163
            $this->addPlugin('Bake');
164
        } catch (MissingPluginException $e) {
0 ignored issues
show
Bug introduced by
The class App\MissingPluginException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
165
            // Do not halt if the plugin is missing
166
        }
167
        $this->addPlugin('Migrations');
168
        // Load more plugins here
169
    }
170
}
171