PHP code example of leuverink / asset-injector

1. Go to this page and download the library: Download leuverink/asset-injector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leuverink / asset-injector example snippets


namespace YourPackage;

use Leuverink\AssetInjector\Contracts\AssetInjector;


class InjectAssets implements AssetInjector
{
    // Used to identify your assets in the HTML response
    public function identifier(): string
    {
        return 'MY_PACKAGE';
    }

    // You can opt in to asset injection by implementing your own checks.
    // For example if a package user can control this via config file, or when your end user hits a middleware
    public function enabled(): bool
    {
        return true;
    }

    // Will inject return value in head tag or before html close if no head is present
    public function inject(): string
    {
        $js = file_get_contents(__DIR__ . '/../build/my-package.js');
        $css = file_get_contents(__DIR__ . '/../build/my-package.css');

        return <<< HTML
        <script type="module">{$js}</script>
        <style>{$css}</style>
        HTML;
    }
}

namespace YourPackage;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Leuverink\AssetInjector\AssetManager;
use YourPackage\InjectAssets;

class ServiceProvider extends BaseServiceProvider
{
    public function boot()
    {
        AssetManager::register(new InjectAssets);
    }
}