Thursday, April 3, 2014

Wie kann ich eine TypoScript-Constants Datei ohne die TYPO3-Frontend-Umgebung benutzen/lesen/parsen/auswerten?


Eine grundlegende Beschreibung zum parsen von TypoScript-Dateien befindet sich hier:
http://docs.typo3.org/typo3cms/TyposcriptSyntaxReference/TypoScriptParserApi/Index.html

Diese wendet sich aber an (Core-)Extension-Entwickler, die z.b. TypoScript-Erweiterungen implementieren moechten.

Falls man die bestehenden TypoScript-Moeglichkeiten, d.h. das bestehende Framework nutzen moechte, wird etwas mehr benoetigt.

Hier ein gekuerztes Beispiel, das aus folgender Situation entstanden ist: in einem eigenen PHP-Page-Not-Found-Handler sollen Werte aus einer TypoScript-Constants-Datei verwendet werden. Im Page-Not-Found-Handler stehen die Werte aus $GLOBALS['TSFE'] nur unvollstaendig zur Verfuegung (da ja keine Seite gefunden wurde), und vor allem kein $GLOBALS['TSFE']->tmpl->setup (d.h. ausgewertetes TypoScript-Setup).


require_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') . 'Classes/Utility/GeneralUtility.php');
require_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') . 'Classes/TypoScript/Parser/TypoScriptParser.php');
require_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('frontend') . 'Classes/Configuration/TypoScript/ConditionMatching/ConditionMatcher.php');


class myPhpObjectClass {
    function myMethod() {
        // 1. read our typoscript constants file:
        $constants_ts = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/path/to/your/constantsfile.ts');
        // 2. build typoscript parser:
        $TSparserObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser');
        // 3. build matchObject for TypoScript conditions to work (ooh, very typo3-ish...):
        $matchObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching\ConditionMatcher');
        // 4. add to (incomplete, because page-not-found) $GLOBALS['TSFE'], required by our $matchObject
        //    (actually only rootLine required, but we build complete tmpl object, just for the sake of typo3-ness...):
        $GLOBALS['TSFE']->tmpl = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\TypoScript\TemplateService');
        $GLOBALS['TSFE']->tmpl->rootLine = $pObj->rootLine;
        // 5. parse typoscript (ooh, again, very typo3-ish: must check include lines... separately...)
        $constants_ts = \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::checkIncludeLines($constants_ts);
        $TSparserObject->parse($constants_ts, $matchObject);
        // 6. whatever we have parsed is now in $TSparserObject->setup:
        $constants = $TSparserObject->setup;
        // 7. finally, we can grab the values needed! :-)
        $myValue = $constants['myValueFromConstantsfile'];

    }
}

(entwickelt mit TYPO3 6.1.7)

Moegliche Verbesserungen:
- Wie parsed man eine Setup-Datei einschliesslich Konstanten (z.b. {$myConstant})?
- bessere Methode (statt file_get_contents($_SERVER['DOCUMENT_ROOT']...)) zum Einlesen der TypoScript-Datei?

No comments:

Post a Comment