forked from nette/nette
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
535 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Nette\Diagnostics; | ||
|
||
|
||
/** | ||
* Resolves source maps | ||
*/ | ||
class SourceMapHelper | ||
{ | ||
|
||
/** | ||
* Converts line number in compiled file into line number in source file (e.g. for latte templates) | ||
* | ||
* @param string | ||
* @param int | ||
* @return null|array [ sourceFile, sourceLine ] or null if not found | ||
*/ | ||
public static function sourceMapLookup($originalFile, $originalLine) | ||
{ | ||
$data = file_get_contents($originalFile); | ||
if (!preg_match('~^// source file: (.+)$~m', $data, $match)) { | ||
return; | ||
} | ||
$sourceFile = trim($match[1]); | ||
|
||
if (!preg_match('~// source map: ([^\s]+)~', $data, $match)) { | ||
return; | ||
} | ||
$map = json_decode($match[1]); | ||
|
||
$compiledLine = $originalLine - substr_count($data, "\n", 0, strpos($data, $match[0])) - 1; // adjust line for source map beginning (some lines before the mapped file + 1 line of source map definition) | ||
|
||
// find closest key | ||
$compiledLines = array(); | ||
foreach (array_keys((array) $map) as $line) { | ||
$compiledLines[$line] = abs($line - $compiledLine); | ||
} | ||
asort($compiledLines); | ||
$closestLine = key($compiledLines); | ||
|
||
return array($sourceFile, reset($map->$closestLine)); | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Diagnostics\Helper::sourceMapLookup() | ||
* | ||
* @author Jan Dolecek | ||
* @package Nette\Diagnostics | ||
* @subpackage UnitTests | ||
*/ | ||
|
||
use Nette\Diagnostics\SourceMapHelper; | ||
use Tester\Assert; | ||
|
||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$file = __DIR__ . '/SourceMap.phtml'; | ||
Assert::same( array('SourceMap.phpt', 1), SourceMapHelper::sourceMapLookup($file, 15) ); | ||
Assert::same( array('SourceMap.phpt', 15), SourceMapHelper::sourceMapLookup($file, 16) ); | ||
Assert::same( array('SourceMap.phpt', 15), SourceMapHelper::sourceMapLookup($file, 17) ); | ||
Assert::same( NULL, SourceMapHelper::sourceMapLookup($file, 19) ); | ||
|
||
// Another source map within the same file - not supported at the moment | ||
// Assert::same( array('SourceMap.phpt', 1), Helpers::sourceMapLookup($file, 32) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
// Example of compiled file with source-map | ||
$sourceMap = array( | ||
array(4, 1), // source line 1 compiled to line 4 | ||
array(5, 15), | ||
array(6, 15), | ||
); | ||
echo serialize($sourceMap); | ||
|
||
// source file: SourceMap.phpt | ||
//sourceMap[073e15abd403c7c814443453dce450affde014a8]000086:a:3:{i:0;a:2:{i:0;i:4;i:1;i:1;}i:1;a:2:{i:0;i:5;i:1;i:15;}i:2;a:2:{i:0;i:6;i:1;i:15;}} -- after this the payload starts | ||
$line1 = | ||
NULL; // of the original file | ||
|
||
$compiledVariable1 = 1; // from original line 1 | ||
if ($x && $y) $compiledVariable15 = 2; | ||
else $compiledVariable15 = 'ABCD'; | ||
|
||
$notFromSource = 'X'; | ||
|
||
//eof:sourceMap[073e15abd403c7c814443453dce450affde014a8] | ||
|
||
|
||
$glueCode = 1; // not from source | ||
|
||
|
||
// Another source map within the same file | ||
//sourceMap[0000000000000000000000000000000000000011]000032:a:1:{i:0;a:2:{i:0;i:4;i:1;i:1;}} -- after this the payload starts | ||
$line1 = | ||
NULL; // of the original file | ||
|
||
$compiledVariable1 = 1; | ||
|
||
//eof:sourceMap[0000000000000000000000000000000000000011] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php // source map %a%: {"21":[14],"25":[24],"26":[18],"27":[21],"32":[24],"35":[31],"36":[28],"37":[29],"38":[30],"40":[31],"50":[12]} | ||
|
||
|
||
// prolog Nette\Latte\Macros\CoreMacros | ||
list($_l, $_g) = Nette\Latte\Macros\CoreMacros::initRuntime($template, '%a%') | ||
; | ||
// prolog Nette\Latte\Macros\UIMacros | ||
// | ||
// block css | ||
// | ||
if (!function_exists($_l->blocks['css'][] = '%a%_css')) { function %a%_css($_l, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v | ||
?> .no-images img { display: none; } | ||
.x .y { font-weight: bold; } | ||
<?php | ||
}} | ||
|
||
// | ||
// block content | ||
// | ||
if (!function_exists($_l->blocks['content'][] = '%a%_content')) { function %a%_content($_l, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v | ||
; call_user_func(reset($_l->blocks['title']), $_l, get_defined_vars()) {* original-line 12 *} | ||
|
||
; | ||
|
||
echo Nette\Templating\Helpers::escapeHtml($x, ENT_NOQUOTES) {* original-line 14 *} | ||
?> | ||
|
||
|
||
<ul> | ||
<?php $iterations = 0; foreach ($items as $item) { ?> <li> | ||
Item <?php echo Nette\Templating\Helpers::escapeHtml($item->id, ENT_NOQUOTES) ?>: | ||
<a | ||
onmouseover="console.log(this)" href="<?php echo htmlSpecialChars($_control->link("item", array($item->id))) ?>"<?php if ($_l->tmp = array_filter(array($item->superCool ? 'superCool' : NULL))) echo ' class="' . htmlSpecialChars(implode(" ", array_unique($_l->tmp))) . '"' ?>> | ||
Link | ||
</a> | ||
</li> | ||
<?php $iterations++; } | ||
?> | ||
</ul> | ||
|
||
<?php Nette\Latte\Macros\FormMacros::renderFormBegin($form = $_form = $_control["myForm"], array()) | ||
?> | ||
Name: <?php echo $_form["name"]->getControl() ?><br> | ||
Mail: <?php echo $_form["email"]->getControl() ?><br> | ||
<?php echo $_form["add"]->getControl() | ||
?> | ||
|
||
<?php Nette\Latte\Macros\FormMacros::renderFormEnd($_form) | ||
?> | ||
|
||
<hr> | ||
<?php | ||
}} | ||
|
||
// | ||
// block title | ||
// | ||
if (!function_exists($_l->blocks['title'][] = '%a%_title')) { function %a%_title($_l, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v | ||
?><h1>Page <?php echo Nette\Templating\Helpers::escapeHtml($name, ENT_NOQUOTES) ?></h1> | ||
<?php | ||
}} | ||
|
||
// | ||
// end of blocks | ||
// | ||
|
||
// template extending and snippets support | ||
|
||
$_l->extends = empty($template->_extended) && isset($_control) && $_control instanceof Nette\Application\UI\Presenter ? $_control->findLayoutTemplateFile() : NULL; $template->_extended = $_extended = TRUE; | ||
|
||
|
||
if ($_l->extends) { | ||
ob_start(); | ||
|
||
} elseif (!empty($_control->snippetMode)) { | ||
return Nette\Latte\Macros\UIMacros::renderSnippets($_control, $_l, get_defined_vars()); | ||
} | ||
|
||
// | ||
// main template | ||
// | ||
|
||
|
||
|
||
|
||
if ($_l->extends) { ob_end_clean(); return Nette\Latte\Macros\CoreMacros::includeTemplate($_l->extends, get_defined_vars(), $template)->render(); } | ||
call_user_func(reset($_l->blocks['css']), $_l, get_defined_vars()) | ||
|
||
|
||
?> | ||
|
||
|
||
|
||
<?php call_user_func(reset($_l->blocks['content']), $_l, get_defined_vars()) | ||
|
||
; | ||
|
||
// end of source map %a% |
Oops, something went wrong.