Skip to content

Commit

Permalink
View Editor Link (barryvdh#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdousanam authored Oct 31, 2022
1 parent ea7695f commit 7a7315f
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 1 deletion.
70 changes: 69 additions & 1 deletion src/DataCollector/ViewCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,38 @@
use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
use DebugBar\Bridge\Twig\TwigCollector;
use Illuminate\View\View;
use InvalidArgumentException;

class ViewCollector extends TwigCollector
{
protected $templates = [];
protected $collect_data;
protected $exclude_paths;

/**
* A list of known editor strings.
*
* @var array
*/
protected $editors = [
'sublime' => 'subl://open?url=file://%file&line=%line',
'textmate' => 'txmt://open?url=file://%file&line=%line',
'emacs' => 'emacs://open?url=file://%file&line=%line',
'macvim' => 'mvim://open/?url=file://%file&line=%line',
'phpstorm' => 'phpstorm://open?file=%file&line=%line',
'idea' => 'idea://open?file=%file&line=%line',
'vscode' => 'vscode://file/%file:%line',
'vscode-insiders' => 'vscode-insiders://file/%file:%line',
'vscode-remote' => 'vscode://vscode-remote/%file:%line',
'vscode-insiders-remote' => 'vscode-insiders://vscode-remote/%file:%line',
'vscodium' => 'vscodium://file/%file:%line',
'nova' => 'nova://core/open/file?filename=%file&line=%line',
'xdebug' => 'xdebug://%file@%line',
'atom' => 'atom://core/open/file?filename=%file&line=%line',
'espresso' => 'x-espresso://open?filepath=%file&lines=%line',
'netbeans' => 'netbeans://open/?f=%file:%line',
];

/**
* Create a ViewCollector
*
Expand All @@ -36,7 +61,7 @@ public function getWidgets()
return [
'views' => [
'icon' => 'leaf',
'widget' => 'PhpDebugBar.Widgets.TemplatesWidget',
'widget' => 'PhpDebugBar.Widgets.LaravelViewTemplatesWidget',
'map' => 'views',
'default' => '[]'
],
Expand All @@ -47,6 +72,36 @@ public function getWidgets()
];
}

/**
* Get the editor href for a given file and line, if available.
*
* @param string $filePath
* @param int $line
*
* @throws InvalidArgumentException If editor resolver does not return a string
*
* @return null|string
*/
protected function getEditorHref($filePath, $line)
{
if (empty(config('debugbar.editor'))) {
return null;
}

if (empty($this->editors[config('debugbar.editor')])) {
throw new InvalidArgumentException(
'Unknown editor identifier: ' . config('debugbar.editor') . '. Known editors:' .
implode(', ', array_keys($this->editors))
);
}

$filePath = $this->replaceSitesPath($filePath);

$url = str_replace(['%file', '%line'], [$filePath, $line], $this->editors[config('debugbar.editor')]);

return $url;
}

/**
* Add a View instance to the Collector
*
Expand Down Expand Up @@ -93,6 +148,7 @@ public function addView(View $view)
'param_count' => count($params),
'params' => $params,
'type' => $type,
'editorLink' => $this->getEditorHref($view->getPath(), 0),
];

if ($this->getXdebugLink($path)) {
Expand All @@ -111,4 +167,16 @@ public function collect()
'templates' => $templates,
];
}

/**
* Replace remote path
*
* @param string $filePath
*
* @return string
*/
protected function replaceSitesPath($filePath)
{
return str_replace(config('debugbar.remote_sites_path'), config('debugbar.local_sites_path'), $filePath);
}
}
1 change: 1 addition & 0 deletions src/JavascriptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(DebugBar $debugBar, $baseUrl = null, $basePath = nul
$this->cssVendors['fontawesome'] = __DIR__ . '/Resources/vendor/font-awesome/style.css';
$this->jsFiles['laravel-sql'] = __DIR__ . '/Resources/sqlqueries/widget.js';
$this->jsFiles['laravel-cache'] = __DIR__ . '/Resources/cache/widget.js';
$this->jsFiles['laravel-view'] = __DIR__ . '/Resources/templates/widget.js';

$theme = config('debugbar.theme', 'auto');
switch ($theme) {
Expand Down
98 changes: 98 additions & 0 deletions src/Resources/templates/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
(function($) {

var csscls = PhpDebugBar.utils.makecsscls('phpdebugbar-widgets-');

/**
* Widget for the displaying templates data
*
* Options:
* - data
*/
var TemplatesWidget = PhpDebugBar.Widgets.LaravelViewTemplatesWidget = PhpDebugBar.Widget.extend({

className: csscls('templates'),

render: function() {
this.$status = $('<div />').addClass(csscls('status')).appendTo(this.$el);

this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, tpl) {
$('<span />').addClass(csscls('name')).text(tpl.name).appendTo(li);

if (typeof tpl.editorLink !== 'undefined' && tpl.editorLink !== null) {
$('<a href="' + tpl.editorLink + '"></a>')
.addClass(csscls('editor-link'))
.on('click', function (event) {
event.stopPropagation();
})
.appendTo(li);
}
if (typeof tpl.xdebug_link !== 'undefined' && tpl.xdebug_link !== null) {
if (tpl.xdebug_link.ajax) {
$('<a title="' + tpl.xdebug_link.url + '"></a>').on('click', function () {
$.ajax(tpl.xdebug_link.url);
}).addClass(csscls('editor-link')).appendTo(li);
} else {
$('<a href="' + tpl.xdebug_link.url + '"></a>').addClass(csscls('editor-link')).appendTo(li);
}
}
if (tpl.render_time_str) {
$('<span title="Render time" />').addClass(csscls('render-time')).text(tpl.render_time_str).appendTo(li);
}
if (tpl.memory_str) {
$('<span title="Memory usage" />').addClass(csscls('memory')).text(tpl.memory_str).appendTo(li);
}
if (typeof(tpl.param_count) != 'undefined') {
$('<span title="Parameter count" />').addClass(csscls('param-count')).text(tpl.param_count).appendTo(li);
}
if (typeof(tpl.type) != 'undefined' && tpl.type) {
$('<span title="Type" />').addClass(csscls('type')).text(tpl.type).appendTo(li);
}
if (tpl.params && !$.isEmptyObject(tpl.params)) {
var table = $('<table><tr><th colspan="2">Params</th></tr></table>').addClass(csscls('params')).appendTo(li);
for (var key in tpl.params) {
if (typeof tpl.params[key] !== 'function') {
table.append('<tr><td class="' + csscls('name') + '">' + key + '</td><td class="' + csscls('value') +
'"><pre><code>' + tpl.params[key] + '</code></pre></td></tr>');
}
}
li.css('cursor', 'pointer').click(function() {
if (table.is(':visible')) {
table.hide();
} else {
table.show();
}
});
}
}});
this.$list.$el.appendTo(this.$el);
this.$callgraph = $('<div />').addClass(csscls('callgraph')).appendTo(this.$el);

this.bindAttr('data', function(data) {
this.$list.set('data', data.templates);
this.$status.empty();
this.$callgraph.empty();

var sentence = data.sentence || "templates were rendered";
$('<span />').text(data.nb_templates + " " + sentence).appendTo(this.$status);

if (data.accumulated_render_time_str) {
this.$status.append($('<span title="Accumulated render time" />').addClass(csscls('render-time')).text(data.accumulated_render_time_str));
}
if (data.memory_usage_str) {
this.$status.append($('<span title="Memory usage" />').addClass(csscls('memory')).text(data.memory_usage_str));
}
if (data.nb_blocks > 0) {
$('<div />').text(data.nb_blocks + " blocks were rendered").appendTo(this.$status);
}
if (data.nb_macros > 0) {
$('<div />').text(data.nb_macros + " macros were rendered").appendTo(this.$status);
}
if (typeof data.callgraph !== 'undefined') {
this.$callgraph.html(data.callgraph);
}
});
}

});

})(PhpDebugBar.$);

0 comments on commit 7a7315f

Please sign in to comment.