如何从 PHP 执行 LESS 编译器(使用 Symfony)?
我在从 PHP 执行 lessc 编译器时遇到问题。我正在使用 Symfony,并尝试使用 sfLESSPlugin,但没有成功。我将我的代码放入一个在页面渲染之前执行的过滤器中,以便每次刷新页面时,我的 LESS 文件都会编译成一个 CSS 文件(不希望每次进行更改时都手动重新编译,至少在我开发过程中)。以下是我尝试过的不同变体:
$fs = new sfFilesystem();
$command = '/Users/jordanb/node/node_modules/less/bin/lessc less/bootstrap.less css/bootstrap.css';
try
{
$fs->execute($command, null, array($this, 'throwCompilerError'));
}
catch (RuntimeException $e)
{
return false;
}
这会返回一个错误:“执行命令时出现问题”,错误代码为 127。深入研究 Symfony 的execute(),它会调用 proc_open(),然后调用 proc_close()。网上的一些研究告诉我,错误代码 127 意味着未找到该命令。
在命令行上运行完全相同的命令效果很好。
为了更加确定,我在 /Users/jordanb/node/node_modules/less/bin/lessc 上执行了 chmod 777,只是为了确保这不是权限问题。还是没用。
我还尝试了“lessc”而不是完整路径,但这不起作用。我已将 lessc 添加到我的类路径中,因此输入“which lessc”会给出“/Users/jordanb/node/node_modules/less/bin/lessc”。
我也尝试了一个简单的:
shell_exec('lessc less/bootstrap.less css/bootstrap.css');
这似乎没有做任何事情。我将输出打印到 PHP 错误日志以及文本文件,这两种情况下输出都是空的。在这种情况下,我还尝试使用完整路径。
I'm having trouble executing the lessc compiler from PHP. I'm using Symfony, and have tried using the sfLESSPlugin, but have been unsuccessful. I've put my code in a filter that executes before the page renders, so that every time the page is refreshed, my LESS files are compiled into one CSS file (don't want to have recompile manually every time I make a change, at least while I'm developing). Here are the different variations that I've attempted:
$fs = new sfFilesystem();
$command = '/Users/jordanb/node/node_modules/less/bin/lessc less/bootstrap.less css/bootstrap.css';
try
{
$fs->execute($command, null, array($this, 'throwCompilerError'));
}
catch (RuntimeException $e)
{
return false;
}
This returns an error: "Problem executing command", with an error code of 127. Digging deeper into Symfony's execute(), it calls proc_open() and then proc_close(). Some research online told me that an error code of 127 means that the command was not found.
Running the exact same command on the command line works just fine.
To be extra sure, I executed chmod 777 on /Users/jordanb/node/node_modules/less/bin/lessc, just to make sure it wasn't a permissions issue. Still didn't work.
I also tried just "lessc" instead of the full path, which didn't work. I've added lessc to my classpath, so typing "which lessc" gives me "/Users/jordanb/node/node_modules/less/bin/lessc".
I also tried a simple:
shell_exec('lessc less/bootstrap.less css/bootstrap.css');
which didn't seem to do anything. I printed the output to the PHP error log as well as to a text file, and the output was empty in both cases. I also tried using the full path in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以制作一个按照您想要的方式编译内容的 shell 脚本:
compile_css.sh
然后
最后从您的脚本中调用它
这将使您更好地控制可以执行的内容,但如果您仍然想从 php 执行此操作,请尝试此:
You can make a shell script that compiles things the way you want:
compile_css.sh
Then
And finally call it from your script
This will give you more control over what you can execute, but if you still want to do it from php, try this: