- Finalizing the documentation before tagging 1.0 release
This commit is contained in:
1 parent
bb29a56077
commit
46535c2734
29 files changed
+933
-211
No files matched your search
@@ -85,6 +85,14 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||
|
||||
public static function getSourceLineFromException(int $sourceLine) : ? int
|
||||
{
|
||||
$selfSource = file_get_contents(__FILE__);
|
||||
|
||||
foreach(explode("\n", $selfSource) as $line => $content) {
|
||||
if ( strpos($content, str_replace('$', '%', '/*$EXCEPTION_LINE_BASE$*/')) !== false ) {
|
||||
return $sourceLine - $line;
|
||||
}
|
||||
}
|
||||
|
||||
$sourceFile = file_get_contents("%TEMPLATE%");
|
||||
|
||||
if ( $sourceFile ) {
|
||||
|
||||
@@ -24,6 +24,8 @@ abstract class Context {
|
||||
|
||||
public array $useStack = [];
|
||||
|
||||
public int $functions = 0;
|
||||
|
||||
public array $functionStack = [];
|
||||
|
||||
public array $hooks = [];
|
||||
|
||||
@@ -33,7 +33,7 @@ class BlockToken implements ControlStructure {
|
||||
sprintf('A block awaiting arguments `%s` instead received `%s` with values `%s`', '$arguments', implode(', ', array_map('gettype', \$inlineVariables ?? [])), json_encode(\$inlineVariables))
|
||||
);
|
||||
}
|
||||
?>
|
||||
/*%EXCEPTION_LINE_BASE%*/?>
|
||||
PHP;
|
||||
|
||||
case "define":
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
class EchoToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "echo", "echo.raw" ];
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "echo":
|
||||
return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>";
|
||||
|
||||
case "echo.raw":
|
||||
return "<?php \\Picea\\ControlStructure\\EchoToken::echoRaw($arguments) ?>";
|
||||
}
|
||||
}
|
||||
|
||||
public static function echoRaw($arguments) : string
|
||||
{
|
||||
return "<?php echo $arguments ?>";
|
||||
}
|
||||
|
||||
public static function echoSafe($arguments) : string
|
||||
{
|
||||
return "<?php echo $arguments ?>";
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
class EndRawToken implements ControlStructure {
|
||||
|
||||
public string $token = "endraw";
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,8 @@ class FunctionToken implements ControlStructure {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "function":
|
||||
$context->functions++;
|
||||
|
||||
return $this->printFunction($context, $arguments);
|
||||
|
||||
case "return":
|
||||
@@ -31,7 +33,6 @@ class FunctionToken implements ControlStructure {
|
||||
|
||||
protected function printFunction($context, ?string $arguments) : string
|
||||
{
|
||||
$context->functions++;
|
||||
return "<?php function $arguments { ?>";
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ class IncludeToken implements ControlStructure {
|
||||
|
||||
public string $token = "include";
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
return "<?php echo \$___class__template->picea->inlineContent($arguments); ?>";
|
||||
public function parse(\Picea\Compiler\Context &$context, ? string $viewPath, string $token) {
|
||||
return "<?php echo \$___class__template->picea->inlineContent($viewPath); ?>";
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
class RawToken implements ControlStructure {
|
||||
|
||||
public string $token = "raw";
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Exception;
|
||||
|
||||
use Picea\Picea;
|
||||
|
||||
/**
|
||||
* Whenever an error occured rendering a view, this is the exception that will show up.
|
||||
*
|
||||
*/
|
||||
class RenderHtmlException extends \Exception
|
||||
{
|
||||
protected Picea $picea;
|
||||
|
||||
public function __construct(object $compiledObject, Picea $picea, string $message, int $code, \Throwable $previous)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
|
||||
$this->picea = $picea;
|
||||
|
||||
$this->defineError($previous, $compiledObject);
|
||||
}
|
||||
|
||||
protected function defineError(\Throwable $previous, object $compiledObject) : void
|
||||
{
|
||||
$loadedTemplates = array_flip($this->picea->loadedTemplateFile);
|
||||
|
||||
foreach($previous->getTrace() as $trace) {
|
||||
if ( isset($trace['file'], $loadedTemplates[$trace['file']]) ) {
|
||||
$class = $loadedTemplates[ $trace['file'] ];
|
||||
|
||||
$content = include($trace['file']);
|
||||
|
||||
$this->file = $content['view'];
|
||||
$this->line = $class::getSourceLineFromException($trace['line']);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getTemplateFile(string $filePath) : ? array
|
||||
{
|
||||
$content = null;
|
||||
|
||||
if ( is_array($content) && isset($content['classname'], $content['namespace'], $content['view'], $content['extends']) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Twig\Error;
|
||||
|
||||
use Twig\Source;
|
||||
use Twig\Template;
|
||||
|
||||
/**
|
||||
* Twig base exception.
|
||||
*
|
||||
* This exception class and its children must only be used when
|
||||
* an error occurs during the loading of a template, when a syntax error
|
||||
* is detected in a template, or when rendering a template. Other
|
||||
* errors must use regular PHP exception classes (like when the template
|
||||
* cache directory is not writable for instance).
|
||||
*
|
||||
* To help debugging template issues, this class tracks the original template
|
||||
* name and line where the error occurred.
|
||||
*
|
||||
* Whenever possible, you must set these information (original template name
|
||||
* and line number) yourself by passing them to the constructor. If some or all
|
||||
* these information are not available from where you throw the exception, then
|
||||
* this class will guess them automatically (when the line number is set to -1
|
||||
* and/or the name is set to null). As this is a costly operation, this
|
||||
* can be disabled by passing false for both the name and the line number
|
||||
* when creating a new instance of this class.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class RenderingError extends \Exception
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* By default, automatic guessing is enabled.
|
||||
*
|
||||
* @param string $message The error message
|
||||
* @param int $lineno The template line where the error occurred
|
||||
* @param Source|null $source The source context where the error occurred
|
||||
*/
|
||||
public function __construct(string $message, Source $source = null, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct('', 0, $previous);
|
||||
|
||||
if (null === $source) {
|
||||
$name = null;
|
||||
} else {
|
||||
$name = $source->getName();
|
||||
$this->sourceCode = $source->getCode();
|
||||
$this->sourcePath = $source->getPath();
|
||||
}
|
||||
|
||||
$this->lineno = $lineno;
|
||||
$this->name = $name;
|
||||
$this->rawMessage = $message;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static function generateFromCompiledClass(\Throwable $exception) : self
|
||||
{
|
||||
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
private function readTemplateInformation(): void
|
||||
{
|
||||
$template = null;
|
||||
$templateClass = null;
|
||||
|
||||
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
|
||||
foreach ($backtrace as $trace) {
|
||||
if (isset($trace['object']) && $trace['object'] instanceof Template) {
|
||||
$currentClass = \get_class($trace['object']);
|
||||
$isEmbedContainer = 0 === strpos($templateClass, $currentClass);
|
||||
if (null === $this->name || ($this->name == $trace['object']->getTemplateName() && !$isEmbedContainer)) {
|
||||
$template = $trace['object'];
|
||||
$templateClass = \get_class($trace['object']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update template name
|
||||
if (null !== $template && null === $this->name) {
|
||||
$this->name = $template->getTemplateName();
|
||||
}
|
||||
|
||||
// update template path if any
|
||||
if (null !== $template && null === $this->sourcePath) {
|
||||
$src = $template->getSourceContext();
|
||||
$this->sourceCode = $src->getCode();
|
||||
$this->sourcePath = $src->getPath();
|
||||
}
|
||||
|
||||
if (null === $template || $this->lineno > -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$r = new \ReflectionObject($template);
|
||||
$file = $r->getFileName();
|
||||
|
||||
$exceptions = [$e = $this];
|
||||
while ($e = $e->getPrevious()) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
|
||||
while ($e = array_pop($exceptions)) {
|
||||
$traces = $e->getTrace();
|
||||
array_unshift($traces, ['file' => $e->getFile(), 'line' => $e->getLine()]);
|
||||
|
||||
while ($trace = array_shift($traces)) {
|
||||
if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
|
||||
if ($codeLine <= $trace['line']) {
|
||||
// update template line
|
||||
$this->lineno = $templateLine;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use Picea\Compiler\Context;
|
||||
|
||||
class PrintExtension implements Extension {
|
||||
|
||||
public array $token = [ "print", "print.safe", "print.raw" ];
|
||||
public array $token = [ "echo", "echo.raw", "print", "print.raw", "printf", "printf.raw" ];
|
||||
|
||||
public int $flag = \ENT_QUOTES;
|
||||
|
||||
@@ -21,12 +21,19 @@ class PrintExtension implements Extension {
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
|
||||
{
|
||||
switch($token) {
|
||||
case 'echo':
|
||||
case 'print':
|
||||
case "print.safe":
|
||||
return "<?php echo htmlspecialchars(sprintf($arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
||||
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
||||
|
||||
case 'echo.raw':
|
||||
case "print.raw":
|
||||
return "<?php echo sprintf($arguments) ?>";
|
||||
return "<?php echo $arguments ?>";
|
||||
|
||||
case 'printf':
|
||||
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
||||
|
||||
case 'printf.raw':
|
||||
return "<?php printf((string) $arguments) ?>";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ class DefaultRegistrations implements LanguageRegistration
|
||||
$compiler->registerControlStructure(new \Picea\ControlStructure\SwitchToken());
|
||||
$compiler->registerControlStructure(new \Picea\ControlStructure\DefaultToken());
|
||||
$compiler->registerControlStructure(new \Picea\ControlStructure\BreakToken());
|
||||
$compiler->registerControlStructure(new \Picea\ControlStructure\ContinueToken());
|
||||
$compiler->registerControlStructure(new \Picea\ControlStructure\ExtendsToken());
|
||||
$compiler->registerControlStructure(new \Picea\ControlStructure\SectionToken());
|
||||
$compiler->registerControlStructure(new \Picea\ControlStructure\FunctionToken());
|
||||
|
||||
+8
-50
@@ -20,8 +20,6 @@ class Picea implements LanguageRegistration
|
||||
|
||||
public string $builderTemplatePath;
|
||||
|
||||
public Closure $responseHtml;
|
||||
|
||||
public Caching\Cache $cache;
|
||||
|
||||
public FileFetcher $fileFetcher;
|
||||
@@ -35,7 +33,6 @@ class Picea implements LanguageRegistration
|
||||
public array $compiled = [];
|
||||
|
||||
public function __construct(
|
||||
? Closure $responseHtml = null,
|
||||
? Compiler\Context $context = null,
|
||||
? Caching\Cache $cache = null,
|
||||
? Compiler $compiler = null,
|
||||
@@ -44,7 +41,6 @@ class Picea implements LanguageRegistration
|
||||
? string $builderTemplatePath = null,
|
||||
bool $debug = false
|
||||
){
|
||||
$this->response = $responseHtml;
|
||||
$this->cache = $cache ?? new Caching\Memory("");
|
||||
$this->context = $context ?? new Compiler\BaseContext();
|
||||
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations();
|
||||
@@ -75,53 +71,15 @@ class Picea implements LanguageRegistration
|
||||
return call_user_func($object);
|
||||
}
|
||||
catch(\Throwable $ex) {
|
||||
#throw $ex;
|
||||
# Temporary class for an experiment on retrieving more accurate debug info
|
||||
throw new class($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex) extends \Exception {
|
||||
|
||||
protected Picea $picea;
|
||||
|
||||
public function __construct(object $compiledObject, Picea $picea, string $message, int $code, \Throwable $previous)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
|
||||
$this->picea = $picea;
|
||||
|
||||
# $template = $this->getTemplateFile( $previous->getFile() );
|
||||
$this->defineError($previous, $compiledObject);
|
||||
}
|
||||
|
||||
protected function defineError(\Throwable $previous, object $compiledObject) : void
|
||||
{
|
||||
$loadedTemplates = array_flip($this->picea->loadedTemplateFile);
|
||||
|
||||
foreach($previous->getTrace() as $trace) {
|
||||
if ( isset($trace['file'], $loadedTemplates[$trace['file']]) ) {
|
||||
$class = $loadedTemplates[ $trace['file'] ];
|
||||
|
||||
$content = include($trace['file']);
|
||||
|
||||
$this->file = $content['view'];
|
||||
$this->line = $class::getSourceLineFromException($trace['line']);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getTemplateFile(string $filePath) : ? array
|
||||
{
|
||||
$content = null;
|
||||
|
||||
if ( is_array($content) && isset($content['classname'], $content['namespace'], $content['view'], $content['extends']) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
if (! $ex instanceof Exception\RenderHtmlException ) {
|
||||
throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
|
||||
}
|
||||
else {
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user