PHPackages                             josantonius/asset - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. josantonius/asset

ActiveLibrary

josantonius/asset
=================

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

v2.0.3(3y ago)919321MITPHPPHP ^8.1

Since Dec 14Pushed 3y ago2 watchersCompare

[ Source](https://github.com/josantonius/php-asset)[ Packagist](https://packagist.org/packages/josantonius/asset)[ GitHub Sponsors](https://github.com/Josantonius)[ RSS](/packages/josantonius-asset/feed)WikiDiscussions main Synced 2mo ago

READMEChangelog (10)Dependencies (3)Versions (13)Used By (1)

PHP Asset library
=================

[](#php-asset-library)

[![Latest Stable Version](https://camo.githubusercontent.com/7f9996e0d8d4a76d522803efa0240c1747df0547b0bbd18f3f547d6c0ed03a13/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f61737365742f762f737461626c65)](https://packagist.org/packages/josantonius/asset)[![License](https://camo.githubusercontent.com/3e06cecc7d1ffd5478e54a9a560f087f38e5555b89e4f0d0cbf625dd7077663f/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f61737365742f6c6963656e7365)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/929cc50dd31ebaccb7f10e1298bd4a47938bf4b4ef8071a0247f0e88fa563c6c/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f61737365742f646f776e6c6f616473)](https://packagist.org/packages/josantonius/asset)[![CI](https://github.com/josantonius/php-asset/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-asset/actions/workflows/ci.yml)[![CodeCov](https://camo.githubusercontent.com/0368549d17618aa3e336bc3950f260d301a7dcb228ce06f4954b521cf7e8f01a/68747470733a2f2f636f6465636f762e696f2f67682f6a6f73616e746f6e6975732f7068702d61737365742f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/josantonius/php-asset)[![PSR1](https://camo.githubusercontent.com/b502a899c9aec217e98971160f816f87346be272cf1a25cfa4793f2ee724bfc8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d312d6635373034362e737667)](https://www.php-fig.org/psr/psr-1/)[![PSR4](https://camo.githubusercontent.com/d1c090de87e968254a6658528f3bfe9c9dad422d6047fd1323dc211560182c01/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d342d3962353962362e737667)](https://www.php-fig.org/psr/psr-4/)[![PSR12](https://camo.githubusercontent.com/19c529c6dc0656dcc2a16c1a84af450b7bd0dc7b0571b8f17e4fc9f2414f8821/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d31322d3161626339632e737667)](https://www.php-fig.org/psr/psr-12/)

**Translations**: [Español](.github/lang/es-ES/README.md)

PHP library for handling HTML links and scripts.

---

- [Requirements](#requirements)
- [Installation](#installation)
- [Available Classes](#available-classes)
    - [Asset Class](#asset-class)
    - [Asset Facade](#asset-facade)
    - [BodyScript Class](#bodyscript-class)
    - [HeadScript Class](#headscript-class)
    - [Link Class](#link-class)
- [Usage](#usage)
- [Tests](#tests)
- [TODO](#todo)
- [Changelog](#changelog)
- [Contribution](#contribution)
- [Sponsor](#sponsor)
- [License](#license)

---

Requirements
------------

[](#requirements)

- Operating System: Linux | Windows.
- PHP versions: 8.1 | 8.2.

Installation
------------

[](#installation)

The preferred way to install this extension is through [Composer](http://getcomposer.org/download/).

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 Classes
-----------------

[](#available-classes)

### Asset Class

[](#asset-class)

`Josantonius\Asset\Asset`

Print the added scripts for the body:

```
public function outputBodyScripts(): string;
```

Print the added scripts for the head:

```
public function outputHeadScripts(): string;
```

Print the added links:

```
public function outputLinks(): string;
```

### Asset Facade

[](#asset-facade)

`Josantonius\Asset\Facades\Asset`

Add body script:

```
/**
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
 */
public static function addBodyScript(
    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
): BodyScript;
```

Add head script:

```
/**
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
 */
public static function addHeadScript(
    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
): HeadScript;
```

Add link:

```
/**
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
 */
public static function addLink(
    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,
): Link;
```

Print the added scripts for the body:

```
public static function outputBodyScripts(): string;
```

Print the added scripts for the head:

```
public static function outputHeadScripts(): string;
```

Print the added links:

```
public static function outputLinks(): string;
```

### BodyScript Class

[](#bodyscript-class)

`Josantonius\Asset\Elements\BodyScript`

Add body script:

```
/**
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
 */
public function __construct(
    private null|bool   $async          = null,
    private null|string $crossorigin    = null,
    private null|bool   $defer          = null,
    private null|string $fetchpriority  = null,
    private null|string $integrity      = null,
    private null|bool   $nomodule       = null,
    private null|string $nonce          = null,
    private null|string $referrerpolicy = null,
    private null|string $src            = null,
    private null|string $type           = null
);
```

### HeadScript Class

[](#headscript-class)

`Josantonius\Asset\Elements\HeadScript`

Add head script:

```
/**
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
 */
public function __construct(
    private null|bool   $async          = null,
    private null|string $crossorigin    = null,
    private null|bool   $defer          = null,
    private null|string $fetchpriority  = null,
    private null|string $integrity      = null,
    private null|bool   $nomodule       = null,
    private null|string $nonce          = null,
    private null|string $referrerpolicy = null,
    private null|string $src            = null,
    private null|string $type           = null
);
```

### Link Class

[](#link-class)

`Josantonius\Asset\Elements\Link`

Add link:

```
/**
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
 */
public function __construct(
    private null|string $as             = null,
    private null|string $crossorigin    = null,
    private null|bool   $disabled       = null,
    private null|string $fetchpriority  = null,
    private null|string $href           = null,
    private null|string $hreflang       = null,
    private null|string $imagesizes     = null,
    private null|string $imagesrcset    = null,
    private null|string $integrity      = null,
    private null|string $media          = null,
    private null|string $prefetch       = null,
    private null|string $referrerpolicy = null,
    private null|string $rel            = null,
    private null|string $sizes          = null,
    private null|string $target         = null,
    private null|string $title          = null,
    private null|string $type           = null,
);
```

Usage
-----

[](#usage)

Example of use for this library:

### Add body script

[](#add-body-script)

```
use Josantonius\Asset\Elements\BodyScript;

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

```
use Josantonius\Asset\Facades\Asset;

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

### Add head script

[](#add-head-script)

```
use Josantonius\Asset\Elements\HeadScript;

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

```
use Josantonius\Asset\Facades\Asset;

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

### Add link

[](#add-link)

```
use Josantonius\Asset\Elements\Link;

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

```
use Josantonius\Asset\Facades\Asset;

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

### Print the added scripts for the body

[](#print-the-added-scripts-for-the-body)

```
use Josantonius\Asset\Asset;

$asset = new Asset();

echo $asset->outputBodyScripts();
```

```
use Josantonius\Asset\Facades\Asset;

echo Asset::outputBodyScripts();
```

### Print the added scripts for the head

[](#print-the-added-scripts-for-the-head)

```
use Josantonius\Asset\Asset;

$asset = new Asset();

echo $asset->outputHeadScripts();
```

```
use Josantonius\Asset\Facades\Asset;

echo Asset::outputHeadScripts();
```

### Print the added links

[](#print-the-added-links)

```
use Josantonius\Asset\Asset;

$asset = new Asset();

echo $asset->outputLinks();
```

```
use Josantonius\Asset\Facades\Asset;

echo Asset::outputLinks();
```

### Full example

[](#full-example)

**`index.php`**

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

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');
```

**`page.html`**

```

    outputLinks() ?>
    outputHeadScripts() ?>

    outputBodyScripts() ?>

```

**Result:**

```

```

### Full example using the facade

[](#full-example-using-the-facade)

**`index.php`**

```
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');
```

**`page.html`**

```

```

**Result:**

```

```

Tests
-----

[](#tests)

To run [tests](tests) you just need [composer](http://getcomposer.org/download/)and to execute the following:

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

```
cd php-asset
```

```
composer install
```

Run unit tests with [PHPUnit](https://phpunit.de/):

```
composer phpunit
```

Run code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):

```
composer phpcs
```

Run [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:

```
composer phpmd
```

Run all previous tests:

```
composer tests
```

TODO
----

[](#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](phpmd.xml) and [phpcs.xml](phpcs.xml))
- Add other HTML elements
- Add feature to add code between the `` tags

Changelog
---------

[](#changelog)

Detailed changes for each release are documented in the [release notes](https://github.com/josantonius/php-asset/releases).

Contribution
------------

[](#contribution)

Please make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull request, start a discussion or report a issue.

Thanks to all [contributors](https://github.com/josantonius/php-asset/graphs/contributors)! ❤️

Sponsor
-------

[](#sponsor)

If this project helps you to reduce your development time, [you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work 😊

License
-------

[](#license)

This repository is licensed under the [MIT License](LICENSE).

Copyright © 2016-present, [Josantonius](https://github.com/josantonius#contact)

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~206 days

Recently: every ~419 days

Total

11

Last Release

1369d ago

Major Versions

1.1.7 → v2.0.02022-06-30

PHP version history (3 changes)1.0.0PHP &gt;=7.0

1.1.2PHP ^5.6 || ^7.0

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b221283501ec8a9cbaefaf27821a91ae8ddd33bddf1fccc6c6815b7ad216ff1?d=identicon)[Josantonius](/maintainers/Josantonius)

---

Top Contributors

[![josantonius](https://avatars.githubusercontent.com/u/18104336?v=4)](https://github.com/josantonius "josantonius (88 commits)")

---

Tags

css-filesjs-filesphpphp-assetphp-libraryscriptsstylesheetstylesheetsphpcssJSassetprintstylesscripts

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/josantonius-asset/health.svg)

```
[![Health](https://phpackages.com/badges/josantonius-asset/health.svg)](https://phpackages.com/packages/josantonius-asset)
```

###  Alternatives

[fisharebest/laravel-assets

Asset management for Laravel

208.1k](/packages/fisharebest-laravel-assets)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
