Skip to content

Commit

Permalink
Cleanup + MariaDB fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Feb 6, 2024
1 parent 1ecb86f commit 585a131
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 38 deletions.
10 changes: 0 additions & 10 deletions src/models/DeprecationError.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace craft\models;

use craft\base\Model;
use craft\helpers\Json;
use craft\validators\DateTimeValidator;
use DateTime;

Expand Down Expand Up @@ -60,15 +59,6 @@ class DeprecationError extends Model
*/
public ?array $traces = null;

public function __construct($config = [])
{
if (is_string($config['traces'] ?? null)) {
$config['traces'] = Json::decode($config['traces']);
}

parent::__construct($config);
}

/**
* @inheritdoc
*/
Expand Down
10 changes: 0 additions & 10 deletions src/models/GqlSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace craft\models;

use craft\base\Model;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\records\GqlSchema as GqlSchemaRecord;
use craft\validators\UniqueValidator;
Expand Down Expand Up @@ -52,15 +51,6 @@ class GqlSchema extends Model
*/
private array $_cachedPairs = [];

public function __construct($config = [])
{
if (isset($config['scope']) && is_string($config['scope'])) {
$config['scope'] = Json::decode($config['scope']);
}

parent::__construct($config);
}

/**
* @inheritdoc
*/
Expand Down
5 changes: 0 additions & 5 deletions src/services/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use craft\events\WidgetEvent;
use craft\helpers\Component as ComponentHelper;
use craft\helpers\Db;
use craft\helpers\Json;
use craft\records\Widget as WidgetRecord;
use craft\widgets\CraftSupport as CraftSupportWidget;
use craft\widgets\Feed as FeedWidget;
Expand Down Expand Up @@ -124,10 +123,6 @@ public function createWidget(mixed $config): WidgetInterface
$config = ['type' => $config];
}

if (is_string($config['settings'] ?? null)) {
$config['settings'] = Json::decode($config['settings']);
}

try {
$widget = ComponentHelper::createComponent($config, WidgetInterface::class);
} catch (MissingComponentException $e) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/Deprecator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use craft\elements\db\ElementQuery;
use craft\errors\DeprecationException;
use craft\helpers\Db;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\helpers\Template;
use craft\models\DeprecationError;
Expand Down Expand Up @@ -139,6 +138,7 @@ private function _storeLogsInDb(): bool
public function storeLogs(): void
{
$db = Craft::$app->getDb();
$tableSchema = $db->getSchema()->getTableSchema(Table::DEPRECATIONERRORS);

foreach ($this->_pendingRequestLogs as $log) {
try {
Expand All @@ -149,7 +149,7 @@ public function storeLogs(): void
'file' => $log->file,
'line' => $log->line,
'message' => $log->message,
'traces' => $log->traces,
'traces' => Db::prepareValueForDb($log->traces, $tableSchema->columns['traces']->dbType),
]);
$log->id = (int)$db->getLastInsertID();
} catch (Exception $e) {
Expand Down
6 changes: 2 additions & 4 deletions src/services/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,8 @@ private function _sections(): MemoizableArray
}

$this->_sections = new MemoizableArray($results, function(array $result) use (&$siteSettingsBySection) {
if (!empty($result['previewTargets'])) {
$result['previewTargets'] = is_string($result['previewTargets'])
? Json::decode($result['previewTargets'])
: $result['previewTargets'];
if (!empty($result['previewTargets']) && is_string($result['previewTargets'])) {
$result['previewTargets'] = Json::decode($result['previewTargets']);
} else {
$result['previewTargets'] = [];
}
Expand Down
3 changes: 1 addition & 2 deletions src/services/Gql.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
use craft\helpers\DateTimeHelper;
use craft\helpers\Db;
use craft\helpers\Gql as GqlHelper;
use craft\helpers\Json;
use craft\helpers\ProjectConfig as ProjectConfigHelper;
use craft\helpers\StringHelper;
use craft\models\FieldLayout;
Expand Down Expand Up @@ -992,7 +991,7 @@ public function handleChangedSchema(ConfigEvent $event): void
$schemaRecord->uid = $schemaUid;
$schemaRecord->name = $data['name'];
$schemaRecord->isPublic = (bool)($data['isPublic'] ?? false);
$schemaRecord->scope = empty($data['scope']) ? [] : $data['scope'];
$schemaRecord->scope = ($data['scope'] ?? false) ?: [];

// Save the schema record
$schemaRecord->save(false);
Expand Down
17 changes: 12 additions & 5 deletions src/services/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,17 @@ public function getUserPreferences(int $userId): array
->select(['preferences'])
->from([Table::USERPREFERENCES])
->where(['userId' => $userId])
->scalar() ?: [];
->scalar();

$this->_userPreferences[$userId] = is_string($preferences)
? Json::decode($preferences)
: $preferences;
if ($preferences) {
if (is_string($preferences)) {
$preferences = Json::decode($preferences);
}
} else {
$preferences = [];
}

$this->_userPreferences[$userId] = $preferences;
}

return $this->_userPreferences[$userId];
Expand All @@ -389,10 +395,11 @@ public function saveUserPreferences(User $user, array $preferences): void
{
// Merge in any other saved preferences
$preferences += $this->getUserPreferences($user->id);
$tableSchema = Craft::$app->getDb()->getSchema()->getTableSchema(Table::USERPREFERENCES);

Db::upsert(Table::USERPREFERENCES, [
'userId' => $user->id,
'preferences' => $preferences,
'preferences' => Db::prepareValueForDb($preferences, $tableSchema->columns['preferences']->dbType),
]);

$this->_userPreferences[$user->id] = $preferences;
Expand Down

0 comments on commit 585a131

Please sign in to comment.