Php 进程未从proc_open获取管道。动态样式表语言

Php 进程未从proc_open获取管道。动态样式表语言,php,linux,shell,pipe,less,Php,Linux,Shell,Pipe,Less,以下是LESC软件的源代码,我认为它会很有帮助: https://github.com/cloudhead/less.js/blob/master/bin/lessc 问题 我在shell中使用LESC只是: lessc file.less 我得到一个css文件输出 我试着用php和proc_open来实现它。但当我将输入文件传输到proc时,lessc并没有得到它。我有一个错误(来自管道1): 在shell中等效于(未传递参数): 我的代码: $descriptorspec = array

以下是LESC软件的源代码,我认为它会很有帮助:

https://github.com/cloudhead/less.js/blob/master/bin/lessc
问题

我在shell中使用LESC只是:

lessc file.less
我得到一个css文件输出

我试着用php和proc_open来实现它。但当我将输入文件传输到proc时,lessc并没有得到它。我有一个错误(来自管道1):

在shell中等效于(未传递参数):

我的代码:

$descriptorspec = array(
    0 => array("file", 'path/to/file/foo.less', "r"), 
    1 => array("pipe", "w"), 
    2 => array("file", '/tmp/lessCompiler-errors', "a")
);

$process = proc_open('lessc', $descriptorspec, $pipes);

if (is_resource($process)) {
    $contents = stream_get_contents($pipes[1]);
    fclose($pipes[1]);    
    proc_close($process);
}
顺便说一句,我试图避免使用exec()函数

我将非常乐意接受任何帮助。
Marcin

我今天遇到了同样的问题,在你发布问题时不确定解决方案是否有效,但现在你可以将“-”作为输入源传递给lessc,以便从stdin读取。因此,您只需更改一行:

$process = proc_open('lessc -', $descriptorspec, $pipes);
我花了一些时间才弄清楚所有细节,下面是我的代码片段:

/* run lessc */
$descriptors = array(
    0 => array( 'pipe', 'r' ),
    1 => array( 'pipe', 'w' ),
    2 => array( 'pipe', 'w' ));
$process = proc_open( 'lessc --no-color -x -', $descriptors, $pipes,
    '/web/less', array( 'PATH' => '/usr/local/bin/' ) );

if( !is_resource( $process ) ) {
    $this->errorMessage( 'Unable to start lessc' );
    exit();
}

/* write generated content */
fwrite( $pipes[0], 'some dynamically generated less code' );
fclose( $pipes[0] );

/* read compiled css */
$css = stream_get_contents( $pipes[1] );
fclose( $pipes[1] );

/* check for errors */
if( $stderr = stream_get_contents( $pipes[2] ) ) {
    $this->errorMessage( "lessc error: {$stderr}" );
    exit();
}
fclose( $pipes[2] );

proc_close( $process );

/* write back */
$this->write( $css );
  • 我不得不在debian wheezy上设置
    路径
    ,因为lessc一直在抱怨:
    /usr/bin/env:node:没有这样的文件或目录
    ,请参阅
  • --no color
    禁止错误消息中的转义码
  • -x
    压缩css

你知道有一个,对吧?
lessc
甚至从
STDIN
读取吗?描述符规范不是参数。。。。OTOH,这相当于
cat file.less | lessc 2>/tmp/lessCompiler error
…@JamWaffles是的,我知道。但我必须使用lessc。无论如何,谢谢你的建议。@Wrikken不幸的是,它没有。所以我不能用open_proc这么做?或者可能有一些解决方法吗?为什么不简单地将
STDIN
作为手册中的默认值,并使用
proc\u打开('lessc/path/to/file.less',…
?使用
escapeshallarg
,只是为了确定一下。是的,就是这样。谢谢您的回答:)
$process = proc_open('lessc -', $descriptorspec, $pipes);
/* run lessc */
$descriptors = array(
    0 => array( 'pipe', 'r' ),
    1 => array( 'pipe', 'w' ),
    2 => array( 'pipe', 'w' ));
$process = proc_open( 'lessc --no-color -x -', $descriptors, $pipes,
    '/web/less', array( 'PATH' => '/usr/local/bin/' ) );

if( !is_resource( $process ) ) {
    $this->errorMessage( 'Unable to start lessc' );
    exit();
}

/* write generated content */
fwrite( $pipes[0], 'some dynamically generated less code' );
fclose( $pipes[0] );

/* read compiled css */
$css = stream_get_contents( $pipes[1] );
fclose( $pipes[1] );

/* check for errors */
if( $stderr = stream_get_contents( $pipes[2] ) ) {
    $this->errorMessage( "lessc error: {$stderr}" );
    exit();
}
fclose( $pipes[2] );

proc_close( $process );

/* write back */
$this->write( $css );