helmich/typo3-typoscript-parser
Composer 安装命令:
composer require helmich/typo3-typoscript-parser
包简介
Parser for the TYPO3 configuration language TypoScript.
README 文档
README
Author
Martin Helmich (typo3 at martin-helmich dot de)
Synopsis
This package contains a library offering a tokenizer and a parser for TYPO3's configuration language, "TypoScript".
Why?
Just as typoscript-lint, this project started of as a simple programming exercise. Tokenizer and parser could probably be implemented in a better way (it's open source, go for it!).
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 use the parser service for this.
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):
First, we need the respective visitor implementation:
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:
use Helmich\TypoScriptParser\Parser\Traverser\Traverser; $traverser = new Traverser($statements); $traverser->addVisitor(new VariableNamingCheckVisitor()); $traverser->walk();
Printing TypoScript
When you are using this package for code transformation,
you might want to print a modified syntax tree back into a file.
You can use the PrettyPrinter class for this:
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinter; use Symfony\Component\Console\Output\StreamOutput; $syntaxTree = [...]; $output = new StreamOutput(fopen('path/to/file', 'w')); $printer = new PrettyPrinter(); $printer->printStatements($syntaxTree, $output);
To get more fine-grained control over the output, you can pass a configuration object into your printer instance:
$printerConfiguration = PrettyPrinterConfiguration::create() ->withSpaceIndentation(2) ->withIndentConditions() ->withClosingGlobalStatement() ->withConditionTermination(PrettyPrinterConditionTermination::EnforceEnd); $printer = new PrettyPrinter($printerConfiguration); $printer->printStatements($syntaxTree, $output);
Dumping the AST
You can dump out the AST with the following code:
use Helmich\TypoScriptParser\Parser\ParseError; use Helmich\TypoScriptParser\Parser\Parser; use Helmich\TypoScriptParser\Parser\StatementDumper; use Helmich\TypoScriptParser\Tokenizer\Tokenizer; $code = <<<'CODE' # A comment CODE; $parser = new Parser(new Tokenizer()); try { $statements = $parser->parseString($code); } catch (ParseError $error) { echo "Parse error: {$error->getMessage()}"; exit; } $dumper = new StatementDumper(); echo $dumper->dump($statements);
helmich/typo3-typoscript-parser 适用场景与选型建议
helmich/typo3-typoscript-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.4M 次下载、GitHub Stars 达 36, 最近一次更新时间为 2014 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 helmich/typo3-typoscript-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 helmich/typo3-typoscript-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.4M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 36
- 点击次数: 23
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-06-19