Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions docs/Open Files In An Editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ $handler->setEditor(function($file, $line) {

```

If your development server is not local it's good to map remote files to local

```php

$handler->setEditorPathReplacements([
'/My/First/Server/Path' => '~/Development/PhpStormOpener',
'/My/Second/Server/Path' => '~/Development/PhpStormOpener',
]);

```

You can add [IntelliJ Platform](https://github.com/pinepain/PhpStormOpener#phpstormopener) support like this:
```php

$handler->setEditor(
function ($file, $line) {
// if your development server is not local it's good to map remote files to local
$translations = array('^' . __DIR__ => '~/Development/PhpStormOpener'); // change to your path

foreach ($translations as $from => $to) {
$file = preg_replace('#' . $from . '#', $to, $file, 1);
}

// IntelliJ platform requires that you send an Ajax request, else the browser will quit the page
return array(
'url' => "http://localhost:63342/api/file/?file=$file&line=$line",
Expand Down
70 changes: 69 additions & 1 deletion src/Whoops/Handler/PrettyPageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class PrettyPageHandler extends Handler
'_ENV' => [],
];

/**
* If your development server is not local map remote files to local
*
* @var array $editorPathReplacements
*/
protected $editorPathReplacements = [];

/**
* An identifier for a known IDE/text editor.
*
Expand Down Expand Up @@ -524,7 +531,7 @@ public function getEditorHref($filePath, $line)
}

$editor['url'] = str_replace("%line", rawurlencode($line), $editor['url']);
$editor['url'] = str_replace("%file", rawurlencode($filePath), $editor['url']);
$editor['url'] = str_replace("%file", rawurlencode($this->replaceRemotePath($filePath)), $editor['url']);

return $editor['url'];
}
Expand Down Expand Up @@ -574,6 +581,8 @@ protected function getEditor($filePath, $line)
}

if (is_callable($this->editor) || (isset($this->editors[$this->editor]) && is_callable($this->editors[$this->editor]))) {
$filePath = $this->replaceRemotePath($filePath);

if (is_callable($this->editor)) {
$callback = call_user_func($this->editor, $filePath, $line);
} else {
Expand All @@ -600,6 +609,65 @@ protected function getEditor($filePath, $line)
return [];
}

/**
* Replace remote file path with local file path
*
* @param string $filePath
* @return string
*/
private function replaceRemotePath($filePath)
{
foreach ($this->editorPathReplacements as $path => $replacement) {
if (strpos($filePath, $path) === 0) {
$filePath = $replacement . substr($filePath, strlen($path));
break;
}
}

return $filePath;
}

/**
* Shorten the file path by removing the remote path replacements
*
* @param string $filePath
* @return string
*/
public function normalizeFilePath($filePath)
{
if (empty($filePath)) {
return '';
}

foreach (array_keys($this->editorPathReplacements) as $path) {
if (strpos($filePath, $path) === 0) {
$filePath = substr($filePath, strlen($path));
break;
}
}

return ltrim(str_replace('\\', '/', $filePath), '/');
}

/**
* @param array $editorPathReplacements
*/
public function setEditorPathReplacements($editorPathReplacements)
{
foreach ($editorPathReplacements as $serverPath => $replacement) {
$this->setEditorPathReplacement($serverPath, $replacement);
}
}

/**
* @param string $serverPath
* @param string $replacement
*/
public function setEditorPathReplacement($serverPath, $replacement)
{
$this->editorPathReplacements[$serverPath] = $replacement;
}

/**
* Set the page title.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Whoops/Resources/views/frame_code.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<?php if ($filePath && $editorHref = $handler->getEditorHref($filePath, (int) $line)): ?>
<a href="<?php echo $editorHref ?>" class="editor-link"<?php echo ($handler->getEditorAjax($filePath, (int) $line) ? ' data-ajax' : '') ?>>
Open:
<strong><?php echo $tpl->breakOnDelimiter('/', $tpl->escape($filePath ?: '<#unknown>')) ?></strong>
<strong><?php echo $tpl->breakOnDelimiter('/', $tpl->escape($handler->normalizeFilePath($filePath) ?: '<#unknown>')) ?></strong>
</a>
<?php else: ?>
<strong><?php echo $tpl->breakOnDelimiter('/', $tpl->escape($filePath ?: '<#unknown>')) ?></strong>
<strong><?php echo $tpl->breakOnDelimiter('/', $tpl->escape($handler->normalizeFilePath($filePath) ?: '<#unknown>')) ?></strong>
<?php endif ?>
</div>
<?php
Expand Down
Loading