mathiasreker/php-script-cache

php-script-cache is a PHP library for caching external scripts locally.

1.1.0 2022-11-03 21:16 UTC

This package is auto-updated.

Last update: 2024-11-11 08:36:56 UTC


README

Packagist Version Packagist Downloads CI status Contributors Forks Stargazers Issues MIT License

php-script-cache is a PHP library for caching external scripts locally.

✅ Cache ✅ Bundle ✅ Minify

Versions & Dependencies

Requirements

  • PHP >= 7.4
  • php-extension ext-json

Installation

Run:

composer require mathiasreker/php-script-cache

Examples

Set up a cronjob to build the assets.

<?php

use MathiasReker\PhpScriptCache\ScriptCache;

require __DIR__ . '/vendor/autoload.php';

(new ScriptCache())
    ->setPath(__DIR__ . '/assets/js')
    ->doMinify()
    ->add([
        'src' => [
            'https://example1.com/script.js',
            'https://example2.com/script.js'
        ], // multiple scripts will get bundled
    ])
    ->add([
        'src' => ['https://example3.com/script.js'],
    ])
    ->build();

Place this code where you want to output the script tags. You can add attributes to the scripts.

<?php

use MathiasReker\PhpScriptCache\ScriptCache;

require __DIR__ . '/vendor/autoload.php';

$result = (new ScriptCache())
    ->setPath(__DIR__ . '/assets/js')
    ->add([
        'src' => [
            'https://example1.com/script.js',
            'https://example2.com/script.js'
        ],
    ])
    ->add([
        'src' => ['https://example3.com/script.js'],
        'id' => 'test',
        'defer' => '',
    ])
    ->fetch();

 // Result: <script src="/assets/js/c5a80e42.js?v=ec25a610"></script><script src="/assets/js/c5a80e42.js?v=539e4fd1" id="test" defer></script>

Documentation

Instantiate the object.

$result = new ScriptCache();
$result->setPath(__DIR__ . '/assets/js');

setPath sets the path of the output folder for built files.

$result->doMinify();

doMinify minifies the content.

$result->add(['src' => ['https://example.com/script.js']]);

add sets the attributes of the script. The src can be an array of several scripts. All the scripts in the collection will get bundled. You can use any attributes. You do not need to set attributes when you build the cache.

$result->build();

build builds the assets.

$result->fetch();

fetch returns a string of the script tags.

Roadmap

See the open issues for a complete list of proposed features (and known issues).

Contributing

If you have a suggestion to improve this, please fork the repo and create a pull request. You can also open an issue with the tag "enhancement". Finally, don't forget to give the project a star! Thanks again!

Docker

If you are using docker, you can use the following command to get started:

docker-compose up -d

Next, access the container:

docker exec -it php-script-cache bash

Tools

PHP Coding Standards Fixer:

composer cs-fix

PHP Coding Standards Checker:

composer cs-check

PHP Stan:

composer phpstan

Unit tests:

composer test

License

It is distributed under the MIT License. See LICENSE for more information.