mirror of
https://github.com/filp/whoops.git
synced 2026-09-19 06:06:19 +00:00
Move template helpers to own class
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
namespace Whoops\Handler;
|
||||
use Whoops\Handler\Handler;
|
||||
use Whoops\Util\TemplateHelper;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
|
||||
@@ -86,6 +87,9 @@ class PrettyPageHandler extends Handler
|
||||
return Handler::DONE;
|
||||
}
|
||||
|
||||
// @todo Make this more dynamic ~~ *
|
||||
$helper = new TemplateHelper;
|
||||
|
||||
$templateFile = $this->getResource("pretty-template.php");
|
||||
$cssFile = $this->getResource("pretty-page.css");
|
||||
|
||||
@@ -122,28 +126,10 @@ class PrettyPageHandler extends Handler
|
||||
// Add extra entries list of data tables:
|
||||
$v->tables = array_merge($extraTables, $v->tables);
|
||||
|
||||
call_user_func(function() use($templateFile, $v) {
|
||||
call_user_func(function() use($templateFile, $v, $helper) {
|
||||
// $e -> cleanup output, optionally preserving URIs as anchors:
|
||||
$e = function($_, $allowLinks = false) {
|
||||
$escaped = htmlspecialchars($_, ENT_QUOTES, 'UTF-8');
|
||||
|
||||
// convert URIs to clickable anchor elements:
|
||||
if($allowLinks) {
|
||||
$escaped = preg_replace(
|
||||
'@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@',
|
||||
"<a href=\"$1\" target=\"_blank\">$1</a>", $escaped
|
||||
);
|
||||
}
|
||||
|
||||
return $escaped;
|
||||
};
|
||||
|
||||
// $slug -> sluggify string (i.e: Hello world! -> hello-world)
|
||||
$slug = function($_) {
|
||||
$_ = str_replace(" ", "-", $_);
|
||||
$_ = preg_replace('/[^\w\d\-\_]/i', '', $_);
|
||||
return strtolower($_);
|
||||
};
|
||||
$e = array($helper, "escape");
|
||||
$slug = array($helper, "slug");
|
||||
|
||||
require $templateFile;
|
||||
});
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Whoops - php errors for cool kids
|
||||
* @author Filipe Dobreira <http://github.com/filp>
|
||||
*/
|
||||
|
||||
namespace Whoops\Util;
|
||||
|
||||
/**
|
||||
* Exposes useful tools for working with/in templates
|
||||
*/
|
||||
class TemplateHelper
|
||||
{
|
||||
/**
|
||||
* Escapes a string for output in an HTML document
|
||||
*
|
||||
* @param string $raw
|
||||
* @return string
|
||||
*/
|
||||
public function escape($raw)
|
||||
{
|
||||
return htmlspecialchars($raw, ENT_QUOTES, "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes a string for output in an HTML document, but preserves
|
||||
* URIs within it, and converts them to clickable anchor elements.
|
||||
*
|
||||
* @param string $raw
|
||||
* @return string
|
||||
*/
|
||||
public function escapeButPreserveUris($raw)
|
||||
{
|
||||
$escaped = $this->escape($raw);
|
||||
return preg_replace(
|
||||
"@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
|
||||
"<a href=\"$1\" target=\"_blank\">$1</a>", $escaped
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string to a slug version of itself
|
||||
*
|
||||
* @param string $original
|
||||
* @return string
|
||||
*/
|
||||
public function slug($original)
|
||||
{
|
||||
$slug = str_replace(" ", "-", $original);
|
||||
$slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
|
||||
return strtolower($slug);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Whoops - php errors for cool kids
|
||||
* @author Filipe Dobreira <http://github.com/filp>
|
||||
*/
|
||||
|
||||
namespace Whoops\Util;
|
||||
use Whoops\TestCase;
|
||||
use Whoops\Util\TemplateHelper;
|
||||
|
||||
class TemplateHelperTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Whoops\Util\TemplateHelper
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->helper = new TemplateHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Whoops\Util\TemplateHelper::escapeButPreserveUris
|
||||
* @covers Whoops\Util\TemplateHelper::escape
|
||||
*/
|
||||
public function testEscape()
|
||||
{
|
||||
$original = "This is a <a href=''>Foo</a> test string";
|
||||
|
||||
$this->assertEquals(
|
||||
$this->helper->escape($original),
|
||||
"This is a <a href=''>Foo</a> test string"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Whoops\Util\TemplateHelper::escapeButPreserveUris
|
||||
*/
|
||||
public function testEscapeButPreserveUris()
|
||||
{
|
||||
$original = "This is a <a href=''>http://google.com</a> test string";
|
||||
|
||||
$this->assertEquals(
|
||||
$this->helper->escapeButPreserveUris($original),
|
||||
"This is a <a href=''><a href=\"http://google.com\" target=\"_blank\">http://google.com</a></a> test string"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user