Skip to content

Commit 7c85fbc

Browse files
committed
feat: make your link_template customizable.
1 parent 0fbad47 commit 7c85fbc

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

src/LogHandler.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,40 @@ class LogHandler
4545
*/
4646
public $default_settings = array(
4747
'file_path' => '../debug.log',
48-
'vscode_links' => true, // Stack trace references files. link them to your repo (https://code.visualstudio.com/docs/editor/command-line#_opening-vs-code-with-urls).
49-
'vscode_path_search' => '', // This is needed if you develop on a vm. like '/srv/www/...'.
50-
'vscode_path_replace' => '', // The local path to your repo. like 'c:/users/...'.
48+
/**
49+
* Stack trace references files. Make those links clickable.
50+
* Parts in double curly braces are placeholders.
51+
* @see https://code.visualstudio.com/docs/editor/command-line#_opening-vs-code-with-urls).
52+
*/
53+
'link_template' => 'vscode://file/{{path}}:{{line_number}}',
54+
'link_path_search' => '', // This is needed if you develop on a vm. like '/srv/www/...'.
55+
'link_path_replace' => '', // The local path to your repo. like 'c:/users/...'.
5156
);
5257

5358
public function __construct($settings)
5459
{
55-
$this->settings = array_merge($this->default_settings, $settings);
60+
$this->settings = array_merge($this->default_settings, $this->handle_deprecated_settings($settings));
61+
}
62+
63+
/**
64+
* Settings-keys were previously named vscode_*. We generalized that.
65+
* To support backwards-compatibility we rename the keys (vscode_foo -> code_foo).
66+
*
67+
* @param array[] $s settings.
68+
* @return array []
69+
*/
70+
private function handle_deprecated_settings($s)
71+
{
72+
if (isset($s['vscode_links']) && true == $s['vscode_links']) {
73+
$s['link_template'] = $this->default_settings['link_template'];
74+
}
75+
foreach ($s as $key => $value) {
76+
if (0 === strpos($key, 'vscode_')) {
77+
$new_key = str_replace('vscode_', 'link_', $key);
78+
$s[ $new_key ] = ! isset($s[ $new_key ]) ? $value : $s[ $new_key ];
79+
}
80+
}
81+
return $s;
5682
}
5783

5884
/**
@@ -124,18 +150,31 @@ public function get_parsed_content()
124150
return array_values($this->content);
125151
}
126152

127-
public function link_vscode_files($string)
153+
public function link_files($string)
128154
{
129-
$string = preg_replace_callback('$([A-Z]:)?([\\\/][^:(\s]+)(?: on line |[:\(])([0-9]+)\)?$', array( $this, 'vscode_link_filter' ), $string);
155+
$string = preg_replace_callback('$([A-Z]:)?([\\\/][^:(\s]+)(?: on line |[:\(])([0-9]+)\)?$', array( $this, 'link_filter' ), $string);
130156
return $string;
131157
}
132158

133-
public function vscode_link_filter($matches)
159+
/**
160+
*
161+
* @param array $matches
162+
* 0 => full match
163+
* 1 => hard-drive ( windows only, like "C:" )
164+
* 2 => path (from: on line")
165+
* 3 => line number
166+
* @return string|bool
167+
*/
168+
public function link_filter($matches)
134169
{
135-
$link = 'vscode://file/' . $matches[1] . $matches[2] . ':' . $matches[3];
136-
// $root = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : $_SERVER['DOCUMENT_ROOT'];
137-
// $val = parse_url( $root, PHP_URL_QUERY );
138-
$link = str_replace($this->settings['vscode_path_search'], $this->settings['vscode_path_replace'], $link);
170+
$template = array(
171+
'path' => str_replace($this->settings['link_path_search'], $this->settings['link_path_replace'], $matches[1] . $matches[2]),
172+
'line_number' => $matches[3]
173+
);
174+
$link = $this->settings['link_template'];
175+
foreach ($template as $key => $value) {
176+
$link = str_replace("{{" . $key . "}}", $value, $link); // apply the template.
177+
}
139178
return "<a href='$link'>" . $matches[0] . '</a>';
140179
}
141180

@@ -167,7 +206,7 @@ public function replace_callback($arr)
167206
$date = date_create($arr[1]); // false if no valid date.
168207
$this->content[ $err_id ]['time'] = $date ? $date->format(\DateTime::ATOM) : $arr[1]; // ISO8601, readable in js
169208
$message = htmlspecialchars(trim($arr[2]), ENT_QUOTES);
170-
$this->content[ $err_id ]['msg'] = $this->settings['vscode_links'] ? $this->link_vscode_files($message) : $message;
209+
$this->content[ $err_id ]['msg'] = $this->settings['link_template'] ? $this->link_files($message) : $message;
171210
$this->content[ $err_id ]['cls'] = implode(
172211
' ',
173212
array_slice(

0 commit comments

Comments
 (0)