Skip to content

Commit

Permalink
Reduce file size of Collector/MySqli & Route/Wamp
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Jul 23, 2024
1 parent c3327d4 commit 94ada91
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 175 deletions.
40 changes: 40 additions & 0 deletions src/Debug/Collector/DatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ trait DatabaseTrait
/** @var list<StatementInfo> */
protected $loggedStatements = array();

/** @var string */
protected $icon = 'fa fa-database';

/**
* Logs StatementInfo
*
Expand Down Expand Up @@ -95,6 +98,43 @@ private function logRuntime(Debug $debug)
}
}

/**
* Call debug method with styling
*
* Replace/wrap %c with style
*
* @param string $method Debug method
* @param string $message Log message
*
* @return void
*/
protected function logWithStyling($method, $message)
{
$params = array(
$message,
);
$cCount = \substr_count($params[0], '%c');
for ($i = 0; $i < $cCount; $i += 2) {
$params[] = 'font-family:monospace;';
$params[] = '';
}
\call_user_func_array(array($this->debug, $method), $params);
}

/**
* Get meta argument
*
* @param array $values Values to metafy
*
* @return array
*/
protected function meta(array $values = array())
{
return $this->debug->meta(\array_merge(array(
'icon' => $this->debug->getCfg('channelIcon', Debug::CONFIG_DEBUG),
), $values));
}

/**
* Were attempts to prettify successful?
*
Expand Down
6 changes: 1 addition & 5 deletions src/Debug/Collector/DoctrineLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ class DoctrineLogger implements SQLLogger
{
use DatabaseTrait;

/** @var string */
protected $icon = 'fa fa-database';

/** @var StatementInfo|null */
protected $statementInfo;

Expand Down Expand Up @@ -81,9 +78,8 @@ public function onDebugOutput(Event $event)
$connectionInfo
? $connectionInfo['url']
: null,
$debug->meta(array(
$this->meta(array(
'argsAsParams' => false,
'icon' => $this->icon,
'level' => 'info',
)),
));
Expand Down
139 changes: 43 additions & 96 deletions src/Debug/Collector/MySqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class MySqli extends mysqliBase
/** @var bool */
protected $autocommit = true;

/** @var string */
protected $icon = 'fa fa-database';

/** @var list<string> */
protected $savePoints = array();

