Skip to content

Commit c91d00a

Browse files
committed
DP-460 Upgrade to PHP 8.1 / Laravel 9
Resolve conflict with `db.schema` component. App code used `db.schema` name for `DbSchemaExtensions` component. Starting from Laravel 9 this name is used internally because of migration to string based accessor for Schema facade laravel/framework@8059b39. The cure is to use `df` prefix for app `db.schema` component. Fix single quotes encode in error message. In PHP 8 default string escape strategy changed to escape single quotes (') as well. This commit adds explicit use of `ENT_COMPAT` flag. See https://www.php.net/manual/en/function.htmlentities.php Get rid of request dynamic properties retrieving. `request->wrap` dynamic property was leading to full request params reload including file uploads. Uploaded file was moved and not available that caused an error. Instead we explicitly read query param by name. Resolve deprecated required parameter after optional one. See https://php.watch/versions/8.0/deprecate-required-param-after-optional Fix db migration using SQLite. Connection config must hold `name` property since Laravel 8.83.2. See laravel/framework@03e3a807. Use 30 sec timeout for cURL requests. Misconfigured TCP logger was leading to system hang up because of endless waiting for response. Now requests will fail after 30 seconds of waiting. We could switch to v2.0 of tymon/jwt-auth package when this issue got resolved: tymondesigns/jwt-auth#2213 Other changes: - Update dependencies - Upgrade CORS middleware - Fix unit tests
1 parent 0e13961 commit c91d00a

File tree

14 files changed

+43
-66
lines changed

14 files changed

+43
-66
lines changed

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
"minimum-stability": "dev",
3131
"prefer-stable": true,
3232
"require": {
33-
"php": ">=7.0.0",
34-
"barryvdh/laravel-cors": "~0.11.0",
35-
"doctrine/dbal": "~2.5.0",
36-
"guzzlehttp/guzzle": "~6.0",
37-
"symfony/yaml": "~2.8|~3.0",
38-
"tymon/jwt-auth": "~1.0.0"
33+
"php": "^8.0",
34+
"fruitcake/laravel-cors": "~3.0.0",
35+
"doctrine/dbal": "^3.1.4",
36+
"guzzlehttp/guzzle": "~7.4.5",
37+
"symfony/yaml": "^6.0",
38+
"tymon/jwt-auth": "dev-develop"
3939
},
4040
"require-dev": {
4141
"phpunit/phpunit": "@stable"

src/Components/DfResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ protected function morphToJson($content)
2222

2323
return json_encode($content, JSON_UNESCAPED_SLASHES);
2424
}
25+
26+
static function create($content = '', $status = 200, array $headers = []) {
27+
return new DfResponse($content, $status, $headers);
28+
}
2529
}

src/Components/ExceptionResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function exceptionToArray(\Exception $exception)
5151
}
5252

5353
$errorInfo['code'] = ($exception->getCode()) ?: ServiceResponseInterface::HTTP_INTERNAL_SERVER_ERROR;
54-
$errorInfo['message'] = htmlentities($exception->getMessage());
54+
$errorInfo['message'] = htmlentities($exception->getMessage(), ENT_COMPAT);
5555

5656
if (config('app.debug', false)) {
5757
$trace = $exception->getTraceAsString();
@@ -68,4 +68,4 @@ public static function exceptionToArray(\Exception $exception)
6868

6969
return $errorInfo;
7070
}
71-
}
71+
}

src/Database/Schema/TableSchema.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ public function getColumns(bool $use_alias = false): array
212212

213213
// re-index for alias usage, easier to find requested fields from client
214214
$columns = [];
215-
$this->columns = null;
216215

