如何解析Typoscript文件?

时间:2017-08-26 08:26:32

标签: typo3 typoscript

我正在编写单元测试,想要检查某个typoscript文件中的数据是否满足我的要求。如何将文本文件转换为数组?可以使用Typo3-Framework。

我的谷歌研究指出使用课程\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser,但我找不到用法示例......

(使用Typo3 7.6)

2 个答案:

答案 0 :(得分:0)

这是有效的(但可能有更好的方法):

<?php
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;

class TyposcriptTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
    public function setUp() {
        parent::setUp();
        $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');  
    }

    protected function loadTSFile($filename) {
        $content = file_get_contents($filename);
        $parser = $this->objectManager->get(TypoScriptParser::class);
        $parser->parse($content);

        return $parser->setup;  
    }

    public function testTS() {
        $array = $this->loadTSFile('...');
        $this->assertTrue(isset($array['tx_extension.']['flexForm.']['andsoon.']]), 'Assertion failed. TS content: ' . var_export($array, true));
    }
}

答案 1 :(得分:0)

这是 TYPO3 v - 10.4.x 的工作示例

    $tsString = 'colors {
          backgroundColor = red
          fontColor = blue
        }

        [ip("123.45.*")]

          headerImage = fileadmin/img1.jpg

        [ELSE]

          headerImage = fileadmin/img2.jpg

        [GLOBAL]

        // Wonder if this works... :-)
        wakeMeUp = 7:00';

        $TSparserObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
              \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class
        );
        $TSparserObject->parse($tsString);

        echo '<pre>';
        print_r($TSparserObject->setup);
        echo '</pre>';