Skip to content

Commit 41487ac

Browse files
authored
Updates for CakePHP 4.4. (#7716)
Consolidated all actions into a single controller.
1 parent 9ca2744 commit 41487ac

18 files changed

+165
-3638
lines changed

frameworks/PHP/cakephp/benchmark_config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"webserver": "nginx",
2121
"os": "Linux",
2222
"database_os": "Linux",
23-
"display_name": "CakePHP",
23+
"display_name": "CakePHP 4.4",
2424
"notes": "",
2525
"versus": "php"
2626
},

frameworks/PHP/cakephp/cakephp-workerman.dockerfile

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ EXPOSE 8080
1919
ADD ./ /cakephp
2020
WORKDIR /cakephp
2121

22-
RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
2322
RUN composer require joanhey/adapterman:^0.5
23+
RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
2424

2525
RUN chmod -R 777 /cakephp
2626

2727
#COPY deploy/conf/cli-php.ini /etc/php/8.1/cli/php.ini
2828

29-
# bypass cli bootstrap for Workerman
30-
RUN sed -i "s|//$isCli = false|$isCli = false|g" config/bootstrap.php
31-
3229
CMD php -c deploy/conf/cli-php.ini \
3330
server.php start

frameworks/PHP/cakephp/config/app.php

-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@
170170
*/
171171
'Error' => [
172172
'errorLevel' => E_ALL,
173-
'exceptionRenderer' => 'Cake\Error\ExceptionRenderer',
174173
'skipLog' => [],
175174
'log' => true,
176175
'trace' => true,

frameworks/PHP/cakephp/config/bootstrap.php

+10-20
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@
3030
require CORE_PATH . 'config' . DS . 'bootstrap.php';
3131

3232
use Cake\Cache\Cache;
33-
use Cake\Console\ConsoleErrorHandler;
3433
use Cake\Core\Configure;
3534
use Cake\Core\Configure\Engine\PhpConfig;
36-
use Cake\Core\Plugin;
37-
use Cake\Database\Type;
35+
use Cake\Database\Type\StringType;
36+
use Cake\Database\TypeFactory;
3837
use Cake\Datasource\ConnectionManager;
39-
use Cake\Error\ErrorHandler;
40-
use Cake\Http\ServerRequest;
38+
use Cake\Error\ErrorTrap;
39+
use Cake\Error\ExceptionTrap;
4140
use Cake\Log\Log;
4241
use Cake\Mailer\Mailer;
4342
use Cake\Mailer\TransportFactory;
44-
use Cake\Utility\Inflector;
43+
use Cake\Routing\Router;
4544
use Cake\Utility\Security;
4645

4746
/**
@@ -114,20 +113,8 @@
114113
/*
115114
* Register application error and exception handlers.
116115
*/
117-
$isCli = PHP_SAPI === 'cli';
118-
//$isCli = false; // Workerman
119-
if ($isCli) {
120-
(new ConsoleErrorHandler(Configure::read('Error')))->register();
121-
} else {
122-
(new ErrorHandler(Configure::read('Error')))->register();
123-
}
124-
125-
/*
126-
* Include the CLI bootstrap overrides.
127-
*/
128-
if ($isCli) {
129-
require __DIR__ . '/bootstrap_cli.php';
130-
}
116+
(new ErrorTrap(Configure::read('Error')))->register();
117+
(new ExceptionTrap(Configure::read('Error')))->register();
131118

132119
/*
133120
* Set the full base URL.
@@ -187,6 +174,9 @@
187174
// TypeFactory::build('timestamptimezone')
188175
// ->useMutable();
189176

177+
// There is no time-specific type in Cake
178+
TypeFactory::map('time', StringType::class);
179+
190180
/*
191181
* Custom Inflector rules, can be set to correctly pluralize or singularize
192182
* table, model, controller names or whatever other string is passed to the

frameworks/PHP/cakephp/config/bootstrap_cli.php

-28
This file was deleted.

frameworks/PHP/cakephp/config/cacert.pem

-3,401
This file was deleted.

frameworks/PHP/cakephp/src/Application.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
4949

5050
public function routes(RouteBuilder $routes): void
5151
{
52-
$routes->connect('/json', ['controller' => 'Json', 'action' => 'index']);
53-
$routes->connect('/plaintext', ['controller' => 'Plaintext', 'action' => 'index']);
54-
$routes->connect('/fortunes', ['controller' => 'Fortunes', 'action' => 'index']);
55-
$routes->connect('/db', ['controller' => 'World', 'action' => 'query']);
56-
$routes->connect('/queries', ['controller' => 'World', 'action' => 'index']);
57-
$routes->connect('/updates', ['controller' => 'World', 'action' => 'updates']);
58-
}
52+
$routes->connect('/json', ['controller' => 'Main', 'action' => 'json']);
53+
$routes->connect('/plaintext', ['controller' => 'Main', 'action' => 'plaintext']);
54+
$routes->connect('/fortunes', ['controller' => 'Main', 'action' => 'fortunes']);
55+
$routes->connect('/db', ['controller' => 'Main', 'action' => 'db']);
56+
$routes->connect('/queries', ['controller' => 'Main', 'action' => 'queries']);
57+
$routes->connect('/updates', ['controller' => 'Main', 'action' => 'updates']);
58+
}
5959
}

frameworks/PHP/cakephp/src/Controller/FortunesController.php

-32
This file was deleted.

frameworks/PHP/cakephp/src/Controller/JsonController.php

-19
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Cake\Controller\Controller;
6+
7+
class MainController extends Controller
8+
{
9+
public function json()
10+
{
11+
$data = ['message' => 'Hello, World!'];
12+
13+
return $this->_jsonResponse($data);
14+
}
15+
16+
public function db()
17+
{
18+
$world = $this->fetchTable('World')->get(self::_randomInt());
19+
20+
return $this->_jsonResponse($world);
21+
}
22+
23+
public function fortunes()
24+
{
25+
// Calling all() will execute the query
26+
// and return the result set.
27+
$fortunes = $this->fetchTable('Fortune')->find()->all();
28+
29+
// stuffing in the dynamic data
30+
$fortune = $this->fetchTable('Fortune')->newEntity([
31+
'id' => 0,
32+
'message' => 'Additional fortune added at request time.',
33+
]);
34+
35+
$fortunes = $fortunes->appendItem($fortune);
36+
37+
$fortunes = $fortunes->sortBy('message', SORT_ASC, SORT_STRING)->compile(false);
38+
39+
$this->set('fortunes', $fortunes);
40+
}
41+
42+
public function queries()
43+
{
44+
// Create an array with the response string.
45+
$worlds = [];
46+
$queries = $this->_getQueryCount();
47+
$worldRepo = $this->fetchTable('World');
48+
49+
// For each query, store the result set values in the response array
50+
while ($queries--) {
51+
$worlds[] = $worldRepo->get(self::_randomInt());
52+
}
53+
54+
return $this->_jsonResponse($worlds);
55+
}
56+
57+
public function updates()
58+
{
59+
// Create an array with the response string.
60+
$worlds = [];
61+
$queries = $this->_getQueryCount();
62+
$worldRepo = $this->fetchTable('World');
63+
64+
// For each query, store the result set values in the response array
65+
while ($queries--) {
66+
$world = $worldRepo->get(self::_randomInt());
67+
$world->randomNumber = self::_randomInt();
68+
69+
$worldRepo->save($world);
70+
$worlds[] = $world;
71+
}
72+
73+
return $this->_jsonResponse($worlds);
74+
}
75+
76+
public function plaintext()
77+
{
78+
return $this->response
79+
->withStringBody('Hello, World!')
80+
->withType('text/plain');
81+
}
82+
83+
protected function _jsonResponse($data)
84+
{
85+
return $this->response
86+
->withType('application/json')
87+
->withStringBody(\json_encode($data));
88+
}
89+
90+
protected function _getQueryCount()
91+
{
92+
$queries = (int)$this->request->getQuery('queries');
93+
94+
if ($queries === 0) {
95+
$queries = 1;
96+
} elseif ($queries > 500) {
97+
$queries = 500;
98+
}
99+
100+
return $queries;
101+
}
102+
103+
protected static function _randomInt()
104+
{
105+
return random_int(1, 10000);
106+
}
107+
}

frameworks/PHP/cakephp/src/Controller/PlaintextController.php

-16
This file was deleted.

frameworks/PHP/cakephp/src/Controller/WorldController.php

-76
This file was deleted.

0 commit comments

Comments
 (0)