且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel/Blade缓存CSS文件

更新时间:2023-02-26 08:00:36

一般说明

访问Laravel Blade视图时,它将生成一个临时文件,因此不必每次访问视图时都需要处理Blade语法.这些文件以文件名(文件路径的MD5哈希)存储在app/storage/view中.

通常,当您更改视图时,Laravel会在下次访问视图时自动重新生成这些文件,然后一切正常.通过比较 filemtime() 功能.在您的情况下,可能是有问题,并且没有重新生成临时文件.在这种情况下,您必须删除这些文件,以便可以重新生成它们.它没有任何害处,因为它们是从您的视图自动生成的,并且可以随时重新生成.它们仅用于缓存目的.

Usually when you change a view, Laravel regenerate these files automatically at the next view access and everything goes on. This is done by comparing the modification times of the generated file and the view's source file through the filemtime() function. Probably in your case there was a problem and the temporary file wasn't regenerated. In this case, you have to delete these files, so they can be regenerated. It doesn't harm anything, because they are autogenerated from your views and can be regenerated anytime. They are only for cache purposes.

通常,它们应该自动刷新,但是如果这些文件被卡住并且遇到类似此类的问题,则可以随时删除这些文件,但是正如我所说的,这些只是罕见的例外.

Normally, they should be refreshed automatically, but you can delete these files anytime if they get stuck and you have problems like these, but as I said these should be just rare exceptions.

以下所有代码均来自laravel/framerok/src/Illuminate/View/.我在原件上添加了一些额外的注释.

All the following codes are from laravel/framerok/src/Illuminate/View/. I added some extra comments to the originals.

Engines/CompilerEngine.php开始,我们掌握了理解力学的主要代码.

Starting from Engines/CompilerEngine.php we have the main code we need to understand the mechanics.

public function get($path, array $data = array())
{
    // Push the path to the stack of the last compiled templates.
    $this->lastCompiled[] = $path;

    // If this given view has expired, which means it has simply been edited since
    // it was last compiled, we will re-compile the views so we can evaluate a
    // fresh copy of the view. We'll pass the compiler the path of the view.
    if ($this->compiler->isExpired($path))
    {
        $this->compiler->compile($path);
    }

    // Return the MD5 hash of the path concatenated
    // to the app's view storage folder path.
    $compiled = $this->compiler->getCompiledPath($path);

    // Once we have the path to the compiled file, we will evaluate the paths with
    // typical PHP just like any other templates. We also keep a stack of views
    // which have been rendered for right exception messages to be generated.
    $results = $this->evaluatePath($compiled, $data);

    // Remove last compiled path.
    array_pop($this->lastCompiled);

    return $results;
}

检查是否需要再生

这将在Compilers/Compiler.php中完成.这是一项重要功能.根据结果​​,将决定是否应重新编译视图.如果返回的是false而不是true,则可能是无法重新生成视图的原因.

Check if regeneration required

This will be done in Compilers/Compiler.php. This is an important function. Depending on the result it will be decided whether the view should be recompiled. If this returns false instead of true that can be a reason for views not being regenerated.

public function isExpired($path)
{
    $compiled = $this->getCompiledPath($path);

    // If the compiled file doesn't exist we will indicate that the view is expired
    // so that it can be re-compiled. Else, we will verify the last modification
    // of the views is less than the modification times of the compiled views.
    if ( ! $this->cachePath || ! $this->files->exists($compiled))
    {
        return true;
    }

    $lastModified = $this->files->lastModified($path);

    return $lastModified >= $this->files->lastModified($compiled);
}

重新生成视图

如果视图已过期,它将重新生成.在Compilers\BladeCompiler.php中,我们看到编译器将遍历所有Blade关键字,并最终返回包含已编译PHP代码的字符串.然后它将检查是否设置了视图存储路径,并使用一个文件名将该文件保存在该文件中,该文件名是视图文件名的MD5哈希值.

Regenerate view

If the view is expired it will be regenerated. In Compilers\BladeCompiler.php we see that the compiler will loop through all Blade keywords and finally give back a string that contains the compiled PHP code. Then it will check if the view storage path is set and save the file there with a filename that is the MD5 hash of the view's filename.

public function compile($path)
{
    $contents = $this->compileString($this->files->get($path));

    if ( ! is_null($this->cachePath))
    {
        $this->files->put($this->getCompiledPath($path), $contents);
    }
}

评估

最后在Engines/PhpEngine.php中评估视图.它使用 extract() trycatch中具有传递路径的文件,所有具有handleViewException()的异常都将再次引发该异常.也有一些输出缓冲.

Evaluate

Finally in Engines/PhpEngine.php the view is evaluated. It imports the data passed to the view with extract() and include the file with the passed path in a try and catch all exceptions with handleViewException() that throws the exception again. There are some output buffering too.