* PHP library for handling styles and scripts: Add, minify, unify and print.

Fund package maintenance!
Josantonius

v2.0.0 2022-06-30 19:31 UTC

This package is auto-updated.

Last update: 2022-06-30 19:36:58 UTC


README

Latest Stable Version License Total Downloads CI CodeCov PSR1 PSR4 PSR12

Translations: Español

PHP library for handling HTML links and scripts.

Requirements

This library is compatible with the PHP versions: 8.1.

Installation

The preferred way to install this extension is through Composer.

To install PHP Asset library, simply:

composer require josantonius/asset

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:

composer require josantonius/asset --prefer-source

You can also clone the complete repository with Git:

git clone https://github.com/josantonius/php-asset.git

Available Methods

Available classes and methods in this library:

Add body script

new BodyScript(
    null|bool   $async = null,
    null|string $crossorigin = null,
    null|bool   $defer = null,
    null|string $fetchpriority = null,
    null|string $integrity = null,
    null|bool   $nomodule = null,
    null|string $nonce = null,
    null|string $referrerpolicy = null,
    null|string $src = null,
    null|string $type = null
);

Add head script

new HeadScript(
    null|bool   $async = null,
    null|string $crossorigin = null,
    null|bool   $defer = null,
    null|string $fetchpriority = null,
    null|string $integrity = null,
    null|bool   $nomodule = null,
    null|string $nonce = null,
    null|string $referrerpolicy = null,
    null|string $src = null,
    null|string $type = null
);

Add link

new Link(
    null|string $as = null,
    null|string $crossorigin = null,
    null|bool   $disabled = null,
    null|string $fetchpriority = null,
    null|string $href = null,
    null|string $hreflang = null,
    null|string $imagesizes = null,
    null|string $imagesrcset = null,
    null|string $integrity = null,
    null|string $media = null,
    null|string $prefetch = null,
    null|string $referrerpolicy = null,
    null|string $rel = null,
    null|string $sizes = null,
    null|string $target = null,
    null|string $title = null,
    null|string $type = null,
);

Print the added scripts for the body

$asset->outputBodyScripts();

@Return string Added scripts

Print the added scripts for the head

$asset->outputHeadScripts();

@Return string Added scripts

Print the added links

$asset->outputLinks();

@Return string Added links

Quick Start

Using objects

use Josantonius\Asset\Asset;
use Josantonius\Asset\Elements\BodyScript;
use Josantonius\Asset\Elements\HeadScript;
use Josantonius\Asset\Elements\Link;

new BodyScript(/* ... */);
new HeadScript(/* ... */);
new Link(/* ... */);

$asset = new Asset();

Using the facade

Alternatively you can use a facade to access the methods statically:

use Josantonius\Asset\Facades\Asset;

Usage

Example of use for this library:

- Add body script

Using objects:

new BodyScript(
    src: 'https://example.com/script.js'
);

Using the facade:

Asset::addBodyScript(
    src: 'script.js',
    type: 'text/javascript'
);

- Add head script

Using objects:

new HeadScript(
    src: 'script.js',
    type: 'module'
);

Using the facade:

Asset::addHeadScript(
    crossorigin: 'anonymous',
    defer: true,
    integrity: 'sha256-n9+',
    src: 'https://example.com/script.js',
    type: 'text/javascript'
);

- Add link

Using objects:

new Link(
    crossorigin: 'anonymous',
    href: 'https://example.com/style.css',
    integrity: 'sha256-n9+',
    media: 'all',
    rel: 'stylesheet'
);

Using the facade:

Asset::addLink(
    href: 'https://example.com/style.css',
    rel: 'stylesheet'
);

- Print the added scripts for the body

Using objects:

echo $asset->outputBodyScripts();

Using the facade:

echo Asset::outputBodyScripts();

- Print the added scripts for the head

Using objects:

echo $asset->outputHeadScripts();

Using the facade:

echo Asset::outputHeadScripts();

- Print the added links

Using objects:

echo $asset->outputLinks();

Using the facade:

echo Asset::outputLinks();

- Full example

Using objects:

use Josantonius\Asset\Elements\BodyScript;
use Josantonius\Asset\Elements\HeadScript;
use Josantonius\Asset\Elements\Link;

new BodyScript(src: 'foo.js', async: true);
new BodyScript(src: 'bar.js', type: 'text/javascript');

new HeadScript(src: 'https://example.com/foo.js', type: 'module');
new HeadScript(src: 'https://example.com/bar.js', defer: true);

new Link(href: 'https://example.com/foo.css', rel: 'stylesheet');
new Link(href: 'https://example.com/bar.css', rel: 'stylesheet', media: 'all');
<?php
use Josantonius\Asset\Asset;
$asset = new Asset();
?>
<html>
  <head>
    <?= $asset->outputLinks() ?>
    <?= $asset->outputHeadScripts() ?>
  </head>
  <body>
    <?= $asset->outputBodyScripts() ?>
  </body>
</html>

Using the facade:

use Josantonius\Asset\Facades\Asset;

Asset::addBodyScript(src: 'foo.js', async: true);
Asset::addBodyScript(src: 'bar.js', type: 'text/javascript');

Asset::addHeadScript(src: 'https://example.com/foo.js', type: 'module');
Asset::addHeadScript(src: 'https://example.com/bar.js', defer: true);

Asset::addLink(href: 'https://example.com/foo.css', rel: 'stylesheet');
Asset::addLink(href: 'https://example.com/bar.css', rel: 'stylesheet', media: 'all');
<?php
use Josantonius\Asset\Facades\Asset;
?>
<html>
  <head>
    <?= Asset::outputLinks() ?>
    <?= Asset::outputHeadScripts() ?>
  </head>
  <body>
    <?= Asset::outputBodyScripts() ?>
  </body>
</html>

Result:

<html>
  <head>
    <link href="https://example.com/foo.css" rel="stylesheet">
    <link href="https://example.com/bar.css" rel="stylesheet" media="all">
    <script src="https://example.com/foo.js" type="module"></script>
    <script defer src="https://example.com/bar.js"></script>
  </head>
  <body>
    <script async src="foo.js"></script>
    <script src="bar.js" type="text/javascript"></script>
  </body>
</html>

Tests

To run tests you just need composer and to execute the following:

git clone https://github.com/josantonius/php-asset.git
cd php-asset
composer install

Run unit tests with PHPUnit:

composer phpunit

Run code standard tests with PHPCS:

composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:

composer phpmd

Run all previous tests:

composer tests

TODO

  • Add new feature
  • Improve tests
  • Improve documentation
  • Improve English translation in the README file
  • Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)
  • Add other HTML elements
  • Add feature to add code between the <script> tags

Changelog

Detailed changes for each release are documented in the release notes.

Contribution

Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.

Thanks to all contributors! ❤️

Sponsor

If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊

License

This repository is licensed under the MIT License.

Copyright © 2016-present, Josantonius