Expand All @@ -62,20 +59,11 @@ class MySqli extends mysqliBase
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null, Debug $debug = null)
public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null, Debug $debug = null) // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
{
$params = \func_num_args()
// @phpcs:ignore SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
? array(
'host' => $host,
'username' => $username,
'passwd' => $passwd,
'dbname' => $dbname,
'port' => $port,
'socket' => $socket,
)
: array();
$this->doConstruct($params);
$this->doConstruct(\func_num_args()
? \array_slice(\func_get_args(), 0, 6)
: array());
if (!$debug) {
$debug = Debug::getChannel('MySqli', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
Expand All @@ -87,11 +75,7 @@ public function __construct($host = null, $username = null, $passwd = null, $dbn
}

/**
* Turns on or off auto-committing database modification (begin transaction)
*
* @param bool $mode Whether to turn on auto-commit or not.
*
* @return bool
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function autocommit($mode)
Expand All @@ -102,48 +86,34 @@ public function autocommit($mode)
}

/**
* Initiates a transaction
*
* @param int $flags A bitmask of MYSQLI_TRANS_START_* constants
* @param string $name Savepoint name for the transaction
*
* @return bool
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function begin_transaction($flags = 0, $name = null)
{
// name became nullable as of PHP 8
$return = $name === null
? parent::begin_transaction($flags)
: parent::begin_transaction($flags, $name);
if ($return === false) {
$this->debug->warn($this->error);
return $return;
}
$this->savePoints = $name
$this->savePoints = $name !== null
? array($name)
: array();
$debugArgs = \array_filter(array(
'begin_transaction',
$name,
$this->debug->meta(array(
'icon' => $this->debug->getCfg('channelIcon', Debug::CONFIG_DEBUG),
)),
));
\call_user_func_array(array($this->debug, 'info'), $debugArgs);
$infoParams = \array_filter(array('begin_transaction', $name, $this->meta()));
\call_user_func_array(array($this->debug, 'info'), $infoParams);
return $return;
}

/**
* Commits the current transaction
*
* @param int $flags A bitmask of MYSQLI_TRANS_COR_* constants
* @param string $name If provided then COMMIT/name/ is executed.
*
* @return bool
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function commit($flags = 0, $name = null)
{
// name became nullable as of PHP 8
$return = $name === null
? parent::commit($flags)
: parent::commit($flags, $name);
Expand All @@ -155,12 +125,12 @@ public function commit($flags = 0, $name = null)
if ($name !== null) {
$this->debug->warn('passing $name param to mysqli::commit() does nothing!');
}
$this->debug->info('commit', $this->debug->meta(array(
'icon' => $this->debug->getCfg('channelIcon', Debug::CONFIG_DEBUG),
)));
$this->debug->info('commit', $this->meta());
return $return;
}

// execute_query defined in ExecuteQueryTrait

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -214,18 +184,14 @@ public function real_query($query)
public function release_savepoint($name)
{
$return = parent::release_savepoint($name);
$index = \array_search($name, $this->savePoints, true);
if (PHP_VERSION_ID < 70000) {
$this->debug->warn(
'mysqli::release_savepoint on PHP < 7.0 just calls %cSAVEPOINT `Sally`%c',
'font-family: monospace;',
''
);
$this->logWithStyling('warn', 'mysqli::release_savepoint on PHP < 7.0 just calls %cSAVEPOINT `Sally`%c');
}
if ($return === false) {
$this->debug->warn($this->error);
return $return;
}
$index = \array_search($name, $this->savePoints, true);
if ($index !== false) {
unset($this->savePoints[$index]);
$this->savePoints = \array_values($this->savePoints);
Expand All @@ -237,16 +203,12 @@ public function release_savepoint($name)
}

/**
* Rolls back current transaction
*
* @param int $flags A bitmask of MYSQLI_TRANS_COR_* constants.
* @param string $name If provided then ROLLBACK /name/ is executed.
*
* @return bool
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function rollBack($flags = 0, $name = null)
{
// name became nullable as of PHP 8
$return = $name === null
? parent::rollback($flags)
: parent::rollback($flags, $name);
Expand All @@ -256,17 +218,9 @@ public function rollBack($flags = 0, $name = null)
}
$this->savePoints = array();
if ($name !== null) {
$this->debug->warn(
'passing $name param to %cmysqli::rollback()%c does not %cROLLBACK TO name%c as you would expect!',
'font-family: monospace;',
'',
'font-family: monospace;',
''
);
$this->logWithStyling('warn', 'passing $name param to %cmysqli::rollback()%c does not %cROLLBACK TO name%c as you would expect!');
}
$this->debug->info('rollBack', $this->debug->meta(array(
'icon' => $this->debug->getCfg('channelIcon', Debug::CONFIG_DEBUG),
)));
$this->debug->info('rollBack', $this->meta());
return $return;
}

Expand Down Expand Up @@ -310,28 +264,26 @@ public function onDebugOutput(Event $event)
{
$debug = $event->getSubject();
$debug->groupSummary(0);
$debug->groupCollapsed(
'MySqli info',
$this->host_info,
$this->meta(array(
'argsAsParams' => false,
'level' => 'info',
))
);
\set_error_handler(static function ($errno, $errstr) {
throw new RuntimeException($errstr, $errno);
}, E_ALL);
try {
$debug->groupCollapsed(
'MySqli info',
$this->host_info,
$debug->meta(array(
'argsAsParams' => false,
'icon' => $this->icon,
'level' => 'info',
))
);
$this->logRuntime($debug);
$debug->groupEnd(); // groupCollapsed
} catch (RuntimeException $e) {
$debug->group('MySqli Error', $debug->meta(array('level' => 'error')));
$debug->log('Connection Error');
$debug->groupEnd(); // MySqli Error
$debug->groupEnd(); // groupCollapsed (opened in try)
$debug->groupEnd();
}
\restore_error_handler();
$debug->groupEnd(); // groupCollapsed
$debug->groupEnd(); // groupSummary
}

Expand All @@ -345,18 +297,19 @@ public function onDebugOutput(Event $event)
private function currentDatabase()
{
$result = parent::query('select database() as `database`');
if ($result instanceof \mysqli_result) {
$row = $result->fetch_assoc();
if ($row) {
return $row['database'];
}
}
return null;
$row = $result instanceof \mysqli_result
? $result->fetch_assoc()
: null;
return $row
? $row['database']
: null;
}

/**
* Call mysqli constructor with appropriate params
*
* Default values will be used for all empty values
*
* @param array $params host, username, etc
*
* @return void
Expand All @@ -379,17 +332,11 @@ private function doConstruct($params)
'port' => \ini_get('mysqli.default_port'),
'socket' => \ini_get('mysqli.default_socket'),
);
$params = \array_filter($params);
$params = \array_merge($paramsDefault, $params);
$params = \array_replace(\array_fill(0, \count($paramsDefault), null), $params);
$params = \array_combine(\array_keys($paramsDefault), $params);
$p = \array_merge($paramsDefault, \array_filter($params));
$this->connectionAttempted = true;
parent::__construct(
$params['host'],
$params['username'],
$params['passwd'],
$params['dbname'],
$params['port'],
$params['socket']
);
parent::__construct($p['host'], $p['username'], $p['passwd'], $p['dbname'], $p['port'], $p['socket']);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Debug/Collector/MySqli/ExecuteQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
*/
trait ExecuteQueryTrait
{
// execute_query did not exist in PHP < 8.2
}
}
Loading

0 comments on commit 94ada91

Please sign in to comment.