Skip to content

Commit

Permalink
DP-460 Upgrade to PHP 8.1 / Laravel 9
Browse files Browse the repository at this point in the history
 - Update dependencies
 - Get rid of global helper functions
 - Handle empty argument in array_merge
 - Fix exception error to suit to integration test
 - Refactor blob streaming in order to support file caching added in
 df-file v0.8 (see RemoteFileSystem::streamBlobIfModified). Vendor
 details implemented in S3FileSystem::getBlobInChunks
 - 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.
  • Loading branch information
daniilly committed Feb 27, 2023
1 parent da30574 commit e6dbf3b
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 208 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"dreamfactory/df-database": "~0.12",
"php": "^8.0",
"dreamfactory/df-database": "~1.0",
"dreamfactory/df-email": "~0.10",
"dreamfactory/df-file": "~0.7",
"dreamfactory/df-sqldb": "~0.17",
"dreamfactory/df-file": "~0.8",
"dreamfactory/df-sqldb": "~1.0",
"aws/aws-sdk-php": "3.*"
},
"autoload": {
Expand Down
48 changes: 10 additions & 38 deletions src/Components/S3FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DreamFactory\Core\Exceptions\DfException;
use DreamFactory\Core\Exceptions\BadRequestException;
use Aws\S3\S3Client;
use Illuminate\Support\Arr;

/**
* Class S3FileSystem
Expand Down Expand Up @@ -70,7 +71,7 @@ public function __construct($config)
$ex->getCode());
}

$this->container = array_get($config, 'container');
$this->container = Arr::get($config, 'container');

if (!$this->containerExists($this->container)) {
$this->createContainer(['name' => $this->container]);
Expand Down Expand Up @@ -170,7 +171,7 @@ public function containerExists($container = '')
*/
public function createContainer($properties, $metadata = [])
{
$name = array_get($properties, 'name', array_get($properties, 'path'));
$name = Arr::get($properties, 'name', Arr::get($properties, 'path'));
if (empty($name)) {
throw new BadRequestException('No name found for container in create request.');
}
Expand Down Expand Up @@ -563,50 +564,21 @@ public function getBlobProperties($container, $name)
}
}

/**
* @param string $container
* @param string $name
* @param array $params
*
* @throws DfException
*/
public function streamBlob($container, $name, $params = [])
protected function getBlobInChunks($containerName, $name, $chunkSize): \Generator
{
try {
$this->checkConnection();

/** @var \Aws\Result $result */
$result = $this->blobConn->getObject(
[
'Bucket' => $container,
'Key' => $name
]
);

header('Last-Modified: ' . $result->get('LastModified'));
header('Content-Type: ' . $result->get('ContentType'));
header('Content-Length:' . intval($result->get('ContentLength')));
$result = $this->blobConn->getObject(['Bucket' => $containerName, 'Key' => $name]);
$stream = &$result['Body'];
$stream->rewind();

$disposition =
(isset($params['disposition']) && !empty($params['disposition'])) ? $params['disposition'] : 'inline';

header('Content-Disposition: ' . $disposition . '; filename="' . $name . '";');
//echo $result->get('Body');
$result['Body']->rewind();
$chunk = \Config::get('df.file_chunk_size');
while ($data = $result['Body']->read($chunk)) {
echo $data;
while (!$stream->eof()) {
yield $stream->read($chunkSize);
}
} catch (\Exception $ex) {
if ($ex instanceof S3Exception) {
$code = $ex->getStatusCode();
$status_header = "HTTP/1.1 $code";
header($status_header);
header('Content-Type: text/html');
echo 'Failed to stream/download file. File ' . $name . ' was not found. ' . $ex->getMessage();
} else {
throw new DfException('Failed to stream blob: ' . $ex->getMessage());
}
throw new DfException('Failed to stream blob: ' . $ex->getMessage());
}
}
}
13 changes: 7 additions & 6 deletions src/Database/Schema/DynamoDbSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DreamFactory\Core\Database\Schema\TableSchema;
use DreamFactory\Core\Exceptions\NotFoundException;
use DreamFactory\Core\Exceptions\BadRequestException;
use Illuminate\Support\Arr;

