我正在使用我非常喜欢的 Polymer 1.0,但我似乎无法使 polybuild
工具与任何服务器端脚本一起工作。例如,使用一个简单的示例 test.php 文件
<!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&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 文本。我使用命令
polybuild --maximum-crush test.php
我得到了两个文件 test.build.html
和 test.build.js
但它只是去掉了 php.ini 文件。我希望保留 php,但我不确定 polybuild 工具是否可行。
最佳答案
给定源文件 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
,其中的 php 来自 test.php
神奇地出现在它应该在的地方!
这是 polybuild.php
的源代码。
<?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;
}
}
}
}
关于php - Polybuild PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34159615/