php-script-cache
is a PHP library for caching external scripts locally.
✅ Cache ✅ Bundle ✅ Minify
Version | PHP | Documentation |
---|---|---|
^1.0 | ^7.4 | current |
PHP
>= 7.4- php-extension
ext-json
Run:
composer require mathiasreker/php-script-cache
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>
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.
See the open issues for a complete list of proposed features (and known issues).
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!
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
PHP Coding Standards Fixer:
composer cs-fix
PHP Coding Standards Checker:
composer cs-check
PHP Stan:
composer phpstan
Unit tests:
composer test
It is distributed under the MIT License. See LICENSE
for more information.