php pre,一个PHP的预处理库

PHP PreProcessor

What is this? It was an attempt at pre-processing of include/require'd files in PHP.

TLDR: Just use preprocess.io...

How does it work

Dark magic and voodoo. Don't worry about it

Included Transformations

Json - For including .json files

This transformation will let you load json files by simply calling $data = require 'path/to/data.json';. The $data that's returned is automatically parsed.

Yaml - For including .yaml files

This transformation will automatically load yaml files for you.

How to enable

Create a base PHP file to declare your included transformations.

boot.php

use PhpPreprocessor\PreProcessor;

Then, to enable parsing of .json files with the Json transformation, add it to the preprocessor

PreProcessor::instance()

->addTransform('json', new PhpPreprocessor\Transform\Json);

You can add multiple transforms together:

PreProcessor::instance()

->addTransform('json', new PhpPreprocessor\Transform\Json)

->addTransform('yaml', new PhpPreprocessor\Transform\Yaml);

Finally, define an autoload file in your composer.json:

"autoload": {

"files": [

"path/to/boot.php"

]

}

Now, just use it!

Important Notes

You can only install one transformation per file extension. Additional or redeclarations will result in an exception.

You can declare a "global" transformation that will be run on all included files (after a prior transformation is run if matched). Use the PreProcessor::EXT_ALL constant in place of an extension.

To Save Runtime Cost

All of this parsing happens at runtime, which can be expensive. This is useful for development, but what about production?

You can pre-process your codebase using the cli tool preprocessor. This will rewrite every file with a matching processer extension (and php files) using the transforms and move them into a new build directory. Non-matching files are copied into the new directory without processing.

A token is included in the processed file <?php '🧙'; to prevent re-processing of the file in a production environment.

$ vendor/bin/preprocessor build srcDir destDir

You can also specify a "boot file" which declares the transforms as a third argument:

$ vendor/bin/preprocessor build srcDir destDir srcDir/path/to/boot.php

How do I build transformations?

Simply build a class that implements the PhpPreprocessor\Transform interface:

use PhpPreprocessor\Transform;

class MyTransform implements Transform {

public function transform(string $data): string

{

}

}

The transform method will be called for every matched file, with the $data parameter containing the included file contents.

The returned string MUST be valid PHP code.

Pre vs Post Parsing

Json is a good example of the tradeoffs of parsing location. Should you parse the JSON in the transformation and export the result, or should you render the parsing code. The Json transform does both optionally.

The reason this is important is because of error handling. The Yaml parser throws an exception on invalid YAML. Using Transform::PREPROCESS means that the exception will happen when building (if you're using the build tool). Using Transform::POSTPROCESS will always have the exception occur in the require call.

Credits:

Loading mechanism was heavily inspired and partially derived by Patchwork

Also heavily dependent and derived from Php-Parser...

Status of the project

Right now, it's just a proof-of-concept. Because of that, use preprocess.io if you want similar functionality...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值