217-
/** @var ColumnSchema $column */
218216
try {
217+
/** @var ColumnSchema $column */
219218
foreach ($this->columns as $column) {
220219
$columns[strtolower($column->getName(true))] = $column;
221220
}

src/Exceptions/DfException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function toArray()
6464
{
6565
$errorInfo['code'] = $this->getCode();
6666
$errorInfo['context'] = $this->getContext();
67-
$errorInfo['message'] = htmlentities($this->getMessage());
67+
$errorInfo['message'] = htmlentities($this->getMessage(), ENT_COMPAT);
6868

6969
if (config('app.debug', false)) {
7070
$trace = $this->getTraceAsString();

src/Facades/DbSchemaExtensions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class DbSchemaExtensions extends Facade
1616
*/
1717
protected static function getFacadeAccessor()
1818
{
19-
return 'db.schema';
19+
return 'df.db.schema';
2020
}
2121
}

src/LaravelServiceProvider.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function addAliases()
8787
{
8888
$this->app->alias('df.service', ServiceManager::class);
8989
$this->app->alias('df.system.table_model_map', SystemTableModelMapper::class);
90-
$this->app->alias('db.schema', DbSchemaExtensions::class);
90+
$this->app->alias('df.db.schema', DbSchemaExtensions::class);
9191

9292
// DreamFactory Specific Facades...
9393
$loader = AliasLoader::getInstance();
@@ -162,7 +162,7 @@ protected function registerServices()
162162

163163
// The database schema extension manager is used to resolve various database schema extensions.
164164
// It also implements the resolver interface which may be used by other components adding schema extensions.
165-
$this->app->singleton('db.schema', function ($app) {
165+
$this->app->singleton('df.db.schema', function ($app) {
166166
return new DbSchemaExtensions($app);
167167
});
168168
}
@@ -171,7 +171,8 @@ protected function registerExtensions()
171171
{
172172
// Add our database drivers.
173173
$this->app->resolving('db', function (DatabaseManager $db) {
174-
$db->extend('sqlite', function ($config) {
174+
$db->extend('sqlite', function ($config, $name) {
175+
$config = Arr::add($config, 'name', $name);
175176
$connector = new SQLiteConnector();
176177
$connection = $connector->connect($config);
177178

src/Providers/CorsServiceProvider.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace DreamFactory\Core\Providers;
44

5-
use Barryvdh\Cors\HandleCors;
6-
use Barryvdh\Cors\HandlePreflight;
7-
use Asm89\Stack\CorsService;
5+
use Fruitcake\Cors\HandleCors;
6+
use Fruitcake\Cors\CorsService;
87
use DreamFactory\Core\Models\CorsConfig;
98
use Illuminate\Database\QueryException;
109
use Illuminate\Contracts\Http\Kernel;
@@ -38,14 +37,6 @@ public function boot(Request $request, Kernel $kernel)
3837
{
3938
$config = $this->getOptions($request);
4039
$this->app->singleton(CorsService::class, function () use ($config){
41-
42-
if (isset($config['allowedOrigins'])) {
43-
foreach ($config['allowedOrigins'] as $origin) {
44-
if (strpos($origin, '*') !== false) {
45-
$config['allowedOriginsPatterns'][] = $this->convertWildcardToPattern($origin);
46-
}
47-
}
48-
}
4940
return new CorsService($config);
5041
});
5142

@@ -60,30 +51,6 @@ public function boot(Request $request, Kernel $kernel)
6051
}
6152

6253
Route::prependMiddlewareToGroup('df.api', 'df.cors');
63-
64-
if ($request->isMethod('OPTIONS')) {
65-
/** @noinspection PhpUndefinedMethodInspection */
66-
$kernel->prependMiddleware(HandlePreflight::class);
67-
}
68-
}
69-
70-
/**
71-
* Create a pattern for a wildcard, based on Str::is() from Laravel
72-
*
73-
* @see https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Str.php
74-
* @param $pattern
75-
* @return string
76-
*/
77-
protected function convertWildcardToPattern($pattern)
78-
{
79-
$pattern = preg_quote($pattern, '#');
80-
81-
// Asterisks are translated into zero-or-more regular expression wildcards
82-
// to make it convenient to check if the strings starts with the given
83-
// pattern such as "library/*", making any string check convenient.
84-
$pattern = str_replace('\*', '.*', $pattern);
85-
86-
return '#^'.$pattern.'\z#u';
8754
}
8855

8956
/**

src/Testing/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TestCase extends LaravelTestCase
3737
/**
3838
* Runs before every test class.
3939
*/
40-
public static function setupBeforeClass()
40+
public static function setupBeforeClass(): void
4141
{
4242
echo "\n------------------------------------------------------------------------------\n";
4343
echo "Running test: " . get_called_class() . "\n";
@@ -114,7 +114,7 @@ public function stage()
114114
*/
115115
public function createApplication()
116116
{
117-
$app = require '/opt/dreamfactory/bootstrap/app.php';
117+
$app = require './bootstrap/app.php';
118118

119119
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
120120

src/Utility/Curl.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Curl extends Verbs
8282
*/
8383
public static function get($url, $payload = [], $curlOptions = [])
8484
{
85-
return static::_httpRequest(static::GET, $url, $payload, $curlOptions);
85+
return static::_httpRequest($url, static::GET, $payload, $curlOptions);
8686
}
8787

8888
/**
@@ -94,7 +94,7 @@ public static function get($url, $payload = [], $curlOptions = [])
9494
*/
9595
public static function put($url, $payload = [], $curlOptions = [])
9696
{
97-
return static::_httpRequest(static::PUT, $url, $payload, $curlOptions);
97+
return static::_httpRequest($url, static::PUT, $payload, $curlOptions);
9898
}
9999

100100
/**
@@ -106,7 +106,7 @@ public static function put($url, $payload = [], $curlOptions = [])
106106
*/
107107
public static function post($url, $payload = [], $curlOptions = [])
108108
{
109-
return static::_httpRequest(static::POST, $url, $payload, $curlOptions);
109+
return static::_httpRequest($url, static::POST, $payload, $curlOptions);
110110
}
111111

112112
/**
@@ -118,7 +118,7 @@ public static function post($url, $payload = [], $curlOptions = [])
118118
*/
119119
public static function delete($url, $payload = [], $curlOptions = [])
120120
{
121-
return static::_httpRequest(static::DELETE, $url, $payload, $curlOptions);
121+
return static::_httpRequest($url, static::DELETE, $payload, $curlOptions);
122122
}
123123

124124
/**
@@ -130,7 +130,7 @@ public static function delete($url, $payload = [], $curlOptions = [])
130130
*/
131131
public static function head($url, $payload = [], $curlOptions = [])
132132
{
133-
return static::_httpRequest(static::HEAD, $url, $payload, $curlOptions);
133+
return static::_httpRequest($url, static::HEAD, $payload, $curlOptions);
134134
}
135135

136136
/**
@@ -142,7 +142,7 @@ public static function head($url, $payload = [], $curlOptions = [])
142142
*/
143143
public static function options($url, $payload = [], $curlOptions = [])
144144
{
145-
return static::_httpRequest(static::OPTIONS, $url, $payload, $curlOptions);
145+
return static::_httpRequest($url, static::OPTIONS, $payload, $curlOptions);
146146
}
147147

148148
/**
@@ -154,7 +154,7 @@ public static function options($url, $payload = [], $curlOptions = [])
154154
*/
155155
public static function copy($url, $payload = [], $curlOptions = [])
156156
{
157-
return static::_httpRequest(static::COPY, $url, $payload, $curlOptions);
157+
return static::_httpRequest($url, static::COPY, $payload, $curlOptions);
158158
}
159159

160160
/**
@@ -166,7 +166,7 @@ public static function copy($url, $payload = [], $curlOptions = [])
166166
*/
167167
public static function patch($url, $payload = [], $curlOptions = [])
168168
{
169-
return static::_httpRequest(static::PATCH, $url, $payload, $curlOptions);
169+
return static::_httpRequest($url, static::PATCH, $payload, $curlOptions);
170170
}
171171

172172
/**
@@ -179,7 +179,7 @@ public static function patch($url, $payload = [], $curlOptions = [])
179179
*/
180180
public static function request($method, $url, $payload = [], $curlOptions = [])
181181
{
182-
return static::_httpRequest($method, $url, $payload, $curlOptions);
182+
return static::_httpRequest($url, $method, $payload, $curlOptions);
183183
}
184184

185185
/**
@@ -191,7 +191,7 @@ public static function request($method, $url, $payload = [], $curlOptions = [])
191191
* @throws \InvalidArgumentException
192192
* @return bool|mixed|\stdClass
193193
*/
194-
protected static function _httpRequest($method = self::GET, $url, $payload = [], $curlOptions = [])
194+
protected static function _httpRequest($url, $method = self::GET, $payload = [], $curlOptions = [])
195195
{
196196
if (!static::contains($method)) {
197197
throw new \InvalidArgumentException('Invalid method "' . $method . '" specified.');
@@ -209,6 +209,7 @@ protected static function _httpRequest($method = self::GET, $url, $payload = [],
209209
CURLOPT_HEADER => true,
210210
CURLINFO_HEADER_OUT => true,
211211
CURLOPT_SSL_VERIFYPEER => false,
212+
CURLOPT_TIMEOUT => 30
212213
];
213214

214215
// Merge in the global options if any

0 commit comments

Comments
 (0)