Skip to content

Commit

Permalink
Use DI (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored May 22, 2024
1 parent 19abfc2 commit 50ccfc0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/StringParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Codefog\HasteBundle;

use Contao\ArrayUtil;
use Contao\CoreBundle\InsertTag\InsertTagParser;
use Contao\CoreBundle\String\SimpleTokenParser;
use Contao\StringUtil;
use Contao\System;

class StringParser
{
Expand All @@ -23,6 +24,12 @@ class StringParser

public const NO_ENTITIES = 16;

public function __construct(
private SimpleTokenParser $simpleTokenParser,
private InsertTagParser $insertTagParser,
) {
}

/**
* Recursively replace simple tokens and insert tags.
*/
Expand All @@ -36,13 +43,13 @@ public function recursiveReplaceTokensAndTags(string $text, array $tokens, int $
$text = StringUtil::decodeEntities($text);

// first parse the tokens as they might have if-else clauses
$buffer = System::getContainer()->get('contao.string.simple_token_parser')->parse($text, $tokens);
$buffer = $this->simpleTokenParser->parse($text, $tokens);

// then replace the insert tags
$buffer = System::getContainer()->get('contao.insert_tag.parser')->replaceInline($buffer);
$buffer = $this->insertTagParser->replaceInline($buffer);

// check if the insert tags have returned a simple token
if (str_contains((string) $buffer, '##') && $buffer !== $text) {
if (str_contains($buffer, '##') && $buffer !== $text) {
$buffer = $this->recursiveReplaceTokensAndTags($buffer, $tokens, $textFlags);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/StringParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Codefog\HasteBundle\Tests;

use Codefog\HasteBundle\StringParser;
use Contao\CoreBundle\InsertTag\InsertTagParser;
use Contao\CoreBundle\String\SimpleTokenParser;
use PHPUnit\Framework\TestCase;

class StringParserTest extends TestCase
Expand All @@ -14,7 +16,7 @@ class StringParserTest extends TestCase
*/
public function testFlatten(mixed $value, string $key, array $data, array $expected): void
{
$parser = new StringParser();
$parser = new StringParser($this->createMock(SimpleTokenParser::class), $this->createMock(InsertTagParser::class));
$parser->flatten($value, $key, $data);

$this->assertSame($expected, $data);
Expand Down

0 comments on commit 50ccfc0

Please sign in to comment.