Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
refactor: Code cleanup for Craft CMS 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed Mar 25, 2022
1 parent 6659e45 commit 074c26a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
28 changes: 17 additions & 11 deletions src/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

namespace nystudio107\connect;

use nystudio107\connect\models\Settings;
use nystudio107\connect\variables\ConnectVariable;

use Craft;
use craft\base\Plugin;
use craft\web\twig\variables\CraftVariable;

use nystudio107\connect\models\Settings;
use nystudio107\connect\variables\ConnectVariable;
use yii\base\Event;

/**
Expand All @@ -32,9 +30,9 @@ class Connect extends Plugin
// =========================================================================

/**
* @var Connect
* @var ?Connect
*/
public static $plugin;
public static ?Connect $plugin = null;

// Public Properties
// =========================================================================
Expand All @@ -44,27 +42,35 @@ class Connect extends Plugin
*/
public string $schemaVersion = '1.0.0';

/**
* @var bool
*/
public bool $hasCpSection = false;

/**
* @var bool
*/
public bool $hasCpSettings = false;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function init()
public function init(): void
{
parent::init();
self::$plugin = $this;

Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
static function (Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('connect', ConnectVariable::class);
}
);

Craft::info(
Craft::t(
'connect',
Expand All @@ -81,7 +87,7 @@ function (Event $event) {
/**
* @inheritdoc
*/
protected function createSettingsModel(): ?\craft\base\Model
protected function createSettingsModel(): Settings
{
return new Settings();
}
Expand Down
6 changes: 3 additions & 3 deletions src/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Query extends \yii\db\Query
// =========================================================================

/**
* @var Connection the database connections
* @var ?Connection the database connections
*/
public $db = null;
public ?Connection $db = null;

// Public Methods
// =========================================================================
Expand All @@ -37,7 +37,7 @@ public function createCommand($db = null)
{
return parent::createCommand($db ?? $this->db);
}

/**
* @inheritdoc
*/
Expand Down
2 changes: 1 addition & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Settings extends Model
/**
* @var array the database connections
*/
public $connections = [];
public array $connections = [];

// Public Methods
// =========================================================================
Expand Down
18 changes: 8 additions & 10 deletions src/variables/ConnectVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

namespace nystudio107\connect\variables;

use Craft;
use nystudio107\connect\Connect;
use nystudio107\connect\db\Query;
use nystudio107\connect\models\Settings;

use Craft;

use yii\db\Connection;
use yii\db\Exception;

Expand All @@ -34,9 +32,9 @@ class ConnectVariable
*
* @param string $configName
*
* @return null|Connection
* @return ?Connection
*/
public function open(string $configName)
public function open(string $configName): ?Connection
{
$connection = null;
$config = $this->getDbConfig($configName);
Expand All @@ -56,11 +54,11 @@ public function open(string $configName)
/**
* Returns a new generic query.
*
* @param null|Connection $connection
* @param ?Connection $connection
*
* @return Query
*/
public function query($connection = null): Query
public function query(?Connection $connection = null): Query
{
return new Query([
'db' => $connection,
Expand All @@ -86,9 +84,9 @@ protected function getDbConfig(string $configName): array
$dbConnection = $settings->connections[$configName];
$config = [
'dsn' => "{$dbConnection['driver']}:"
."host={$dbConnection['server']};"
."dbname={$dbConnection['database']};"
."port={$dbConnection['port']};",
. "host={$dbConnection['server']};"
. "dbname={$dbConnection['database']};"
. "port={$dbConnection['port']};",
'username' => $dbConnection['user'],
'password' => $dbConnection['password'],
'tablePrefix' => $dbConnection['tablePrefix'],
Expand Down

0 comments on commit 074c26a

Please sign in to comment.