Passed
Pull Request — master (#10)
by Stefano
02:59
created

BaseApplication::bootstrap()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 19
rs 10
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 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 BEdita\WebTools;
14
15
use BEdita\WebTools\Shell\CacheShell;
16
use Cake\Core\Configure;
17
use Cake\Core\Exception\MissingPluginException;
18
use Cake\Error\Middleware\ErrorHandlerMiddleware;
19
use Cake\Http\BaseApplication as CakeBaseApplication;
20
use Cake\Http\MiddlewareQueue;
21
use Cake\Routing\Middleware\AssetMiddleware;
22
use Cake\Routing\Middleware\RoutingMiddleware;
23
24
/**
25
 * Application setup base class.
26
 *
27
 * This defines the bootstrapping logic and middleware layers you
28
 * want to use in your application.
29
 */
30
class BaseApplication extends CakeBaseApplication
31
{
32
    /**
33
     * {@inheritDoc}
34
     *
35
     * Replace CakePHP `cache` command with \BEdita\WebTools\Shell\CacheShell
36
     */
37
    public function console($commands)
38
    {
39
        return $commands->addMany($commands->autoDiscover())
40
            ->add('cache', CacheShell::class);
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function bootstrap()
47
    {
48
        // Call parent to load bootstrap from files.
49
        parent::bootstrap();
50
51
        if (PHP_SAPI === 'cli') {
52
            $this->bootstrapCli();
53
        }
54
55
        /*
56
         * Only try to load DebugKit in development mode
57
         * Debug Kit should not be installed on a production system
58
         */
59
        if (Configure::read('debug')) {
60
            $this->addPlugin(\DebugKit\Plugin::class);
61
        }
62
63
        // Load more plugins here
64
        $this->addPlugin('WyriHaximus/TwigView', ['bootstrap' => true]);
65
    }
66
67
    /**
68
     * Setup the middleware queue your application will use.
69
     *
70
     * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
71
     * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
72
     */
73
    public function middleware($middlewareQueue) : MiddlewareQueue
74
    {
75
        $middlewareQueue
76
            // Catch any exceptions in the lower layers,
77
            // and make an error page/response
78
            ->add(new ErrorHandlerMiddleware(null, Configure::read('Error')))
79
80
            // Handle plugin/theme assets like CakePHP normally does.
81
            ->add(new AssetMiddleware([
82
                'cacheTime' => Configure::read('Asset.cacheTime')
83
            ]))
84
85
            // Add routing middleware.
86
            // If you have a large number of routes connected, turning on routes
87
            // caching in production could improve performance. For that when
88
            // creating the middleware instance specify the cache config name by
89
            // using it's second constructor argument:
90
            // `new RoutingMiddleware($this, '_cake_routes_')`
91
            ->add(new RoutingMiddleware($this));
92
93
        return $middlewareQueue;
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    protected function bootstrapCli()
100
    {
101
        try {
102
            $this->addPlugin('Bake');
103
        } catch (MissingPluginException $e) {
104
            // Do not halt if the plugin is missing
105
        }
106
    }
107
}
108