Skip to content

Commit

Permalink
additional file size reduction efforts
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Jan 16, 2024
1 parent aef0d77 commit 07e88a0
Show file tree
Hide file tree
Showing 21 changed files with 2,088 additions and 1,627 deletions.
175 changes: 175 additions & 0 deletions src/Debug/Framework/Laravel/LogDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v3.3
*/

namespace bdk\Debug\Framework\Laravel;

use bdk\Debug;
use bdk\Debug\Collector\StatementInfo;
use Exception;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\ServiceProvider;

/**
* Log database activity
*/
class LogDb
{
public $debug;

protected $app;
protected $serviceProvider;

/**
* Constructor
*
* @param ServiceProvider $serviceProvider ServiceProvider instance
* @param Application $app Laravel Application
*/
public function __construct(ServiceProvider $serviceProvider, Application $app)
{
$this->serviceProvider = $serviceProvider;
$this->app = $app;
$this->debug = $serviceProvider->debug;
}

/**
* Subscribe to database events and log them
*
* @return void
*/
public function log()
{
if (!$this->serviceProvider->shouldCollect('db', true) || !isset($this->app['db'])) {
return;
}

/** @var \Illuminate\Database\DatabaseManager */
$dbManager = $this->app['db'];

$dbChannel = $this->debug->getChannel('Db', array(
'channelIcon' => 'fa fa-database',
));

try {
$this->dbListen($dbManager, $dbChannel);
} catch (Exception $e) {
$this->debug->warn($e->getMessage());
}

try {
$this->dbSubscribe($dbManager, $dbChannel);
} catch (Exception $e) {
$this->debug->log('exception', $e->getMessage());
}
}

/**
* Build DatabaseManager event handler
*
* @param Debug $dbChannel PHPDebugConsole channel
* @param string|null $msg Group message
* @param bool $isGroupEnd (false) groupEnd ?
*
* @return Closure
*/
private function buildDbEventHandler(Debug $dbChannel, $msg, $isGroupEnd = false)
{
return static function () use ($dbChannel, $msg, $isGroupEnd) {
if ($isGroupEnd === false) {
$dbChannel->group($msg);
return;
}
if ($msg) {
$dbChannel->log($msg);
}
$dbChannel->groupEnd();
};
}

/**
* Register a database query listener with the connection.
*
* @param DatabaseManager $dbManager DatabaseManager instance
* @param Debug $dbChannel Debug instance
*
* @return void
*/
protected function dbListen(DatabaseManager $dbManager, Debug $dbChannel)
{
// listen found in Illuminate\Database\Connection
$dbManager->listen(function ($query, $bindings = null, $time = null, $connection = null) use ($dbManager, $dbChannel) {
if (!$this->serviceProvider->shouldCollect('db', true)) {
// We've turned off collecting after the listener was attached
return;
}

// Laravel 5.2 changed the way some core events worked. We must account for
// the first argument being an "event object", where arguments are passed
// via object properties, instead of individual arguments.
$connection = $query instanceof QueryExecuted
? $query->connection
: $dbManager->connection($connection);
if ($query instanceof QueryExecuted) {
$bindings = $query->bindings;
$time = $query->time;
$query = $query->sql;
}
$statementInfo = new StatementInfo(
$query,
$connection->prepareBindings($bindings)
);
$statementInfo->setDuration($time);
$statementInfo->appendLog($dbChannel);
});
}

/**
* Listen to database events
*
* @param DatabaseManager $dbManager DatabaseManager instance
* @param Debug $dbChannel Debug instance
*
* @return void
*/
private function dbSubscribe(DatabaseManager $dbManager, Debug $dbChannel)
{
$eventDispatcher = $dbManager->getEventDispatcher();

$eventDispatcher->listen(
\Illuminate\Database\Events\TransactionBeginning::class,
$this->buildDbEventHandler($dbChannel, 'Begin Transaction')
);
$eventDispatcher->listen(
\Illuminate\Database\Events\TransactionCommitted::class,
$this->buildDbEventHandler($dbChannel, null, true)
);
$eventDispatcher->listen(
\Illuminate\Database\Events\TransactionRolledBack::class,
$this->buildDbEventHandler($dbChannel, 'rollback', true)
);

$eventDispatcher->listen(
'connection.*.beganTransaction',
$this->buildDbEventHandler($dbChannel, 'Begin Transaction')
);
$eventDispatcher->listen(
'connection.*.committed',
$this->buildDbEventHandler($dbChannel, null, true)
);
$eventDispatcher->listen(
'connection.*.rollingBack',
$this->buildDbEventHandler($dbChannel, 'rollback', true)
);
}
}
139 changes: 139 additions & 0 deletions src/Debug/Framework/Laravel/LogViews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v3.3
*/

namespace bdk\Debug\Framework\Laravel;

use bdk\Debug\Abstraction\Type;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View;

/**
* Log views
*/
class LogViews
{
public $debug;

protected $app;
protected $serviceProvider;
protected $viewChannel;

/**
* Constructor
*
* @param ServiceProvider $serviceProvider ServiceProvider instance
* @param Application $app Laravel Application
*/
public function __construct(ServiceProvider $serviceProvider, Application $app)
{
$this->serviceProvider = $serviceProvider;
$this->app = $app;
$this->debug = $serviceProvider->debug;
}

/**
* Log views
*
* @return void
*/
public function log()
{
if (!$this->serviceProvider->shouldCollect('laravel', true)) {
return;
}
$this->viewChannel = $this->debug->getChannel('Views', array(
'channelIcon' => 'fa fa-file-text-o',
));
$this->app['events']->listen(
'composing:*',
function ($view, $data = []) {
if ($data) {
$view = $data[0]; // For Laravel >= 5.4
}
$this->logView($view);
}
);
}

/**
* Log view information
*
* @param View $view View instance
*
* @return void
*/
protected function logView(View $view)
{
$name = $view->getName();
$path = $view->getPath();
$pathStr = \is_object($path)
? null
: \realpath($path);

$info = \array_filter(array(
'name' => $name,
'params' => \call_user_func(array($this, 'logViewParams'), $view),
'path' => $pathStr
? $this->debug->abstracter->crateWithVals(
\ltrim(\str_replace(\base_path(), '', $pathStr), '/'),
array(
'attribs' => array(
'data-file' => $path,
),
)
)
: null,
'type' => \is_object($path)
? \get_class($view)
: (\substr($path, -10) === '.blade.php'
? 'blade'
: \pathinfo($path, PATHINFO_EXTENSION)),
));
$this->viewChannel->log('view', $info, $this->viewChannel->meta('detectFiles'));
}

/**
* Get view params (view data)
*
* @param View $view View instance
*
* @return array
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function logViewParams(View $view)
{
$data = $view->getData();
/** @var bool|'type' */
$collectValues = $this->app['config']->get('phpDebugConsole.options.views.data');
if ($collectValues === true) {
\ksort($data);
return $data;
}
if ($collectValues !== 'type') {
$data = \array_keys($data);
\sort($data);
return $data;
}
foreach ($data as $k => $v) {
$type = $this->debug->abstracter->type->getType($v)[0];
$data[$k] = $type === 'object'
? $this->debug->abstracter->crateWithVals(\get_class($v), array(
'typeMore' => Type::TYPE_STRING_CLASSNAME,
))
: $type;
}
\ksort($data);
return $data;
}
}

0 comments on commit 07e88a0

Please sign in to comment.