Changed: getEditorHref() no longer return an array or ajax/url, replaced by getEditorAjax() instead.

This commit is contained in:
Geolim4
2015-05-15 16:57:47 +02:00
parent d3d3b33dce
commit 69042b38f4
3 changed files with 49 additions and 24 deletions
+35 -15
View File
@@ -8,6 +8,7 @@ namespace Whoops\Handler;
use InvalidArgumentException;
use RuntimeException;
use UnexpectedValueException;
use Whoops\Exception\Formatter;
use Whoops\Util\Misc;
use Whoops\Util\TemplateHelper;
@@ -317,7 +318,7 @@ class PrettyPageHandler extends Handler
* @throws InvalidArgumentException If editor resolver does not return a string
* @param string $filePath
* @param int $line
* @return false|string|array
* @return false|string
*/
public function getEditorHref($filePath, $line)
{
@@ -342,26 +343,45 @@ class PrettyPageHandler extends Handler
);
}
if (is_array($editor) && (!isset($editor['ajax']) || !is_bool($editor['ajax']))) {
throw new InvalidArgumentException(
$editor = str_replace("%line", rawurlencode($line), (is_string($editor) ? $editor : $editor['url']));
$editor = str_replace("%file", rawurlencode($filePath), (is_string($editor) ? $editor : $editor['url']));
return $editor;
}
/**
* Given a boolean if the editor link should
* act as an Ajax request. The editor must be a
* valid callable function/closure
*
* @throws UnexpectedValueException If editor resolver does not return a boolean
* @param string $filePath
* @param int $line
* @return bool
*/
public function getEditorAjax($filePath, $line)
{
if ($this->editor === null || !is_callable($this->editor)) {
return false;
}
$editor = $this->editor;
$editor = call_user_func($editor, $filePath, $line);
// Check that the editor is a string or a valid array, and replace the
// %line and %file placeholders:
if (!isset($editor['ajax']) || !is_bool($editor['ajax'])) {
throw new UnexpectedValueException(
__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;
return $editor['ajax'];
}
/**
* @param string $title
* @return void
@@ -9,7 +9,7 @@
<?php $filePath = $frame->getFile(); ?>
<?php if ($filePath && $editorHref = $handler->getEditorHref($filePath, (int) $line)): ?>
Open:
<a href="<?php echo (is_array($editorHref) ? $editorHref['url'] : $editorHref) ?>" class="editor-link"<?php echo (is_array($editorHref) && $editorHref['ajax'] ? ' data-ajax' : '') ?>>
<a href="<?php echo $editorHref ?>" class="editor-link"<?php echo ($handler->getEditorAjax($filePath, (int) $line) ? ' data-ajax' : '') ?>>
<strong><?php echo $tpl->escape($filePath ?: '<#unknown>') ?></strong>
</a>
<?php else: ?>
+13 -8
View File
@@ -209,6 +209,7 @@ class PrettyPageHandlerTest extends TestCase
/**
* @covers Whoops\Handler\PrettyPageHandler::setEditor
* @covers Whoops\Handler\PrettyPageHandler::getEditorHref
* @covers Whoops\Handler\PrettyPageHandler::getEditorAjax
*/
public function testSetEditorCallable()
{
@@ -238,12 +239,15 @@ class PrettyPageHandlerTest extends TestCase
$this->assertEquals(
$handler->getEditorHref('/foo/bar.php', 10),
array(
'url' => 'http://google.com/search/?q=%2Ffoo%2Fbar.php:10',
'ajax' => true
)
'http://google.com/search/?q=%2Ffoo%2Fbar.php:10'
);
$this->assertEquals(
$handler->getEditorAjax('/foo/bar.php', 10),
true
);
$handler->setEditor(function ($file, $line) {
$file = rawurlencode($file);
$line = rawurlencode($line);
@@ -255,12 +259,13 @@ class PrettyPageHandlerTest extends TestCase
$this->assertEquals(
$handler->getEditorHref('/foo/bar.php', 10),
array(
'url' => 'http://google.com/search/?q=%2Ffoo%2Fbar.php:10',
'ajax' => false
)
'http://google.com/search/?q=%2Ffoo%2Fbar.php:10'
);
$this->assertEquals(
$handler->getEditorAjax('/foo/bar.php', 10),
false
);
}