Completed
Push — master ( 492074...95a3da )
by Cheren
14:22
created

Application::bootstrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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