| 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 Cms::mergeConfig('App.paths.locales', Plugin::getLocalePath($name)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  | 
            
                        
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: