Search code examples
typo3typoscript

How do I parse a Typoscript file?


I am writing a unit test and want to check if the data in a certain typoscript file satisfies my requirements. How do I convert the text file to an array? The Typo3-Framework is available.

My google research points to using the class \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser but I don't find usage examples ...

(Using Typo3 7.6)


Solution

  • This is working (but possibly there are nicer ways to do this):

    <?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));
        }
    }