Skip to content

Commit

Permalink
Merge pull request #26 from Stillat/validate-command-failing-on-unrec…
Browse files Browse the repository at this point in the history
…ognized-directives

Improves document validator; corrects issues with component tag compilation
  • Loading branch information
JohnathonKoster authored Feb 11, 2024
2 parents 6646e5e + fc14708 commit 650c1a8
Show file tree
Hide file tree
Showing 68 changed files with 378 additions and 252 deletions.
8 changes: 4 additions & 4 deletions src/Compiler/AppendState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
class AppendState
{
/**
* @param AbstractNode $node The node that was compiled.
* @param int $beforeLineNumber The last line number in the compiled document before the node was compiled.
* @param int $afterLineNumber The last line number in the compiled document after the node was compiled.
* @param string $value The node's compiled value.
* @param AbstractNode $node The node that was compiled.
* @param int $beforeLineNumber The last line number in the compiled document before the node was compiled.
* @param int $afterLineNumber The last line number in the compiled document after the node was compiled.
* @param string $value The node's compiled value.
*/
public function __construct(public AbstractNode $node,
public int $beforeLineNumber,
Expand Down
57 changes: 36 additions & 21 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class Compiler
*/
private bool $failStrictly = false;

private array $ignoreDirectives = [];

public function __construct(DocumentParser $parser)
{
$this->compilationBuffer = new StringBuffer();
Expand All @@ -155,14 +157,23 @@ public function __construct(DocumentParser $parser)
);
}

public function ignoreDirectives(array $directives): Compiler
{
$this->ignoreDirectives = $directives;
$this->componentTagCompiler->ignoreDirectives($directives);
$this->parser->ignoreDirectives($directives);

return $this;
}

/**
* Register a custom component tag compiler.
*
* This method will automatically register the provided tag name
* with the component tag compiler.
*
* @param string $tagName The custom component tag prefix.
* @param CustomComponentTagCompiler $compiler The compiler instance.
* @param string $tagName The custom component tag prefix.
* @param CustomComponentTagCompiler $compiler The compiler instance.
* @return $this
*/
public function registerCustomComponentTagCompiler(string $tagName, CustomComponentTagCompiler $compiler): Compiler
Expand All @@ -188,7 +199,7 @@ public static function newComponentHash(string $component): string
* When set to false, the internal component tag compiler will
* not compile Laravel component tags (<x-, <x:, etc.).
*
* @param bool $compileCoreComponents Whether to compile core Laravel component tags.
* @param bool $compileCoreComponents Whether to compile core Laravel component tags.
* @return $this
*/
public function setCompileCoreComponents(bool $compileCoreComponents): Compiler
Expand Down Expand Up @@ -232,7 +243,7 @@ public function getErrors(): Collection
*
* Callbacks are invoked in the order they were registered.
*
* @param callable $callback The callback.
* @param callable $callback The callback.
* @return $this
*/
public function onAppend(callable $callback): Compiler
Expand Down Expand Up @@ -273,7 +284,7 @@ public function getExtensions(): array
* `Stillat\BladeParser\Errors\Exceptions\CompilationException`
* whenever it encounters a parser error.
*
* @param bool $failOnParserErrors Whether to fail on parser errors.
* @param bool $failOnParserErrors Whether to fail on parser errors.
* @return $this
*/
public function setFailOnParserErrors(bool $failOnParserErrors): Compiler
Expand All @@ -297,7 +308,7 @@ public function getFailOnParserErrors(): bool
* When set to true, the compiler will fail on any error type. When
* set to false, it will only fail on fatal errors.
*
* @param bool $isParserErrorsStrict Whether to fail on any parser error.
* @param bool $isParserErrorsStrict Whether to fail on any parser error.
* @return $this
*/
public function setParserErrorsIsStrict(bool $isParserErrorsStrict): Compiler
Expand All @@ -324,7 +335,7 @@ public function getParserErrorsIsStrict(): bool
* In default setups, this is set to the return value of
* `Illuminate\View\Compilers\BladeCompiler::getExtensions()`
*
* @param array $extensions The extensions.
* @param array $extensions The extensions.
*/
public function setExtensions(array $extensions): Compiler
{
Expand All @@ -339,7 +350,7 @@ public function setExtensions(array $extensions): Compiler
* In default setups, this is set to the return value of
* `Illuminate\View\Compilers\BladeCompiler::getAnonymousComponentNamespaces()`
*
* @param array $anonymousNamespaces The anonymous namespaces.
* @param array $anonymousNamespaces The anonymous namespaces.
*/
public function setAnonymousComponentNamespaces(array $anonymousNamespaces): Compiler
{
Expand All @@ -355,7 +366,7 @@ public function setAnonymousComponentNamespaces(array $anonymousNamespaces): Com
* In default setups, this is set to the return value of
* `Illuminate\View\Compilers\BladeCompiler::getClassComponentAliases()`
*
* @param array $aliases The class component aliases.
* @param array $aliases The class component aliases.
*/
public function setClassComponentAliases(array $aliases): Compiler
{
Expand All @@ -371,7 +382,7 @@ public function setClassComponentAliases(array $aliases): Compiler
* In default setups, this is set to the return value of
* `Illuminate\View\Compilers\BladeCompiler::getClassComponentNamespaces()`
*
* @param array $namespaces The class component namespaces.
* @param array $namespaces The class component namespaces.
*/
public function setClassComponentNamespaces(array $namespaces): Compiler
{
Expand All @@ -387,7 +398,7 @@ public function setClassComponentNamespaces(array $namespaces): Compiler
* In default setups, this is set to the return value of
* `Illuminate\View\Compilers\BladeCompiler::getAnonymousComponentPaths()`
*
* @param array $paths The anonymous component paths.
* @param array $paths The anonymous component paths.
*/
public function setAnonymousComponentPaths(array $paths): Compiler
{
Expand All @@ -408,7 +419,7 @@ public function getComponentTagCompiler(): ComponentTagCompiler
/**
* Sets whether the compiler will fail when it encounters unknown component classes.
*
* @param bool $doThrow Whether to throw on unknown component classes.
* @param bool $doThrow Whether to throw on unknown component classes.
*/
public function setThrowExceptionOnUnknownComponentClass(bool $doThrow): void
{
Expand All @@ -418,7 +429,7 @@ public function setThrowExceptionOnUnknownComponentClass(bool $doThrow): void
/**
* Sets whether to compile class component tags.
*
* @param bool $compilesComponentTags Whether to compile component tags.
* @param bool $compilesComponentTags Whether to compile component tags.
*/
public function setCompilesComponentTags(bool $compilesComponentTags): Compiler
{
Expand All @@ -438,7 +449,7 @@ public function getCompilesComponentTags(): bool
/**
* Sets the internal compilation target.
*
* @param CompilationTarget $compilationTarget The compilation target.
* @param CompilationTarget $compilationTarget The compilation target.
*/
public function setCompilationTarget(CompilationTarget $compilationTarget): Compiler
{
Expand All @@ -454,7 +465,7 @@ public function setCompilationTarget(CompilationTarget $compilationTarget): Comp
* `Illuminate\View\Compilers\BladeCompiler::$conditions`
* protected property.
*
* @param array $conditions The condition handlers.
* @param array $conditions The condition handlers.
*/
public function setConditions(array $conditions): Compiler
{
Expand All @@ -479,7 +490,7 @@ public function getConditions(): array
* *not* need to manually call this method to sync compiler information
* if you use the default compiler factory methods/service bindings.
*
* @param callable $precompiler The precompiler.
* @param callable $precompiler The precompiler.
*/
public function precompiler(callable $precompiler): void
{
Expand All @@ -494,7 +505,7 @@ public function precompiler(callable $precompiler): void
* *not* need to manually call this method to sync compiler information
* if you use the default compiler factory methods/service bindings.
*
* @param string $format The format to use.
* @param string $format The format to use.
*/
public function setEchoFormat(string $format): void
{
Expand Down Expand Up @@ -560,7 +571,7 @@ public function resetState(): void
* `Illuminate\View\Compilers\BladeCompiler::$precompilers`
* protected property.
*
* @param array $precompilers The precompilers.
* @param array $precompilers The precompilers.
*/
public function setPrecompilers(array $precompilers): void
{
Expand Down Expand Up @@ -592,7 +603,7 @@ protected function getDirectiveArgsInnerContent(DirectiveNode $directive): strin
/**
* Compile the given Blade template contents.
*
* @param string $template The template.
* @param string $template The template.
*
* @throws Exception
* @throws UnsupportedNodeException
Expand Down Expand Up @@ -746,6 +757,10 @@ public function compileString(string $template): string

$compiled = (string) $this->compilationBuffer;

// Normalize first.
$compiled = str_replace("\r\n", "\n", $compiled);

// Then, replace line endings with the desired ending.
$compiled = str_replace("\n", $lineEnding, $compiled);

if (count($this->footer) > 0) {
Expand Down Expand Up @@ -785,7 +800,7 @@ private function appendToBuffer(AbstractNode $node, string $value): void
/**
* Execute user defined extensions.
*
* @param string $value The value to compile.
* @param string $value The value to compile.
*/
protected function compileExtensions(string $value): string
{
Expand All @@ -799,7 +814,7 @@ protected function compileExtensions(string $value): string
/**
* Add the stored footers to the compiled template.
*
* @param string $result The compiled template.
* @param string $result The compiled template.
*/
protected function addFooters(string $result): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/CompilerServices/DirectiveNameValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DirectiveNameValidator
/**
* Tests if the provided directive name is valid.
*
* @param string $name The directive name.
* @param string $name The directive name.
*/
public static function isNameValid(string $name): bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/CompilerServices/LiteralContentHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class LiteralContentHelpers
/**
* Reverses Blade literal content escape sequences in the provided content.
*
* @param string $content The content.
* @param string $content The content.
*/
public static function getUnescapedContent(string $content): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/CompilerServices/LoopVariablesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct()
/**
* Extracts information about the loop variables in the provided value.
*
* @param string $value The content
* @param string $value The content
*/
public function extractDetails(string $value): LoopVariables
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/CompilerServices/StringSplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private function isBreakableWhitespace(?string $char): bool
/**
* Splits a string on whitespace into an array, ignoring line breaks and embedded strings.
*
* @param string $input The string to split.
* @param string $input The string to split.
* @return string[]
*/
public function split(string $input): array
Expand Down
24 changes: 12 additions & 12 deletions src/Compiler/CompilerServices/StringUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StringUtilities
/**
* Lowercases the first character of the input string.
*
* @param string $value The value.
* @param string $value The value.
*/
public static function lcfirst(string $value): string
{
Expand All @@ -20,8 +20,8 @@ public static function lcfirst(string $value): string
/**
* Safely wraps the provided value in the provided quote style, if it is not already.
*
* @param string $value The value to wrap.
* @param string $quoteStyle The quote style. Supply either ' or "
* @param string $value The value to wrap.
* @param string $quoteStyle The quote style. Supply either ' or "
*/
public static function wrapInQuotes(string $value, string $quoteStyle): string
{
Expand All @@ -40,7 +40,7 @@ public static function wrapInQuotes(string $value, string $quoteStyle): string
/**
* Escapes single quotes within the provided string.
*
* @param string $value The value to escape.
* @param string $value The value to escape.
*/
public static function escapeSingleQuotes(string $value): string
{
Expand All @@ -50,7 +50,7 @@ public static function escapeSingleQuotes(string $value): string
/**
* Normalizes line endings within the provided content.
*
* @param string $content The content to normalize.
* @param string $content The content to normalize.
*/
public static function normalizeLineEndings(string $content): string
{
Expand All @@ -60,7 +60,7 @@ public static function normalizeLineEndings(string $content): string
/**
* Wraps the provided value in single quotes if it is not already.
*
* @param string $value The string to wrap.
* @param string $value The string to wrap.
*/
public static function wrapInSingleQuotes(string $value): string
{
Expand All @@ -78,7 +78,7 @@ public static function wrapInSingleQuotes(string $value): string
/**
* Removes balanced parentheses from the provided string.
*
* @param string $value The value to unwrap.
* @param string $value The value to unwrap.
*/
public static function unwrapParentheses(string $value): string
{
Expand All @@ -105,7 +105,7 @@ public static function unwrapString(string $value): string
/**
* Tests if the provided value has leading whitespace.
*
* @param string $value The value.
* @param string $value The value.
*/
public static function hasLeadingWhitespace(string $value): bool
{
Expand All @@ -119,7 +119,7 @@ public static function hasLeadingWhitespace(string $value): bool
/**
* Tests if the provided value has trailing whitespace.
*
* @param string $value The value.
* @param string $value The value.
*/
public static function hasTrailingWhitespace(string $value): bool
{
Expand All @@ -135,7 +135,7 @@ public static function hasTrailingWhitespace(string $value): bool
*
* A single space will be added to either the start or end.
*
* @param string $value The value.
* @param string $value The value.
*/
public static function ensureStringHasWhitespace(string $value): string
{
Expand All @@ -153,7 +153,7 @@ public static function ensureStringHasWhitespace(string $value): string
/**
* Retrieves the leading whitespace from the provided value.
*
* @param string $value The value.
* @param string $value The value.
*/
public static function extractLeadingWhitespace(string $value): string
{
Expand All @@ -177,7 +177,7 @@ public static function extractLeadingWhitespace(string $value): string
/**
* Retrieves the trailing whitespace from the provided value.
*
* @param string $value The value.
* @param string $value The value.
*/
public static function extractTrailingWhitespace(string $value): string
{
Expand Down
8 changes: 7 additions & 1 deletion src/Compiler/ComponentNodeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ protected function compileParameterEcho(ParameterNode $node): string
$compiler = new Compiler(new DocumentParser());
$compiler->setCompilationTarget(CompilationTarget::ComponentParameter);

return $compiler->compileString($node->value);
$result = $compiler->compileString($node->value);

if ($node->type == ParameterType::InterpolatedValue && Str::startsWith($node->value, '{{') && Str::endsWith($node->value, '}}')) {
$result = "'".$result."'";
}

return $result;
}

protected function toAttributeArray(ComponentNode $component): array
Expand Down
7 changes: 7 additions & 0 deletions src/Compiler/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public function __construct(ComponentNodeCompiler $compiler, DocumentParser $par
$this->parser = $parser;
}

public function ignoreDirectives(array $directives): ComponentTagCompiler
{
$this->parser->ignoreDirectives($directives);

return $this;
}

public function registerCustomCompiler(string $tagName, CustomComponentTagCompiler $compiler): ComponentTagCompiler
{
$this->customCompilers[$tagName] = $compiler;
Expand Down
Loading

0 comments on commit 650c1a8

Please sign in to comment.