class DynamoDbSchema extends Schema
{
Expand Down Expand Up @@ -100,14 +101,14 @@ protected function loadTableColumns(TableSchema $table)
$attributes = $result['Table']['AttributeDefinitions'];
$keys = $result['Table']['KeySchema'];
foreach ($attributes as $attribute) {
$dbType = array_get($attribute, 'AttributeType', 'S');
$dbType = Arr::get($attribute, 'AttributeType', 'S');
$type = static::extractSimpleType(static::awsTypeToType($dbType));
$name = array_get($attribute, 'AttributeName');
$name = Arr::get($attribute, 'AttributeName');
$column = ['name' => $name, 'type' => $type, 'db_type' => $dbType];
$c = new ColumnSchema($column);
$c->quotedName = $this->quoteColumnName($c->name);
foreach ($keys as $key) {
if ($name === array_get($key, 'AttributeName')) {
if ($name === Arr::get($key, 'AttributeName')) {
$c->isPrimaryKey = true;
$table->addPrimaryKey($c->name);
}
Expand All @@ -131,14 +132,14 @@ protected function loadTableColumns(TableSchema $table)
*/
public function createTable($table, $options)
{
if (empty($tableName = array_get($table, 'name'))) {
if (empty($tableName = Arr::get($table, 'name'))) {
throw new \Exception("No valid name exist in the received table schema.");
}

$properties = array_merge(
[static::TABLE_INDICATOR => $tableName],
$this->defaultCreateTable,
(array)array_get($table, 'native')
(array)Arr::get($table, 'native')
);
$result = $this->connection->createTable($properties);

Expand All @@ -156,7 +157,7 @@ public function updateTable($tableSchema, $changes)
// Update the provisioned throughput capacity of the table
$properties = array_merge(
[static::TABLE_INDICATOR => $tableSchema->quotedName],
(array)array_get($changes, 'native')
(array)Arr::get($changes, 'native')
);
$result = $this->connection->updateTable($properties);

Expand Down
63 changes: 27 additions & 36 deletions src/Database/Schema/RedshiftSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use DreamFactory\Core\Enums\DbSimpleTypes;
use DreamFactory\Core\Exceptions\BadRequestException;
use DreamFactory\Core\SqlDb\Database\Schema\SqlSchema;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

/**
* Schema is the class for retrieving metadata information from a AWS Redshift database.
Expand Down Expand Up @@ -40,7 +42,7 @@ public function getSupportedResourceTypes()
protected function translateSimpleColumnTypes(array &$info)
{
// override this in each schema class
$type = (isset($info['type'])) ? $info['type'] : null;
$type = $info['type'] ?? null;
switch ($type) {
// some types need massaging, some need other required properties
case 'pk':
Expand All @@ -65,7 +67,7 @@ protected function translateSimpleColumnTypes(array &$info)
case DbSimpleTypes::TYPE_TIMESTAMP_ON_CREATE:
case DbSimpleTypes::TYPE_TIMESTAMP_ON_UPDATE:
$info['type'] = 'timestamp';
$default = (isset($info['default'])) ? $info['default'] : null;
$default = $info['default'] ?? null;
if (!isset($default)) {
$default = 'CURRENT_TIMESTAMP';
// ON UPDATE CURRENT_TIMESTAMP not supported by PostgreSQL, use triggers
Expand Down Expand Up @@ -119,11 +121,11 @@ protected function translateSimpleColumnTypes(array &$info)
protected function validateColumnSettings(array &$info)
{
// override this in each schema class
$type = (isset($info['type'])) ? $info['type'] : null;
$type = $info['type'] ?? null;
switch ($type) {
// some types need massaging, some need other required properties
case 'boolean':
$default = (isset($info['default'])) ? $info['default'] : null;
$default = $info['default'] ?? null;
if (isset($default)) {
// convert to bit 0 or 1, where necessary
$info['default'] = (filter_var($default, FILTER_VALIDATE_BOOLEAN)) ? 'TRUE' : 'FALSE';
Expand All @@ -135,17 +137,13 @@ protected function validateColumnSettings(array &$info)
case 'int':
case 'bigint':
if (!isset($info['type_extras'])) {
$length =
(isset($info['length']))
? $info['length']
: ((isset($info['precision'])) ? $info['precision']
: null);
$length = $info['length'] ?? $info['precision'] ?? null;
if (!empty($length)) {
$info['type_extras'] = "($length)"; // sets the viewable length
}
}

$default = (isset($info['default'])) ? $info['default'] : null;
$default = $info['default'] ?? null;
if (isset($default) && is_numeric($default)) {
$info['default'] = intval($default);
}
Expand All @@ -156,38 +154,30 @@ protected function validateColumnSettings(array &$info)
case 'real':
case 'double precision':
if (!isset($info['type_extras'])) {
$length =
(isset($info['length']))
? $info['length']
: ((isset($info['precision'])) ? $info['precision']
: null);
$length = $info['length'] ?? $info['precision'] ?? null;
if (!empty($length)) {
$scale =
(isset($info['decimals']))
? $info['decimals']
: ((isset($info['scale'])) ? $info['scale']
: null);
$scale = $info['decimals'] ?? $info['scale'] ?? null;
$info['type_extras'] = (!empty($scale)) ? "($length,$scale)" : "($length)";
}
}

$default = (isset($info['default'])) ? $info['default'] : null;
$default = $info['default'] ?? null;
if (isset($default) && is_numeric($default)) {
$info['default'] = floatval($default);
}
break;

case 'char':
case 'national char':
$length = (isset($info['length'])) ? $info['length'] : ((isset($info['size'])) ? $info['size'] : null);
$length = $info['length'] ?? $info['size'] ?? null;
if (isset($length)) {
$info['type_extras'] = "($length)";
}
break;

case 'varchar':
case 'national varchar':
$length = (isset($info['length'])) ? $info['length'] : ((isset($info['size'])) ? $info['size'] : null);
$length = $info['length'] ?? $info['size'] ?? null;
if (isset($length)) {
$info['type_extras'] = "($length)";
} else // requires a max length
Expand All @@ -198,7 +188,7 @@ protected function validateColumnSettings(array &$info)

case 'time':
case 'timestamp':
$length = (isset($info['length'])) ? $info['length'] : ((isset($info['size'])) ? $info['size'] : null);
$length = $info['length'] ?? $info['size'] ?? null;
if (isset($length)) {
$info['type_extras'] = "($length)";
}
Expand All @@ -214,15 +204,15 @@ protected function validateColumnSettings(array &$info)
*/
protected function buildColumnDefinition(array $info)
{
$type = (isset($info['type'])) ? $info['type'] : null;
$typeExtras = (isset($info['type_extras'])) ? $info['type_extras'] : null;
$type = $info['type'] ?? null;
$typeExtras = $info['type_extras'] ?? null;

$definition = $type . $typeExtras;

$allowNull = (isset($info['allow_null'])) ? filter_var($info['allow_null'], FILTER_VALIDATE_BOOLEAN) : false;
$definition .= ($allowNull) ? ' NULL' : ' NOT NULL';

$default = (isset($info['default'])) ? $info['default'] : null;
$default = $info['default'] ?? null;
if (isset($default)) {
$quoteDefault =
(isset($info['quote_default'])) ? filter_var($info['quote_default'], FILTER_VALIDATE_BOOLEAN) : false;
Expand Down Expand Up @@ -264,7 +254,7 @@ public function resetSequence($table, $value = null)
return;
}
$sequence = '"' . $table->sequenceName . '"';
if (strpos($sequence, '.') !== false) {
if (Str::contains($sequence, '.')) {
$sequence = str_replace('.', '"."', $sequence);
}
if ($value !== null) {
Expand Down Expand Up @@ -302,7 +292,8 @@ protected function loadTableColumns(TableSchema $table)
$columns = $this->connection->select($sql, [':table' => $table->resourceName, ':schema' => $table->schemaName]);
foreach ($columns as $column) {
$column = array_change_key_case((array)$column, CASE_LOWER);
$c = new ColumnSchema(array_except($column, ['atthasdef', 'default', 'attnotnull']));
$c = new ColumnSchema(Arr::except($column, ['atthasdef', 'default', 'attnotnull']));

$c->quotedName = $this->quoteColumnName($c->name);
$c->allowNull = !boolval($column['attnotnull']);
$this->extractLimit($c, $c->dbType);
Expand All @@ -315,7 +306,7 @@ protected function loadTableColumns(TableSchema $table)

if ($c->isPrimaryKey) {
if ($c->autoIncrement) {
$table->sequenceName = array_get($column, 'sequence', $c->name);
$table->sequenceName = Arr::get($column, 'sequence', $c->name);
if ((DbSimpleTypes::TYPE_INTEGER === $c->type)) {
$c->type = DbSimpleTypes::TYPE_ID;
}
Expand Down Expand Up @@ -381,8 +372,8 @@ protected function getTableNames($schema = '')
$names = [];
foreach ($rows as $row) {
$row = (array)$row;
$schemaName = isset($row['table_schema']) ? $row['table_schema'] : '';
$resourceName = isset($row['table_name']) ? $row['table_name'] : '';
$schemaName = $row['table_schema'] ?? '';
$resourceName = $row['table_name'] ?? '';
$internalName = $schemaName . '.' . $resourceName;
$name = $resourceName;
$quotedName = $this->quoteTableName($schemaName) . '.' . $this->quoteTableName($resourceName);
Expand Down Expand Up @@ -411,8 +402,8 @@ protected function getViewNames($schema = '')
$names = [];
foreach ($rows as $row) {
$row = (array)$row;
$schemaName = isset($row['table_schema']) ? $row['table_schema'] : '';
$resourceName = isset($row['table_name']) ? $row['table_name'] : '';
$schemaName = $row['table_schema'] ?? '';
$resourceName = $row['table_name'] ?? '';
$internalName = $schemaName . '.' . $resourceName;
$name = $resourceName;
$quotedName = $this->quoteTableName($schemaName) . '.' . $this->quoteTableName($resourceName);
Expand Down Expand Up @@ -496,7 +487,7 @@ public function createIndex($name, $table, $columns, $unique = false)
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
}
foreach ($columns as $col) {
if (strpos($col, '(') !== false) {
if (Str::contains($col, '(')) {
$cols[] = $col;
} else {
$cols[] = $this->quoteColumnName($col);
Expand Down Expand Up @@ -566,7 +557,7 @@ protected function formatValueToPhpType($value, $type, $allow_null = true)
public function extractType(ColumnSchema $column, $dbType)
{
parent::extractType($column, $dbType);
if (strpos($dbType, '[') !== false || strpos($dbType, 'char') !== false || strpos($dbType, 'text') !== false) {
if (Str::contains($dbType, '[') || Str::contains($dbType, 'char') || Str::contains($dbType, 'text')) {
$column->type = DbSimpleTypes::TYPE_STRING;
} elseif (preg_match('/(real|float|double)/', $dbType)) {
$column->type = DbSimpleTypes::TYPE_DOUBLE;
Expand Down
Loading

0 comments on commit e6dbf3b

Please sign in to comment.