Skip to content

Commit

Permalink
refactor: Code cleanup for Craft CMS 4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 14, 2022
1 parent 5f8a3bf commit ada28d8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
47 changes: 34 additions & 13 deletions src/TwigProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

namespace nystudio107\twigprofiler;

use nystudio107\twigprofiler\models\Settings;
use nystudio107\twigprofiler\services\Profile as ProfileService;
use nystudio107\twigprofiler\twigextensions\ProfilerTwigExtension;

use Craft;
use craft\base\Plugin;
use craft\events\TemplateEvent;
use craft\web\View;

use nystudio107\twigprofiler\models\Settings;
use nystudio107\twigprofiler\services\Profile as ProfileService;
use nystudio107\twigprofiler\twigextensions\ProfilerTwigExtension;
use yii\base\Event;

/**
Expand All @@ -33,18 +31,18 @@
*/
class TwigProfiler extends Plugin
{
// Static Properties
// Public Static Properties
// =========================================================================

/**
* @var TwigProfiler
* @var ?TwigProfiler
*/
public static $plugin;
public static ?TwigProfiler $plugin;

/**
* @var string The name of the rendering template
*/
public static $renderingTemplate = '';
public static string $renderingTemplate = '';

// Public Properties
// =========================================================================
Expand All @@ -54,9 +52,31 @@ class TwigProfiler 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 __construct($id, $parent = null, array $config = [])
{
$config['components'] = [
'profile' => ProfileService::class,
];

parent::__construct($id, $parent, $config);
}

/**
* @inheritdoc
*/
Expand All @@ -65,20 +85,21 @@ public function init(): void
parent::init();
self::$plugin = $this;

/* @var Settings $settings */
$settings = $this->getSettings();
Craft::$app->view->registerTwigExtension(new ProfilerTwigExtension());

if ($settings->appendTemplateName) {
if ($settings !== null && $settings->appendTemplateName) {
// Handler: View::EVENT_BEFORE_RENDER_TEMPLATE
Event::on(
View::class,
View::EVENT_BEFORE_RENDER_TEMPLATE,
function (TemplateEvent $event): void {
static function (TemplateEvent $event): void {
Craft::debug(
'View::EVENT_BEFORE_RENDER_TEMPLATE',
__METHOD__
);
self::$renderingTemplate = ' - '.$event->template;
self::$renderingTemplate = ' - ' . $event->template;
}
);
}
Expand All @@ -99,7 +120,7 @@ function (TemplateEvent $event): void {
/**
* @inheritdoc
*/
protected function createSettingsModel(): \nystudio107\twigprofiler\models\Settings
protected function createSettingsModel(): Settings
{
return new Settings();
}
Expand Down
5 changes: 1 addition & 4 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

namespace nystudio107\twigprofiler\models;

use nystudio107\twigprofiler\TwigProfiler;

use Craft;
use craft\base\Model;

/**
Expand All @@ -30,7 +27,7 @@ class Settings extends Model
* @var bool Controls whether to append the template name to the profile
* category
*/
public $appendTemplateName = true;
public bool $appendTemplateName = true;

// Public Methods
// =========================================================================
Expand Down
1 change: 0 additions & 1 deletion src/translations/en/twig-profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
*/
return [
'{name} plugin loaded' => '{name} plugin loaded',
'Twig Profiler' => 'Twig Profiler',
];
5 changes: 2 additions & 3 deletions src/twigextensions/ProfilerNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace nystudio107\twigprofiler\twigextensions;

use nystudio107\twigprofiler\TwigProfiler;

use Twig\Compiler;
use Twig\Node\Node;

Expand All @@ -39,11 +38,11 @@ public function compile(Compiler $compiler): void
if (!empty($profileName)) {
$compiler
->addDebugInfo($this)
->write(TwigProfiler::class."::\$plugin->profile->begin('".$profileName."');\n")
->write(TwigProfiler::class . "::\$plugin->profile->begin('" . $profileName . "');\n")
->indent()
->subcompile($this->getNode('body'))
->outdent()
->write(TwigProfiler::class."::\$plugin->profile->end('".$profileName."');\n");
->write(TwigProfiler::class . "::\$plugin->profile->end('" . $profileName . "');\n");
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/twigextensions/ProfilerTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,20 @@ public function getTag(): string
/**
* @inheritdoc
*/
public function parse(Token $token): \nystudio107\twigprofiler\twigextensions\ProfilerNode
public function parse(Token $token): ProfilerNode
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$nodes = [
'profile' => $this->parser->getExpressionParser()->parseExpression(),
];
$stream->expect(Token::BLOCK_END_TYPE);
$nodes['body'] = $this->parser->subparse(fn(\Twig\Token $token): bool => $this->decideProfilerEnd($token), true);
$nodes['body'] = $this->parser->subparse(fn(Token $token): bool => $this->decideProfilerEnd($token), true);
$stream->expect(Token::BLOCK_END_TYPE);

return new ProfilerNode($nodes, [], $lineno, $this->getTag());
}


public function decideProfilerEnd(Token $token): bool
{
return $token->test('endprofile');
Expand Down

0 comments on commit ada28d8

Please sign in to comment.