vicgutt/laravel-components-backport

将Laravel 7+的Blade组件回溯到Laravel 5.8+

v0.0.1 2020-11-08 22:00 UTC

This package is auto-updated.

Last update: 2024-09-14 21:06:02 UTC


README

本包将Laravel 7+的Blade组件支持回溯到较旧版本的Laravel。本包已在Laravel 5.8 & 6上进行了测试。如果我们在composer.json文件中将illuminate依赖项降低,较旧的Laravel版本也可能正常工作。由于我个人没有需求,我没有测试5.8之前的版本,但我欢迎任何PR 👌。

本包不试图比Laravel 8+提供的内容更多或更少。

安装

通过composer安装包

composer require vicgutt/laravel-components-backport

此包会自动注册其提供者,并作为您的应用程序的新ViewServiceProvider运行。因此,您应该在config/app.php中注释掉默认的配置。

// config/app.php
'providers' => [
    /*
    * Laravel Framework Service Providers...
    */
    // ...
    // Comment out or delete this line.
    Illuminate\View\ViewServiceProvider::class,

    /*
    * Package Service Providers...
    */
    // Optionally you may add The package's Service Provider manually.
    VicGutt\ComponentBackport\Providers\ComponentBackportServiceProvider::class,
],

文档

有关如何使用Laravel 7+组件的更多详细信息,请参阅官方Laravel文档

为了使这个包正常工作需要做哪些更改?

并不多,令人惊讶。这个包主要是illuminate/view(当前为v8.13.0)的分支,并进行了一些调整

PhpEngine.php

/**
 * Get the evaluated contents of the view at the given path.
 *
 * @param  string  $path
 * @param  array  $data
 * @return string
 */
protected function evaluatePath($path, $data)
{
    $obLevel = ob_get_level();

    ob_start();

    // We'll evaluate the contents of the view inside a try/catch block so we can
    // flush out any stray output that might get out before an error occurs or
    // an exception is thrown. This prevents any partial views from leaking.
    try {
+        extract($data, EXTR_SKIP);
+
+        require $path;
-        $this->files->getRequire($path, $data);
    } catch (Throwable $e) {
        $this->handleViewException($e, $obLevel);
    }

    return ltrim(ob_get_clean());
}

InvokableComponentVariable.php & DeferringDisplayableValue.php

use ArrayIterator;
use Closure;
- use Illuminate\Contracts\Support\DeferringDisplayableValue;
+ use VicGutt\ComponentBackport\Contracts\DeferringDisplayableValue;
use Illuminate\Support\Enumerable;
use IteratorAggregate;

class InvokableComponentVariable implements DeferringDisplayableValue, IteratorAggregate {}
+ <?php
+ 
+ namespace VicGutt\ComponentBackport\Contracts;
+ 
+ interface DeferringDisplayableValue
+ {
+     /**
+      * Resolve the displayable value that the class is deferring.
+      *
+      * @return \Illuminate\Contracts\Support\Htmlable|string
+      */
+     public function resolveDisplayableValue();
+ }