2017 © Pedro Peláez
 

library typo3-typoscript-parser

Parser for the TYPO3 configuration language TypoScript.

image

helmich/typo3-typoscript-parser

Parser for the TYPO3 configuration language TypoScript.

  • Thursday, May 31, 2018
  • by helmich
  • Repository
  • 2 Watchers
  • 9 Stars
  • 19,189 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 17 Versions
  • 20 % Grown

The README.md

TypoScript Parser

Build Status Code Climate Test Coverage Dependabot Status, (*1)

Author

Martin Helmich (typo3 at martin-helmich dot de), (*2)

Synopsis

This package contains a library offering a tokenizer and a parser for TYPO3's configuration language, "TypoScript"., (*3)

Why?

Just as typoscript-lint, this project started of as a simple programming excercise. Tokenizer and parser could probably implemented in a better way (it's open source, go for it!)., (*4)

Usage

Parsing TypoScript

You can use the Helmich\TypoScriptParser\Parser\Parser class to generate a syntax tree from source code input. The class requires an instance of the Helmich\TypoScriptParser\Tokenizer\Tokenizer class as dependency. When using the Symfony DependencyInjection component, you can simply use the service parser for this., (*5)

use Helmich\TypoScriptParser\Parser\Parser,
    Helmich\TypoScriptParser\Tokenizer\Tokenizer;

$typoscript = file_get_contents('path/to/typoscript.ts');
$parser     = new Parser(new Tokenizer());
$statements = $parser->parse($typoscript);

Analyzing TypoScript

You can analyze the generated syntax tree by implementing visitors. For example, let's implement a check that checks for non-CGL-compliant variable names (there's probably no use case for that, just as a simple example):, (*6)

First, we need the respective visitor implementation:, (*7)

use Helmich\TypoScriptParser\Parser\Traverser\Visitor,
    Helmich\TypoScriptParser\Parser\AST\Statement,
    Helmich\TypoScriptParser\Parser\AST\Operator\Assignment,
    Helmich\TypoScriptParser\Parser\AST\NestedAssignment;

class VariableNamingCheckVisitor implements Visitor {
    public function enterTree(array $statements) {}
    public function enterNode(Statement $statement) {
        if ($statement instanceof Assignment || $statement instanceof NestedAssignment) {
            if (!preg_match(',^[0-9]+$,', $statement->object->relativePath)) {
                throw new \Exception('Variable names must be numbers only!');
            }
        }
    }
    public function exitNode(Statement $statement) {}
    public function exitTree(array $statements) {}
}

Then traverse the syntax tree:, (*8)

use Helmich\TypoScriptParser\Parser\Traverser\Traverser;

$traverser = new Traverser($statements);
$traverser->addVisitor(new VariableNamingCheckVisitor());
$traverser->walk();

The Versions

31/01 2017

dev-bugfix/comment-after-delete

dev-bugfix/comment-after-delete https://github.com/martin-helmich

Parser for the TYPO3 configuration language TypoScript.

  Sources   Download

MIT

The Requires

 

The Development Requires

03/04 2016
20/03 2016
19/06 2014

v1.0.0

1.0.0.0 https://github.com/martin-helmich

Parser for the TYPO3 configuration language TypoScript.

  Sources   Download

MIT

The Requires