无法破坏活动的 lambda 函数,嵌套输出缓冲区 wordpress scss 编译器

Cannot destroy active lambda function, nested output buffer wordpress scss compiler

提问人:Kling93 提问时间:10/15/2021 最后编辑:Kling93 更新时间:10/15/2021 访问量:54

问:

我正在使用 wordpress 并使用操作“template_redirect”在发送到浏览器之前启动 html 内容的输出缓冲区。

我已经导入了一个库,可以在 php 中将 SCSS 转换为 css 代码 https://scssphp.github.io/scssphp

下面是一个代码示例

$compiler = new ScssPhp\ScssPhp\Compiler();

add_action( 'template_redirect', 'replace_scss', -100 );
function replace_scss(){
    ob_start( function( $buffer ) {
        $scss = 'body { p { color: red; } }';
            $css = $compiler->compile($scss);
            if(!empty($css) && is_string($css)) return preg_replace('scss_compiled', $css, $buffer );
            else return $buffer;
    }); 
}

编辑:// 以下功能在正常工作时输出

body p { color: red; }

但是,我收到以下错误。

PHP Fatal error:  Cannot destroy active lambda function in scss_compiler\src\Formatter.php on line 287

我假设这是因为在格式化程序中.php它提到了ob_start; 当它已经在回调中时,您无法启动ob_start。

我想知道有没有其他方法可以获取 HTML 的内容,进行更改然后返回浏览器?

php wordpress sass 输出缓冲 ob-start

评论


答:

-1赞 Kling93 10/15/2021 #1

我解决了我自己的答案。

//from the formatter.php in scss php
public function format(OutputBlock $block, SourceMapGenerator $sourceMapGenerator = null)
  {
      $this->sourceMapGenerator = null;

      if ($sourceMapGenerator) {
          $this->currentLine        = 1;
          $this->currentColumn      = 0;
          $this->sourceMapGenerator = $sourceMapGenerator;
      }

      $this->testEmptyChildren($block);

      //ob_start();

      $this->block($block);

      $out = ob_get_contents();

      return $out;
  }

我注释掉了并替换为ob_start();$out = ob_get_clean();$out = ob_get_contents();

至于代码

add_action( 'template_redirect', 'scss_compiler', -100 );
function scss_compiler(){
    ob_start('scss');
    ob_end_flush();
}

function scss($buffer){
    $scss = 'body { p { color: red; } }';
    $compiler = new ScssPhp\ScssPhp\Compiler();
    $css = $compiler->compileString($scss)->getCss();
    error_log($css);
    return $buffer;
}