Skip to content

Commit

Permalink
improve devbuild extension
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Feb 9, 2024
1 parent 1d0e04c commit 652cf6e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 25 deletions.
8 changes: 5 additions & 3 deletions src/Benchmark.php
Expand Up @@ -4,8 +4,6 @@

use Psr\Log\LoggerInterface;
use LeKoala\Base\Helpers\ClassHelper;
use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;

class Benchmark
Expand Down Expand Up @@ -115,6 +113,10 @@ public static function log(string $name, $cb = null): void
$time = $data['time'];
$memory = $data['memory'];

self::getLogger()->debug("$name : $time seconds | $memory memory.", [$requestUri]);
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$line = $bt[1]['line'] ?? 0;
$file = basename($bt[1]['file'] ?? "unknown");

self::getLogger()->debug("$name : $time seconds | $memory memory.", [$requestUri, "$file:$line"]);
}
}
17 changes: 10 additions & 7 deletions src/BetterDebugView.php
Expand Up @@ -10,7 +10,6 @@
use SilverStripe\Core\ClassInfo;
use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\Connect\DatabaseException;
use LeKoala\Base\Helpers\DatabaseHelper;

Expand All @@ -24,7 +23,7 @@ class BetterDebugView extends DebugView

/**
* @param string $file
* @param string $line
* @param string|int $line
* @return string
*/
public static function makeIdeLink($file, $line)
Expand Down Expand Up @@ -53,9 +52,9 @@ public static function makeIdeLink($file, $line)
* Similar to renderVariable() but respects debug() method on object if available
*
* @param mixed $val
* @param array $caller
* @param array<string,mixed> $caller
* @param bool $showHeader
* @param int $argumentIndex
* @param int|null $argumentIndex
* @return string
*/
public function debugVariable($val, $caller, $showHeader = true, $argumentIndex = 0)
Expand Down Expand Up @@ -92,7 +91,7 @@ public function debugVariable($val, $caller, $showHeader = true, $argumentIndex
/**
* @param string $file
* @param int $line
* @return array
* @return array<mixed>
*/
protected function extractArgumentsName($file, $line)
{
Expand Down Expand Up @@ -214,6 +213,10 @@ public function renderError($httpRequest, $errno, $errstr, $errfile, $errline)
return $output;
}

/**
* @param Exception $exception
* @return void
*/
public function writeException(Exception $exception)
{
$infos = self::makeIdeLink($exception->getFile(), $exception->getLine());
Expand Down Expand Up @@ -242,7 +245,7 @@ public function writeException(Exception $exception)
/**
* Render a call track
*
* @param array $trace The debug_backtrace() array
* @param array<mixed> $trace The debug_backtrace() array
* @return string
*/
public function renderTrace($trace)
Expand All @@ -258,7 +261,7 @@ public function renderTrace($trace)
/**
* Render a backtrace array into an appropriate plain-text or HTML string.
*
* @param array $bt The trace array, as returned by debug_backtrace() or Exception::getTrace()
* @param array<string,mixed> $bt The trace array, as returned by debug_backtrace() or Exception::getTrace()
* @return string The rendered backtrace
*/
public static function get_rendered_backtrace($bt)
Expand Down
64 changes: 49 additions & 15 deletions src/Extensions/DevBuildExtension.php
Expand Up @@ -10,6 +10,8 @@
use SilverStripe\Core\Extension;
use SilverStripe\ORM\DataObject;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use LeKoala\DevToolkit\Helpers\DevUtils;
use LeKoala\DevToolkit\Helpers\FileHelper;
use LeKoala\DevToolkit\Helpers\SubsiteHelper;

Expand All @@ -26,39 +28,73 @@
*
* Preserve current subsite
*
* @property \SilverStripe\Dev\DevBuildController|\LeKoala\DevToolkit\Extensions\DevBuildExtension $owner
* @property \SilverStripe\Dev\DevBuildController $owner
*/
class DevBuildExtension extends Extension
{
/**
* @var \SilverStripe\Subsites\Model\Subsite|null
*/
protected $currentSubsite;

/**
* @return \SilverStripe\Dev\DevBuildController
*/
public function getExtensionOwner()
{
return $this->owner;
}

/**
* @return HTTPRequest
*/
public function getRequest()
{
return $this->getExtensionOwner()->getRequest();
}

/**
* @return void
*/
public function beforeCallActionHandler()
{
$this->currentSubsite = SubsiteHelper::currentSubsiteID();

$renameColumns = $this->owner->getRequest()->getVar('fixTableCase');
$annotate = $this->getRequest()->getVar('annotate');
if ($annotate) {
\SilverLeague\IDEAnnotator\DataObjectAnnotator::config()->enabled = true;
\SilverLeague\IDEAnnotator\DataObjectAnnotator::config()->enabled_modules = ['app'];
}

$renameColumns = $this->getRequest()->getVar('fixTableCase');
if ($renameColumns) {
$this->displayMessage("<div class='build'><p><b>Fixing tables case</b></p><ul>\n\n");
$this->fixTableCase();
$this->displayMessage("</ul>\n<p><b>Tables fixed!</b></p></div>");
}

$renameColumns = $this->owner->getRequest()->getVar('renameColumns');
$renameColumns = $this->getRequest()->getVar('renameColumns');
if ($renameColumns) {
$this->displayMessage("<div class='build'><p><b>Renaming columns</b></p><ul>\n\n");
$this->renameColumns();
$this->displayMessage("</ul>\n<p><b>Columns renamed!</b></p></div>");
}

$truncateSiteTree = $this->owner->getRequest()->getVar('truncateSiteTree');
$truncateSiteTree = $this->getRequest()->getVar('truncateSiteTree');
if ($truncateSiteTree) {
$this->displayMessage("<div class='build'><p><b>Truncating SiteTree</b></p><ul>\n\n");
$this->truncateSiteTree();
$this->displayMessage("</ul>\n<p><b>SiteTree truncated!</b></p></div>");
}

// Reverse the logic, don't populate by default
DevUtils::updatePropCb($this->getRequest(), 'getVars', function ($arr) {
$arr['dont_populate'] = !!$this->getRequest()->getVar('populate');
return $arr;
});
}

protected function fixTableCase()
protected function fixTableCase(): void
{
if (!Director::isDev()) {
throw new Exception("Only available in dev mode");
Expand All @@ -74,7 +110,7 @@ protected function fixTableCase()
//TODO: check list of tables name and match any lowercased one to the right one from the db schema
}

protected function truncateSiteTree()
protected function truncateSiteTree(): void
{
if (!Director::isDev()) {
throw new Exception("Only available in dev mode");
Expand All @@ -101,10 +137,8 @@ protected function truncateSiteTree()
* Loop on all DataObjects and look for rename_columns property
*
* It will rename old columns from old_value => new_value
*
* @return void
*/
protected function renameColumns()
protected function renameColumns(): void
{
$classes = $this->getDataObjects();

Expand Down Expand Up @@ -157,7 +191,7 @@ protected function renameColumns()
}
}

public function afterCallActionHandler()
public function afterCallActionHandler(): void
{
// Other helpers
$clearCache = $this->owner->getRequest()->getVar('clearCache');
Expand Down Expand Up @@ -189,7 +223,7 @@ public function afterCallActionHandler()
}
}

protected function clearCache()
protected function clearCache(): void
{
$this->displayMessage("<strong>Clearing cache folder</strong>");
$folder = Director::baseFolder() . '/silverstripe-cache';
Expand All @@ -202,7 +236,7 @@ protected function clearCache()
$this->displayMessage("Cleared silverstripe-cache folder\n");
}

protected function clearEmptyFolders()
protected function clearEmptyFolders(): void
{
$this->displayMessage("<strong>Clearing empty folders in assets</strong>");
$folder = Director::publicFolder() . '/assets';
Expand All @@ -228,7 +262,7 @@ protected function clearEmptyFolders()
}

/**
* @return array
* @return array<string>
*/
protected function getDataObjects()
{
Expand All @@ -238,9 +272,9 @@ protected function getDataObjects()
}

/**
* @param $message
* @param string $message
*/
protected function displayMessage($message)
protected function displayMessage($message): void
{
echo Director::is_cli() ? strip_tags($message) : nl2br($message);
}
Expand Down
49 changes: 49 additions & 0 deletions src/Helpers/DevUtils.php
@@ -0,0 +1,49 @@
<?php

namespace LeKoala\DevToolkit\Helpers;

use ReflectionObject;

class DevUtils
{
/**
* @param object $obj
* @param string $prop
* @param mixed $val
* @return void
*/
public static function updateProp(object $obj, string $prop, $val): void
{
$refObject = new ReflectionObject($obj);
$refProperty = $refObject->getProperty($prop);
$refProperty->setAccessible(true);
$refProperty->setValue($obj, $val);
}

/**
* @param object $obj
* @param string $prop
* @param callable $cb
* @return void
*/
public static function updatePropCb(object $obj, string $prop, callable $cb): void
{
$refObject = new ReflectionObject($obj);
$refProperty = $refObject->getProperty($prop);
$refProperty->setAccessible(true);
$refProperty->setValue($obj, $cb($refProperty->getValue($obj)));
}

/**
* @param object $obj
* @param string $prop
* @return mixed
*/
public static function getProp(object $obj, string $prop)
{
$refObject = new ReflectionObject($obj);
$refProperty = $refObject->getProperty($prop);
$refProperty->setAccessible(true);
return $refProperty->getValue($obj);
}
}
6 changes: 6 additions & 0 deletions src/Helpers/DuplicateMembersMerger.php
Expand Up @@ -4,6 +4,7 @@

use Exception;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataList;
use SilverStripe\Core\ClassInfo;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
Expand All @@ -17,6 +18,11 @@
*/
class DuplicateMembersMerger
{

/**
* @param DataList $records
* @return void
*/
public static function merge($records)
{
$all = array();
Expand Down

0 comments on commit 652cf6e

Please sign in to comment.