首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Polybuild PHP文件

Polybuild PHP文件
EN

Stack Overflow用户
提问于 2015-12-08 23:23:41
回答 1查看 211关注 0票数 3

我使用的是Polmer1.0,我真的很喜欢它,但我似乎不能让polybuild工具与任何服务器端脚本一起工作。例如,使用一个简单的示例test.php文件

代码语言:javascript
复制
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="PHP Test">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP Test</title>

    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&amp;lang=en">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.5/material.blue-green.min.css">
    <link rel="stylesheet" href="css/my-css.css">


    <!-- jQuery -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    <!-- Polymer -->
    <script src="components/webcomponentsjs/webcomponents-lite.min.js" async></script>
    <link rel="import" href="components/paper-styles/paper-styles.html">
    <link rel="import" href="components/neon-animation/neon-animated-pages.html" async>
    <link rel="import" href="components/neon-animation/neon-animations.html" async>
  </head>
  <body class="mdl-demo mdl-color--grey-100 mdl-color-text--grey-700 mdl-base fullbleed">

    <?php
    echo 'this is PHP';
    ?>

    <!-- JavaScript -->
    <script src="https://storage.googleapis.com/code.getmdl.io/1.0.5/material.min.js"></script>
    <script src="js/my-javascript.js"></script>
  </body>
</html>

这个文件只是按照预期显示了回显的php文本。我使用命令

代码语言:javascript
复制
polybuild --maximum-crush test.php

我得到了两个文件test.build.htmltest.build.js,但它只是去掉了php。我想把php留在里面,但我不确定使用polybuild工具是否可行。

EN

回答 1

Stack Overflow用户

发布于 2016-01-04 09:20:21

给定源文件test.php和来自polybuild的输出文件test.build.html,您可以使用tokenizer extension分析test.php,然后推断将php代码片段放到test.build.html中的位置。

一个粗略的例子

实际上,对于复杂的vulcan导入或test.php中复杂的php和html嵌套来说,这将是困难的,但对于您的示例场景来说,这就足够了。如果你给我一个样例test.build.html,我会确保我的代码能完美地为你工作。我不得不在一个人工设计的版本上进行测试。

下面是它的工作原理。像以前一样运行polybuild --maximum-crush test.php,然后运行php polybuild.php test.php test.build.html > test.build.modified.html。如果你看一看test.build.modified.html,你会发现它是test.build.html和来自test.php的php,神奇地在它应该在的地方!

这是polybuild.php的源代码。

代码语言:javascript
复制
<?php
$buildSource = $argv[1];
$buildHtml   = $argv[2];

$polyPhp = new PolyPhp($buildSource, $buildHtml);
echo $polyPhp->run();

class PolyPhp
{
    private
        $buildSource,
        $buildHtml,
        $sourceAsLines,
        $buildAsLines;

    public function __construct($buildSource, $buildHtml)
    {
        $this->buildSource = $buildSource;
        $this->buildHtml   = $buildHtml;

        // Load the source file as an array of lines
        $this->sourceAsLines = file($buildSource);

        // Load the build file as an array of lines
        $this->buildAsLines = file($buildHtml);
    }

    public function run()
    {
        // Get the tokens from the source php script
        $tokens = token_get_all(file_get_contents($this->buildSource));

        $matchStart = 0;
        $priorHtml  = '';
        foreach($tokens as $token) {
            if(!is_array($token)) {
                continue;
            }

            // Record HTML snippets
            if(token_name($token[0]) == 'T_INLINE_HTML') {
                $priorHtml = $token[1];
            }

            // When we find a php open tag, record the line it occurs on
            if(token_name($token[0]) == 'T_OPEN_TAG') {
                $matchStart = $token[2];
                continue;
            }

            // When we find a closing php tag, reassemble the php block
            // from the buildSource file, then determine where to inject it
            // in the buildHtml file and inject it.
            if(token_name($token[0]) == 'T_CLOSE_TAG') {
                $injectionPoint = $this->findInjectionPoint($priorHtml);
                $php            = $this->extractPhp($matchStart - 1, $token[2]);

                $this->injectPhp($injectionPoint, $php);
            }
        }

        return implode('', $this->buildAsLines);
    }

    /**
     * Inject PHP from the buildSource file
     * into the buildHtml file.
     */
    private function injectPhp($injectPoint, $php)
    {
        $this->buildAsLines = array_merge(
            array_slice($this->buildAsLines, 0, $injectPoint),
            $php,
            array_slice($this->buildAsLines, $injectPoint + 1));
    }

    /**
     * Extract PHP from the buildSource file.
     */
    private function extractPhp($startLine, $finishLine)
    {
        return array_slice($this->sourceAsLines, $startLine, $finishLine - $startLine);
    }

    /**
     * Determine where the injection point in buildHtml is.
     */
    private function findInjectionPoint($priorHtml)
    {
        $lines   = explode("\n", trim($priorHtml));
        $lastTag = trim(array_pop($lines));

        foreach($this->buildAsLines as $line => $tag) {
            $tag = trim($tag);
            if($lastTag == $tag) {
                return $line + 1;
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34159615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档