Completed
Pull Request — master (#10)
by Stefano
02:56
created

BaseApplication::bootstrapCli()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
            try {
61
                $this->addPlugin('DebugKit');
62
            } catch (MissingPluginException $e) {
63
                // Do not halt if the plugin is missing
64
            }
65
        }
66
        // Load more plugins here
67
        $this->addPlugin('WyriHaximus/TwigView', ['bootstrap' => true]);
68
    }
69
70
    /**
71
     * Setup the middleware queue your application will use.
72
     *
73
     * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
74
     * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
75
     */
76
    public function middleware($middlewareQueue) : MiddlewareQueue
77
    {
78
        $middlewareQueue
79
            // Catch any exceptions in the lower layers,
80
            // and make an error page/response
81
            ->add(new ErrorHandlerMiddleware(null, Configure::read('Error')))
82
83
            // Handle plugin/theme assets like CakePHP normally does.
84
            ->add(new AssetMiddleware([
85
                'cacheTime' => Configure::read('Asset.cacheTime')
86
            ]))
87
88
            // Add routing middleware.
89
            // If you have a large number of routes connected, turning on routes
90
            // caching in production could improve performance. For that when
91
            // creating the middleware instance specify the cache config name by
92
            // using it's second constructor argument:
93
            // `new RoutingMiddleware($this, '_cake_routes_')`
94
            ->add(new RoutingMiddleware($this));
95
96
        return $middlewareQueue;
97
    }
98
99
    /**
100
     * @return void
101
     */
102
    protected function bootstrapCli()
103
    {
104
        try {
105
            $this->addPlugin('Bake');
106
        } catch (MissingPluginException $e) {
107
            // Do not halt if the plugin is missing
108
        }
109
    }
110
}
111