Skip to content

Commit b92219a

Browse files
committed
Replace some deprecated config and the front controller
1 parent 4c2be88 commit b92219a

File tree

4 files changed

+171
-34
lines changed

4 files changed

+171
-34
lines changed

config/bootstrap.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@
4040

4141
use Cake\Cache\Cache;
4242
use Cake\Console\ConsoleErrorHandler;
43-
use Cake\Core\App;
4443
use Cake\Core\Configure;
4544
use Cake\Core\Configure\Engine\PhpConfig;
4645
use Cake\Core\Plugin;
4746
use Cake\Database\Type;
4847
use Cake\Datasource\ConnectionManager;
4948
use Cake\Error\ErrorHandler;
49+
use Cake\Http\ServerRequest;
5050
use Cake\Log\Log;
5151
use Cake\Mailer\Email;
52-
use Cake\Network\Request;
5352
use Cake\Routing\DispatcherFactory;
54-
use Cake\Utility\Inflector;
5553
use Cake\Utility\Security;
5654

5755
/**
@@ -150,11 +148,11 @@
150148
/**
151149
* Setup detectors for mobile and tablet.
152150
*/
153-
Request::addDetector('mobile', function ($request) {
151+
ServerRequest::addDetector('mobile', function ($request) {
154152
$detector = new \Detection\MobileDetect();
155153
return $detector->isMobile();
156154
});
157-
Request::addDetector('tablet', function ($request) {
155+
ServerRequest::addDetector('tablet', function ($request) {
158156
$detector = new \Detection\MobileDetect();
159157
return $detector->isTablet();
160158
});
@@ -196,13 +194,6 @@
196194
Plugin::load('DebugKit', ['bootstrap' => true]);
197195
}
198196

199-
/**
200-
* Connect middleware/dispatcher filters.
201-
*/
202-
DispatcherFactory::add('Asset');
203-
DispatcherFactory::add('Routing');
204-
DispatcherFactory::add('ControllerFactory');
205-
206197
/**
207198
* Enable default locale format parsing.
208199
* This is needed for matching the auto-localized string output of Time() class when parsing dates.

config/requirements.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @since 3.5.0
13+
* @license https://opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
16+
/*
17+
* You can empty out this file, if you are certain that you match all requirements.
18+
*/
19+
20+
/*
21+
* You can remove this if you are confident that your PHP version is sufficient.
22+
*/
23+
if (version_compare(PHP_VERSION, '5.6.0') < 0) {
24+
trigger_error('Your PHP version must be equal or higher than 5.6.0 to use CakePHP.' . PHP_EOL, E_USER_ERROR);
25+
}
26+
27+
/*
28+
* You can remove this if you are confident you have intl installed.
29+
*/
30+
if (!extension_loaded('intl')) {
31+
trigger_error('You must enable the intl extension to use CakePHP.' . PHP_EOL, E_USER_ERROR);
32+
}
33+
34+
/*
35+
* You can remove this if you are confident you have mbstring installed.
36+
*/
37+
if (!extension_loaded('mbstring')) {
38+
trigger_error('You must enable the mbstring extension to use CakePHP.' . PHP_EOL, E_USER_ERROR);
39+
}

src/Application.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @since 3.3.0
13+
* @license https://opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace App;
16+
17+
use Cake\Core\Configure;
18+
use Cake\Core\Exception\MissingPluginException;
19+
use Cake\Error\Middleware\ErrorHandlerMiddleware;
20+
use Cake\Http\BaseApplication;
21+
use Cake\Http\Middleware\CsrfProtectionMiddleware;
22+
use Cake\Routing\Middleware\AssetMiddleware;
23+
use Cake\Routing\Middleware\RoutingMiddleware;
24+
25+
/**
26+
* Application setup class.
27+
*
28+
* This defines the bootstrapping logic and middleware layers you
29+
* want to use in your application.
30+
*/
31+
class Application extends BaseApplication
32+
{
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function bootstrap()
37+
{
38+
// Call parent to load bootstrap from files.
39+
parent::bootstrap();
40+
41+
if (PHP_SAPI === 'cli') {
42+
try {
43+
$this->addPlugin('Bake');
44+
} catch (MissingPluginException $e) {
45+
// Do not halt if the plugin is missing
46+
}
47+
48+
$this->addPlugin('Migrations');
49+
}
50+
51+
/*
52+
* Only try to load DebugKit in development mode
53+
* Debug Kit should not be installed on a production system
54+
*/
55+
if (Configure::read('debug')) {
56+
$this->addPlugin(\DebugKit\Plugin::class);
57+
}
58+
}
59+
60+
/**
61+
* Define the routes for an application.
62+
*
63+
* Use the provided RouteBuilder to define an application's routing, register scoped middleware.
64+
*
65+
* @param \Cake\Routing\RouteBuilder $routes A route builder to add routes into.
66+
* @return void
67+
*/
68+
public function routes($routes)
69+
{
70+
// Register scoped middleware for use in routes.php
71+
$routes->registerMiddleware('csrf', new CsrfProtectionMiddleware([
72+
'httpOnly' => true
73+
]));
74+
75+
parent::routes($routes);
76+
}
77+
78+
/**
79+
* Setup the middleware queue your application will use.
80+
*
81+
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
82+
* @return \Cake\Http\MiddlewareQueue The updated middleware queue.
83+
*/
84+
public function middleware($middlewareQueue)
85+
{
86+
$middlewareQueue
87+
// Catch any exceptions in the lower layers,
88+
// and make an error page/response
89+
->add(new ErrorHandlerMiddleware(null, Configure::read('Error')))
90+
91+
// Handle plugin/theme assets like CakePHP normally does.
92+
->add(new AssetMiddleware([
93+
'cacheTime' => Configure::read('Asset.cacheTime')
94+
]))
95+
96+
// Add routing middleware.
97+
// Routes collection cache enabled by default, to disable route caching
98+
// pass null as cacheConfig, example: `new RoutingMiddleware($this)`
99+
// you might want to disable this cache in case your routing is extremely simple
100+
->add(new RoutingMiddleware($this, '_cake_routes_'));
101+
102+
return $middlewareQueue;
103+
}
104+
}

webroot/index.php

+25-22
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,39 @@
22
/**
33
* The Front Controller for handling every request
44
*
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
1414
* @since 0.2.9
15-
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
15+
* @license MIT License (https://opensource.org/licenses/mit-license.php)
1616
*/
17-
// for built-in server
18-
if (php_sapi_name() === 'cli-server') {
19-
$_SERVER['PHP_SELF'] = '/' . basename(__FILE__);
2017

21-
$url = parse_url(urldecode($_SERVER['REQUEST_URI']));
22-
$file = __DIR__ . $url['path'];
23-
if (strpos($url['path'], '..') === false && strpos($url['path'], '.') !== false && is_file($file)) {
24-
return false;
25-
}
18+
// Check platform requirements
19+
require dirname(__DIR__) . '/config/requirements.php';
20+
21+
// For built-in server
22+
if (PHP_SAPI === 'cli-server') {
23+
$_SERVER['PHP_SELF'] = '/' . basename(__FILE__);
24+
25+
$url = parse_url(urldecode($_SERVER['REQUEST_URI']));
26+
$file = __DIR__ . $url['path'];
27+
if (strpos($url['path'], '..') === false && strpos($url['path'], '.') !== false && is_file($file)) {
28+
return false;
29+
}
2630
}
27-
require dirname(__DIR__) . '/config/bootstrap.php';
31+
require dirname(__DIR__) . '/vendor/autoload.php';
32+
33+
use App\Application;
34+
use Cake\Http\Server;
2835

29-
use Cake\Network\Request;
30-
use Cake\Network\Response;
31-
use Cake\Routing\DispatcherFactory;
36+
// Bind your application to the server.
37+
$server = new Server(new Application(dirname(__DIR__) . '/config'));
3238

33-
$dispatcher = DispatcherFactory::create();
34-
$dispatcher->dispatch(
35-
Request::createFromGlobals(),
36-
new Response()
37-
);
39+
// Run the request/response through the application and emit the response.
40+
$server->emit($server->run());

0 commit comments

Comments
 (0)