定制 tacman/blade-parser 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tacman/blade-parser

Composer 安装命令:

composer require tacman/blade-parser

包简介

README 文档

README

This is an experimental Blade parser and compiler. It is able to parse Blade templates, and regenerate them.

Parsing a Blade Document

To parse a document, first create an instance of the Blade parser:

use Stillat\BladeParser\Parsers\Blade;

$parser = new Blade();

Once you have a parser instance, you may use the parse($input) method to receive an instance of Stillat\BladeParser\Documents\Template:

use Stillat\BladeParser\Parsers\Blade;

$parser = new Blade();

$document = $parser->parse('@extends("layout")');

The Template document instance contains information about the parsed Blade template. To work with the document, you must implement the Stillat\BladeParser\Visitors\AbstractNodeVisitor interface:

use Stillat\BladeParser\Nodes\Node;
use Stillat\BladeParser\Visitors\AbstractNodeVisitor;

class MyVisitor extends AbstractNodeVisitor
{

    public function onEnter(Node $node)
    {
        echo $node->innerContent;
    }
}

Once you have created your visitor, you must ask the document for a TemplateTraverser instance, add your new visitor, and then call the traverse() method:

$traverser = $document->getTraverser();
$traverser->addVisitor(new MyVisitor());

$traverser->traverse();

The onEnter method within all added traversers will be invoked with the document's nodes.

The built-in Stillat\BladeParser\Visitors\PrinterNodeVisitor and the Stillat\BladeParser\Printers\Php\Printer demonstrate advanced usage of the traverser mechanisms.

Component Details Resolver

The provided PHP compiler (printer) requires an instance of Stillat\BladeParser\Contracts\ComponentDetailsResovlerContract. This interface is responsible for providing information about the component's class name, properties, data, and attributes. This resolver will be environment specific, you may use the mocked test resolver at tests/AppComponentNameFinder.php as an example.

Using the Default PHP Compiler

To compile a Blade document from a collection of previously parsed nodes, you will first need to create an instance of Stillat\BladeParser\Contracts\ComponentDetailsResovlerContract. Once you have this interface implemented, you may set up a compiler like so:

use Stillat\BladeParser\Compilers\PhpCompiler;

$compiler = new PhpCompiler($yourComponentDetailsResolverInstance);


$compiledString = $compiler->compileString('@extends("some_file")');

Known Issues

Not all of the details provided in the Node instances are 100% accurate at this time. Pull requests are welcomed!

License

This parser library is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2022-11-18