dongpu3792 2014-08-26 11:54
浏览 69

PHP:从抽象生成代码(xml,json,数据库,......)

Note Feel free to suggest a better title as I think 'generate code' and 'abstraction' might not be the best expressions for what I want to ask :)

I'm looking for a good pattern/standard way to generate PHP code from some sort of abstraction layer.

Example

I've built a set of classes that act es text-processing filters. These filters can be chained together to transform some input-text into some other output text, e.g.

Input "Hello 'CRUEL' World" 
>> RegexFilter('(.*?)') [extract everything between '']
>> ToLowerFilter [lowercase the extracted text]
Output: cruel

Resulting Code

The corresponding source code looks something like this:

$input = "Hello 'CRUEL' world";
$pattern = "#(.*?)#";
$first = new RegexFilter($pattern);
$second = new ToLowerFilter();
$first->setNextFilter($second);
$ouput = $first->Transform($input);
echo $output; // cruel

I would like to create some sort of abstraction layer, so that the end user (non-programmer) can built his own filter chain without writing code by himself. By "abstraction" I mean something like XML, JSON, stored in a database, etc.

XML example (suggestion)

<filters>
 <filter name='Regex'>
  <properties>
   <property name='pattern'>'(.*)'</property>
  </properties>
  <nextfilter name='ToLower'>
   <properties/>
  </filter>
 </filter>
</filters> 

Remark: The example is very simplistic. Think of more advanced things like "complex objects as properties for filters", "inheritance of filters" (the above filter could get the name "RegexToLowerFilter" and can be referenced by other filters), etc.

Approaches / Ideas

Is there standard way to tackle this sort of problem? So far I've thought of building a custom XML, JSON, etc. serializer/deserializer for every filter but that seems like a lot of work and is potentially error prone because it's done individually for every filter. Further I guess that would require to make every possible dependency also serilizable in the same fashion - which can be problematic when using third party libraries (e.g. JsonPath).

I suppose the most elegant way would be the creation of Domain Specific Language. Unfortunately building a compiler is not the easiest task and probably not always appropriate for every problem in that space. (Then again, feel free to correct me :))

TL;DR

I'm looking for a suitable way / pattern to built an abstraction layer between the (non-progammer) end user and my source code. The solution should take the "simplified" abstraction and "generate" the corresponding code.

  • 写回答

1条回答 默认 最新

  • doujiao2443 2014-08-26 14:35
    关注

    What you're trying to do is called "Model Driven Development"

    Take a look at the various open source sub-projects in the Eclipse model-to-text (M2T) project. My favorite is JET, but there are others with different specification and runtime characteristics.

    Despite the fact that I'm a really big fan of using model driven development to solve these kinds of problems in general, I want to caution you about using these technologies (or your own implementation) to solve this specific problem. If your target audience is really a group of non-programmers then they will have just as much trouble learning your DSL (syntax and keywords) as they would the original language you're trying to hide. The comparison (DSL vs original language) gets worse if there are development tools (debuggers, editors, etc.) for the original language. Model driven technologies tend to be more successful when applied to more expert developers who need their work automated than when applied to less expert developers looking for a miracle.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