├── phpstan.neon ├── CHANGELOG.md ├── ecs.php ├── src ├── translations │ └── en │ │ └── twig-profiler.php ├── config.php ├── twigextensions │ ├── ProfilerTwigExtension.php │ ├── ProfilerNode.php │ └── ProfilerTokenParser.php ├── models │ └── Settings.php ├── services │ ├── ServicesTrait.php │ └── Profile.php ├── icon.svg └── TwigProfiler.php ├── LICENSE.md ├── composer.json └── README.md /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon 3 | 4 | parameters: 5 | level: 5 6 | paths: 7 | - src 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Twig Profiler Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | ## 5.0.0 - 2024.04.27 6 | ### Added 7 | * Stable release for Craft CMS 5 8 | -------------------------------------------------------------------------------- /ecs.php: -------------------------------------------------------------------------------- 1 | paths([ 8 | __DIR__ . '/src', 9 | __FILE__, 10 | ]); 11 | $ecsConfig->parallel(); 12 | $ecsConfig->sets([SetList::CRAFT_CMS_4]); 13 | }; 14 | -------------------------------------------------------------------------------- /src/translations/en/twig-profiler.php: -------------------------------------------------------------------------------- 1 | '{name} plugin loaded', 19 | ]; 20 | -------------------------------------------------------------------------------- /src/config.php: -------------------------------------------------------------------------------- 1 | true, 30 | ]; 31 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) nystudio107 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /src/twigextensions/ProfilerTwigExtension.php: -------------------------------------------------------------------------------- 1 | [ 36 | 'profile' => ProfileService::class, 37 | ], 38 | ]; 39 | } 40 | 41 | // Public Methods 42 | // ========================================================================= 43 | 44 | /** 45 | * Returns the profile service 46 | * 47 | * @return ProfileService The profile service 48 | * @throws InvalidConfigException 49 | */ 50 | public function getProfile(): ProfileService 51 | { 52 | return $this->get('profile'); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 16 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/twigextensions/ProfilerNode.php: -------------------------------------------------------------------------------- 1 | getNode('profile'); 36 | if ($profileName !== null) { 37 | $profileName = $profileName->attributes['value'] ?? ''; 38 | if (!empty($profileName)) { 39 | $compiler 40 | ->addDebugInfo($this) 41 | ->write(TwigProfiler::class . "::\$plugin->profile->begin('" . $profileName . "');\n") 42 | ->indent() 43 | ->subcompile($this->getNode('body')) 44 | ->outdent() 45 | ->write(TwigProfiler::class . "::\$plugin->profile->end('" . $profileName . "');\n"); 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/services/Profile.php: -------------------------------------------------------------------------------- 1 | getLine(); 43 | $stream = $this->parser->getStream(); 44 | $nodes = [ 45 | 'profile' => $this->parser->getExpressionParser()->parseExpression(), 46 | ]; 47 | $stream->expect(Token::BLOCK_END_TYPE); 48 | $nodes['body'] = $this->parser->subparse(fn(Token $token): bool => $this->decideProfilerEnd($token), true); 49 | $stream->expect(Token::BLOCK_END_TYPE); 50 | 51 | return new ProfilerNode($nodes, [], $lineno, $this->getTag()); 52 | } 53 | 54 | public function decideProfilerEnd(Token $token): bool 55 | { 56 | return $token->test('endprofile'); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-twigprofiler/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-twigprofiler/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-twigprofiler/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-twigprofiler/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-twigprofiler/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-twigprofiler/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-twigprofiler/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence) 2 | 3 | # Twig Profiler plugin for Craft CMS 5.x 4 | 5 | Twig Profiler allows you to profile sections of your Twig templates, and see the resulting timings in the Yii2 Debug Toolbar 6 | 7 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 8 | 9 | ## Requirements 10 | 11 | This plugin requires Craft CMS 5.0.0 or later. 12 | 13 | ## Installation 14 | 15 | To install the plugin, follow these instructions. 16 | 17 | 1. Open your terminal and go to your Craft project: 18 | 19 | cd /path/to/project 20 | 21 | 2. Then tell Composer to load the plugin: 22 | 23 | composer require nystudio107/craft-twigprofiler 24 | 25 | 3. Install the plugin via `./craft install/plugin twig-profiler` via the CLI, or in the Control Panel, go to Settings → Plugins and click the “Install” button for Twig Profiler. 26 | 27 | Or you can install the plugin via the **Plugin Store** in the Craft CMS Control Panel. 28 | 29 | ## Documentation 30 | 31 | Click here -> [Twig Profiler Documentation](https://nystudio107.com/plugins/twig-profiler/documentation) 32 | 33 | ## Twig Profiler Roadmap 34 | 35 | Some things to do, and ideas for potential features: 36 | 37 | * Release it 38 | 39 | Brought to you by [nystudio107](https://nystudio107.com/) 40 | -------------------------------------------------------------------------------- /src/TwigProfiler.php: -------------------------------------------------------------------------------- 1 | getSettings(); 81 | Craft::$app->view->registerTwigExtension(new ProfilerTwigExtension()); 82 | 83 | if ($settings !== null && $settings->appendTemplateName) { 84 | // Handler: View::EVENT_BEFORE_RENDER_TEMPLATE 85 | Event::on( 86 | View::class, 87 | View::EVENT_BEFORE_RENDER_TEMPLATE, 88 | static function(TemplateEvent $event): void { 89 | Craft::debug( 90 | 'View::EVENT_BEFORE_RENDER_TEMPLATE', 91 | __METHOD__ 92 | ); 93 | self::$renderingTemplate = ' - ' . $event->template; 94 | } 95 | ); 96 | } 97 | 98 | Craft::info( 99 | Craft::t( 100 | 'twig-profiler', 101 | '{name} plugin loaded', 102 | ['name' => $this->name] 103 | ), 104 | __METHOD__ 105 | ); 106 | } 107 | 108 | // Protected Methods 109 | // ========================================================================= 110 | 111 | /** 112 | * @inheritdoc 113 | */ 114 | protected function createSettingsModel(): Settings 115 | { 116 | return new Settings(); 117 | } 118 | } 119 | --------------------------------------------------------------------------------