Callable editor can now return an array

This commit is contained in:
Geolim4
2015-05-15 03:49:13 +02:00
parent c522ade8a7
commit ccd137eec8
6 changed files with 81 additions and 13 deletions
+12 -1
View File
@@ -365,7 +365,7 @@ PrettyPageHandler::addResourcePath(string $resourcesPath)
// Sets an editor to use to open referenced files, either by
// a string identifier, or as an arbitrary callable that returns
// a string that can be used as an href attribute.
// a string or an array that can be used as an href attribute.
// Available built-in editors are:
// - sublime
// - emacs
@@ -373,6 +373,15 @@ PrettyPageHandler::addResourcePath(string $resourcesPath)
// - macvim
PrettyPageHandler::setEditor(string $editor)
PrettyPageHandler::setEditor(function ($file, $line) { return string })
// Additionally you may want that the link acts as an ajax request (e.g. Intellij platform)
PrettyPageHandler::setEditor(function ($file, $line) {
return array(
'url' => "http://localhost:63342/api/file/?file=$file&line=$line",
'ajax' => true
);
}
)
#=> null
// Similar to PrettyPageHandler::setEditor, but allows you
@@ -387,6 +396,8 @@ PrettyPageHandler::setEditor(function ($file, $line) { return string })
// $handler->addEditor('whatevs', 'whatevs://open?file=file://%file&line=%line')
PrettyPageHandler::addEditor(string $editor, $resolver)
#=> null
PrettyPageHandler::handle()
#=> int | null
+6 -2
View File
@@ -30,7 +30,7 @@ $handler->setEditor(function($file, $line) {
```
You can add PhpStorm support with [PhpStormOpener](https://github.com/pinepain/PhpStormOpener#phpstormopener) (Mac OS X only):
You can add [IntelliJ Platform](https://github.com/pinepain/PhpStormOpener#phpstormopener) support like this:
```php
$handler->setEditor(
@@ -42,7 +42,11 @@ $handler->setEditor(
$file = preg_replace('#' . $from . '#', $to, $file, 1);
}
return "pstorm://$file:$line";
// Intellig 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",
'ajax' => true
);
}
);
+20 -6
View File
@@ -317,7 +317,7 @@ class PrettyPageHandler extends Handler
* @throws InvalidArgumentException If editor resolver does not return a string
* @param string $filePath
* @param int $line
* @return false|string
* @return false|string|array
*/
public function getEditorHref($filePath, $line)
{
@@ -334,16 +334,30 @@ class PrettyPageHandler extends Handler
$editor = call_user_func($editor, $filePath, $line);
}
// Check that the editor is a string, and replace the
// Check that the editor is a string or a valid array, and replace the
// %line and %file placeholders:
if (!is_string($editor)) {
if (!is_string($editor) && (is_array($editor) && (!isset($editor['url']) || !is_string($editor['url'])))) {
throw new InvalidArgumentException(
__METHOD__ . " should always resolve to a string; got something else instead"
__METHOD__ . " should always resolve to a string or a valid editor array; got something else instead"
);
}
$editor = str_replace("%line", rawurlencode($line), $editor);
$editor = str_replace("%file", rawurlencode($filePath), $editor);
if (is_array($editor) && (!isset($editor['ajax']) || !is_bool($editor['ajax']))) {
throw new InvalidArgumentException(
__METHOD__ . " was unable to resolve ajax option; got something else instead"
);
}
if(is_string($editor))
{
$editor = str_replace("%line", rawurlencode($line), $editor);
$editor = str_replace("%file", rawurlencode($filePath), $editor);
}
else
{
$editor['url'] = str_replace("%line", rawurlencode($line), $editor['url']);
$editor['url'] = str_replace("%file", rawurlencode($filePath), $editor['url']);
}
return $editor;
}
+2 -2
View File
@@ -5,7 +5,7 @@ Zepto(function($) {
var $container = $('.details-container');
var $activeLine = $frameContainer.find('.frame.active');
var $activeFrame = $container.find('.frame-code.active');
var $editorLinks = $('.editor-link');
var $ajaxEditors = $('.editor-link[data-ajax]');
var headerHeight = $('header').height();
var highlightCurrentLine = function() {
@@ -79,7 +79,7 @@ Zepto(function($) {
});
// Avoid to quit the page with some protocol (e.g. IntelliJ Platform REST API)
$editorLinks.on('click', function(e){
$ajaxEditors.on('click', function(e){
e.preventDefault();
$.get(this.href);
});
@@ -9,7 +9,7 @@
<?php $filePath = $frame->getFile(); ?>
<?php if ($filePath && $editorHref = $handler->getEditorHref($filePath, (int) $line)): ?>
Open:
<a href="<?php echo $editorHref ?>" class="editor-link">
<a href="<?php echo (is_array($editorHref) ? $editorHref['url'] : $editorHref) ?>" class="editor-link"<?php echo (is_array($editorHref) && $editorHref['ajax'] ? ' data-ajax' : '') ?>>
<strong><?php echo $tpl->escape($filePath ?: '<#unknown>') ?></strong>
</a>
<?php else: ?>
+40 -1
View File
@@ -13,7 +13,7 @@ use Whoops\TestCase;
class PrettyPageHandlerTest extends TestCase
{
/**
* @return \Whoops\Handler\JsonResponseHandler
* @return \Whoops\Handler\PrettyPageHandler
*/
private function getHandler()
{
@@ -213,6 +213,8 @@ class PrettyPageHandlerTest extends TestCase
public function testSetEditorCallable()
{
$handler = $this->getHandler();
// Test Callable editor with String return
$handler->setEditor(function ($file, $line) {
$file = rawurlencode($file);
$line = rawurlencode($line);
@@ -223,6 +225,43 @@ class PrettyPageHandlerTest extends TestCase
$handler->getEditorHref('/foo/bar.php', 10),
'http://google.com/search/?q=%2Ffoo%2Fbar.php:10'
);
// Then test Callable editor with Array return
$handler->setEditor(function ($file, $line) {
$file = rawurlencode($file);
$line = rawurlencode($line);
return array(
'url' => "http://google.com/search/?q=$file:$line",
'ajax' => true,
);
});
$this->assertEquals(
$handler->getEditorHref('/foo/bar.php', 10),
array(
'url' => 'http://google.com/search/?q=%2Ffoo%2Fbar.php:10',
'ajax' => true
)
);
$handler->setEditor(function ($file, $line) {
$file = rawurlencode($file);
$line = rawurlencode($line);
return array(
'url' => "http://google.com/search/?q=$file:$line",
'ajax' => false,
);
});
$this->assertEquals(
$handler->getEditorHref('/foo/bar.php', 10),
array(
'url' => 'http://google.com/search/?q=%2Ffoo%2Fbar.php:10',
'ajax' => false
)
);
}
/**