Skip to content

Commit

Permalink
refactoring, added Helpers::startsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 27, 2016
1 parent cfefb74 commit 003e484
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Latte/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ private function processHtmlTagEnd(Token $token)

private function processHtmlAttributeBegin(Token $token)
{
if (strncmp($token->name, Parser::N_PREFIX, strlen(Parser::N_PREFIX)) === 0) {
if (Helpers::startsWith($token->name, Parser::N_PREFIX)) {
$name = substr($token->name, strlen(Parser::N_PREFIX));
if (isset($this->htmlNode->macroAttrs[$name])) {
throw new CompileException("Found multiple attributes $token->name.");
Expand All @@ -452,7 +452,7 @@ private function processHtmlAttributeBegin(Token $token)
$context = NULL;
if (in_array($this->contentType, [self::CONTENT_HTML, self::CONTENT_XHTML], TRUE)) {
$lower = strtolower($token->name);
if (substr($lower, 0, 2) === 'on') {
if (Helpers::startsWith($lower, 'on')) {
$context = self::CONTENT_JS;
} elseif ($lower === 'style') {
$context = self::CONTENT_CSS;
Expand Down Expand Up @@ -543,7 +543,7 @@ public function closeMacro($name, $args = NULL, $modifiers = NULL, $isRightmost
$node = $this->macroNode;

if (!$node || ($node->name !== $name && '' !== $name) || $modifiers
|| ($args && $node->args && strncmp("$node->args ", "$args ", strlen($args) + 1))
|| ($args && $node->args && !Helpers::startsWith("$node->args ", "$args "))
|| $nPrefix !== $node->prefix
) {
$name = $nPrefix
Expand Down
4 changes: 2 additions & 2 deletions src/Latte/Compiler/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Parser
*/
public function parse($input)
{
if (substr($input, 0, 3) === "\xEF\xBB\xBF") { // BOM
if (Helpers::startsWith($input, "\xEF\xBB\xBF")) { // BOM
$input = substr($input, 3);
}

Expand Down Expand Up @@ -195,7 +195,7 @@ private function contextHtmlTag()
$token->value = isset($matches['value']) ? $matches['value'] : '';

if ($token->value === '"' || $token->value === "'") { // attribute = "'
if (strncmp($token->name, self::N_PREFIX, strlen(self::N_PREFIX)) === 0) {
if (Helpers::startsWith($token->name, self::N_PREFIX)) {
$token->value = '';
if ($m = $this->match('~(.*?)' . $matches['value'] . '~xsi')) {
$token->value = $m[1];
Expand Down
10 changes: 10 additions & 0 deletions src/Latte/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ public static function removeFilter(& $modifier, $filter)
return (bool) $found;
}


/**
* Starts the $haystack string with the prefix $needle?
* @return bool
*/
public static function startsWith($haystack, $needle)
{
return strncmp($haystack, $needle, strlen($needle)) === 0;
}

}
2 changes: 1 addition & 1 deletion src/Latte/Loaders/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct($baseDir = NULL)
public function getContent($file)
{
$file = $this->baseDir . $file;
if ($this->baseDir && strncmp($this->normalizePath($file), $this->baseDir, strlen($this->baseDir))) {
if ($this->baseDir && !Latte\Helpers::startsWith($this->normalizePath($file), $this->baseDir)) {
throw new \RuntimeException("Template '$file' is not within the allowed path '$this->baseDir'.");

} elseif (!is_file($file)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Latte/Macros/CoreMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function macroElse(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
} elseif ($node->args) {
$hint = substr($node->args, 0, 2) === 'if' ? ', did you mean {elseif}?' : '';
$hint = Helpers::startsWith($node->args, 'if') ? ', did you mean {elseif}?' : '';
throw new CompileException('Arguments are not allowed in ' . $node->getNotation() . $hint);
}
$ifNode = $node->parentNode;
Expand Down Expand Up @@ -261,7 +261,7 @@ public function macroUse(MacroNode $node, PhpWriter $writer)
public function macroCapture(MacroNode $node, PhpWriter $writer)
{
$variable = $node->tokenizer->fetchWord();
if (substr($variable, 0, 1) !== '$') {
if (!Helpers::startsWith($variable, '$')) {
throw new CompileException("Invalid capture block variable '$variable'");
}
$this->checkExtraArgs($node);
Expand Down

0 comments on commit 003e484

Please sign in to comment.