Application   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 7
dl 0
loc 98
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bootstrap() 0 29 4
A middleware() 0 26 1
A addPlugin() 0 15 3
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Core
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Core".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core;
17
18
use Core\Core\Plugin;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Core\Plugin.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use Cake\Core\Configure;
20
use Core\Event\EventManager;
21
use Cake\Http\BaseApplication;
22
use Cake\Http\MiddlewareQueue;
23
use Cake\Core\PluginInterface;
24
use Core\View\Middleware\ThemeMiddleware;
25
use Cake\Routing\Middleware\AssetMiddleware;
26
use Cake\Routing\Middleware\RoutingMiddleware;
27
use Cake\Error\Middleware\ErrorHandlerMiddleware;
28
29
/**
30
 * Class Application
31
 *
32
 * @package App
33
 */
34
class Application extends BaseApplication
35
{
36
37
    /**
38
     * Load all the application configuration and bootstrap logic.
39
     *
40
     * Override this method to add additional bootstrap logic for your application.
41
     *
42
     * @return  void
43
     */
44
    public function bootstrap()
45
    {
46
        parent::bootstrap();
47
48
        $this->addPlugin('Core', ['bootstrap' => true, 'routes' => true]);
49
50
        //  Load all plugins.
51
        $plugins = [
52
            'Search',
53
            'Config',
54
            'Community',
55
            'Migrations',
56
            'Extensions',
57
            Configure::read('Theme.site'),
58
            Configure::read('Theme.admin'),
59
        ];
60
61
        foreach ($plugins as $name) {
62
            if (Plugin::isLoaded($name)) {
63
                continue;
64
            }
65
66
            if ($path = Plugin::findPlugin($name)) {
67
                $this->addPlugin($name, Plugin::getConfigForLoad($path));
68
            }
69
        }
70
71
        EventManager::loadListeners();
72
    }
73
74
    /**
75
     * Setup the middleware queue your application will use.
76
     *
77
     * @param   MiddlewareQueue $middlewareQueue The middleware queue to setup.
78
     *
79
     * @return  MiddlewareQueue The updated middleware queue.
80
     */
81
    public function middleware($middlewareQueue)
82
    {
83
        $middlewareQueue
84
85
            /** Catch any exceptions in the lower layers, and make an error page/response */
86
            ->add(ErrorHandlerMiddleware::class)
87
88
            /** Handle plugin/theme assets like CakePHP normally does. */
89
            ->add(AssetMiddleware::class)
90
91
            /**
92
             * Add routing middleware.
93
             * Routes collection cache enabled by default, to disable route caching pass null as cacheConfig, example:
94
             * `new RoutingMiddleware($this)` you might want to disable this cache in case your routing
95
             * is extremely simple.
96
             */
97
            ->add(new RoutingMiddleware($this, '_cake_routes_'))
98
99
            /**
100
             * Add theme middleware.
101
             * Setup request params 'theme'. Param hold current theme name.
102
             */
103
            ->add(ThemeMiddleware::class);
104
105
        return $middlewareQueue;
106
    }
107
108
    /**
109
     * Add new plugin.
110
     *
111
     * @param   PluginInterface|string $name
112
     * @param   array $config
113
     *
114
     * @return  $this
115
     */
116
    public function addPlugin($name, array $config = [])
117
    {
118
        parent::addPlugin($name, $config);
119
120
        $plugin = (array) $name;
121
122
        foreach ($plugin as $_name) {
123
            if ((bool) Plugin::isLoaded($_name)) {
124
                Plugin::addManifestCallback($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 116 can also be of type object<Cake\Core\PluginInterface>; however, Core\Core\Plugin::addManifestCallback() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
125
                Cms::mergeConfig('App.paths.locales', Plugin::getLocalePath($name));
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 116 can also be of type object<Cake\Core\PluginInterface>; however, Core\Core\Plugin::getLocalePath() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
126
            }
127
        }
128
129
        return $this;
130
    }
131
}
132