{"id":16329298,"url":"https://github.com/josantonius/php-asset","last_synced_at":"2025-03-22T22:31:28.024Z","repository":{"id":41344688,"uuid":"76417487","full_name":"josantonius/php-asset","owner":"josantonius","description":"PHP library for handling HTML links and scripts","archived":false,"fork":false,"pushed_at":"2022-09-29T15:38:01.000Z","size":1221,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T15:21:44.475Z","etag":null,"topics":["css-files","js-files","php","php-asset","php-library","scripts","stylesheet","stylesheets"],"latest_commit_sha":null,"homepage":"https://josantonius.dev","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/josantonius.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["Josantonius"]}},"created_at":"2016-12-14T02:33:28.000Z","updated_at":"2024-02-07T09:01:45.000Z","dependencies_parsed_at":"2022-08-21T12:10:13.073Z","dependency_job_id":null,"html_url":"https://github.com/josantonius/php-asset","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-asset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-asset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-asset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-asset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josantonius","download_url":"https://codeload.github.com/josantonius/php-asset/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245028564,"owners_count":20549541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css-files","js-files","php","php-asset","php-library","scripts","stylesheet","stylesheets"],"created_at":"2024-10-10T23:15:02.531Z","updated_at":"2025-03-22T22:31:27.772Z","avatar_url":"https://github.com/josantonius.png","language":"PHP","funding_links":["https://github.com/sponsors/Josantonius"],"categories":[],"sub_categories":[],"readme":"# PHP Asset library\n\n[![Latest Stable Version](https://poser.pugx.org/josantonius/asset/v/stable)](https://packagist.org/packages/josantonius/asset)\n[![License](https://poser.pugx.org/josantonius/asset/license)](LICENSE)\n[![Total Downloads](https://poser.pugx.org/josantonius/asset/downloads)](https://packagist.org/packages/josantonius/asset)\n[![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)\n[![CodeCov](https://codecov.io/gh/josantonius/php-asset/branch/main/graph/badge.svg)](https://codecov.io/gh/josantonius/php-asset)\n[![PSR1](https://img.shields.io/badge/PSR-1-f57046.svg)](https://www.php-fig.org/psr/psr-1/)\n[![PSR4](https://img.shields.io/badge/PSR-4-9b59b6.svg)](https://www.php-fig.org/psr/psr-4/)\n[![PSR12](https://img.shields.io/badge/PSR-12-1abc9c.svg)](https://www.php-fig.org/psr/psr-12/)\n\n**Translations**: [Español](.github/lang/es-ES/README.md)\n\nPHP library for handling HTML links and scripts.\n\n---\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Available Classes](#available-classes)\n  - [Asset Class](#asset-class)\n  - [Asset Facade](#asset-facade)\n  - [BodyScript Class](#bodyscript-class)\n  - [HeadScript Class](#headscript-class)\n  - [Link Class](#link-class)\n- [Usage](#usage)\n- [Tests](#tests)\n- [TODO](#todo)\n- [Changelog](#changelog)\n- [Contribution](#contribution)\n- [Sponsor](#sponsor)\n- [License](#license)\n\n---\n\n## Requirements\n\n- Operating System: Linux | Windows.\n\n- PHP versions: 8.1 | 8.2.\n\n## Installation\n\nThe preferred way to install this extension is through [Composer](http://getcomposer.org/download/).\n\nTo install **PHP Asset library**, simply:\n\n```console\ncomposer require josantonius/asset\n```\n\nThe previous command will only install the necessary files,\nif you prefer to **download the entire source code** you can use:\n\n```console\ncomposer require josantonius/asset --prefer-source\n```\n\nYou can also **clone the complete repository** with Git:\n\n```console\ngit clone https://github.com/josantonius/php-asset.git\n```\n\n## Available Classes\n\n### Asset Class\n\n`Josantonius\\Asset\\Asset`\n\nPrint the added scripts for the body:\n\n```php\npublic function outputBodyScripts(): string;\n```\n\nPrint the added scripts for the head:\n\n```php\npublic function outputHeadScripts(): string;\n```\n\nPrint the added links:\n\n```php\npublic function outputLinks(): string;\n```\n\n### Asset Facade\n\n`Josantonius\\Asset\\Facades\\Asset`\n\nAdd body script:\n\n```php\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script\n */\npublic static function addBodyScript(\n    null|bool   $async          = null,\n    null|string $crossorigin    = null,\n    null|bool   $defer          = null,\n    null|string $fetchpriority  = null,\n    null|string $integrity      = null,\n    null|bool   $nomodule       = null,\n    null|string $nonce          = null,\n    null|string $referrerpolicy = null,\n    null|string $src            = null,\n    null|string $type           = null\n): BodyScript;\n```\n\nAdd head script:\n\n```php\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script\n */\npublic static function addHeadScript(\n    null|bool   $async          = null,\n    null|string $crossorigin    = null,\n    null|bool   $defer          = null,\n    null|string $fetchpriority  = null,\n    null|string $integrity      = null,\n    null|bool   $nomodule       = null,\n    null|string $nonce          = null,\n    null|string $referrerpolicy = null,\n    null|string $src            = null,\n    null|string $type           = null\n): HeadScript;\n```\n\nAdd link:\n\n```php\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link\n */\npublic static function addLink(\n    null|string $as             = null,\n    null|string $crossorigin    = null,\n    null|bool   $disabled       = null,\n    null|string $fetchpriority  = null,\n    null|string $href           = null,\n    null|string $hreflang       = null,\n    null|string $imagesizes     = null,\n    null|string $imagesrcset    = null,\n    null|string $integrity      = null,\n    null|string $media          = null,\n    null|string $prefetch       = null,\n    null|string $referrerpolicy = null,\n    null|string $rel            = null,\n    null|string $sizes          = null,\n    null|string $target         = null,\n    null|string $title          = null,\n    null|string $type           = null,\n): Link;\n```\n\nPrint the added scripts for the body:\n\n```php\npublic static function outputBodyScripts(): string;\n```\n\nPrint the added scripts for the head:\n\n```php\npublic static function outputHeadScripts(): string;\n```\n\nPrint the added links:\n\n```php\npublic static function outputLinks(): string;\n```\n\n### BodyScript Class\n\n`Josantonius\\Asset\\Elements\\BodyScript`\n\nAdd body script:\n\n```php\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script\n */\npublic function __construct(\n    private null|bool   $async          = null,\n    private null|string $crossorigin    = null,\n    private null|bool   $defer          = null,\n    private null|string $fetchpriority  = null,\n    private null|string $integrity      = null,\n    private null|bool   $nomodule       = null,\n    private null|string $nonce          = null,\n    private null|string $referrerpolicy = null,\n    private null|string $src            = null,\n    private null|string $type           = null\n);\n```\n\n### HeadScript Class\n\n`Josantonius\\Asset\\Elements\\HeadScript`\n\nAdd head script:\n\n```php\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script\n */\npublic function __construct(\n    private null|bool   $async          = null,\n    private null|string $crossorigin    = null,\n    private null|bool   $defer          = null,\n    private null|string $fetchpriority  = null,\n    private null|string $integrity      = null,\n    private null|bool   $nomodule       = null,\n    private null|string $nonce          = null,\n    private null|string $referrerpolicy = null,\n    private null|string $src            = null,\n    private null|string $type           = null\n);\n```\n\n### Link Class\n\n`Josantonius\\Asset\\Elements\\Link`\n\nAdd link:\n\n```php\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link\n */\npublic function __construct(\n    private null|string $as             = null,\n    private null|string $crossorigin    = null,\n    private null|bool   $disabled       = null,\n    private null|string $fetchpriority  = null,\n    private null|string $href           = null,\n    private null|string $hreflang       = null,\n    private null|string $imagesizes     = null,\n    private null|string $imagesrcset    = null,\n    private null|string $integrity      = null,\n    private null|string $media          = null,\n    private null|string $prefetch       = null,\n    private null|string $referrerpolicy = null,\n    private null|string $rel            = null,\n    private null|string $sizes          = null,\n    private null|string $target         = null,\n    private null|string $title          = null,\n    private null|string $type           = null,\n);\n```\n\n## Usage\n\nExample of use for this library:\n\n### Add body script\n\n```php\nuse Josantonius\\Asset\\Elements\\BodyScript;\n\nnew BodyScript(\n    src: 'https://example.com/script.js'\n);\n```\n\n```php\nuse Josantonius\\Asset\\Facades\\Asset;\n\nAsset::addBodyScript(\n    src: 'script.js',\n    type: 'text/javascript'\n);\n```\n\n### Add head script\n\n```php\nuse Josantonius\\Asset\\Elements\\HeadScript;\n\nnew HeadScript(\n    src: 'script.js',\n    type: 'module'\n);\n```\n\n```php\nuse Josantonius\\Asset\\Facades\\Asset;\n\nAsset::addHeadScript(\n    crossorigin: 'anonymous',\n    defer: true,\n    integrity: 'sha256-n9+',\n    src: 'https://example.com/script.js',\n    type: 'text/javascript'\n);\n```\n\n### Add link\n\n```php\nuse Josantonius\\Asset\\Elements\\Link;\n\nnew Link(\n    crossorigin: 'anonymous',\n    href: 'https://example.com/style.css',\n    integrity: 'sha256-n9+',\n    media: 'all',\n    rel: 'stylesheet'\n);\n```\n\n```php\nuse Josantonius\\Asset\\Facades\\Asset;\n\nAsset::addLink(\n    href: 'https://example.com/style.css',\n    rel: 'stylesheet'\n);\n```\n\n### Print the added scripts for the body\n\n```php\nuse Josantonius\\Asset\\Asset;\n\n$asset = new Asset();\n\necho $asset-\u003eoutputBodyScripts();\n```\n\n```php\nuse Josantonius\\Asset\\Facades\\Asset;\n\necho Asset::outputBodyScripts();\n```\n\n### Print the added scripts for the head\n\n```php\nuse Josantonius\\Asset\\Asset;\n\n$asset = new Asset();\n\necho $asset-\u003eoutputHeadScripts();\n```\n\n```php\nuse Josantonius\\Asset\\Facades\\Asset;\n\necho Asset::outputHeadScripts();\n```\n\n### Print the added links\n\n```php\nuse Josantonius\\Asset\\Asset;\n\n$asset = new Asset();\n\necho $asset-\u003eoutputLinks();\n```\n\n```php\nuse Josantonius\\Asset\\Facades\\Asset;\n\necho Asset::outputLinks();\n```\n\n### Full example\n\n**`index.php`**\n\n```php\nuse Josantonius\\Asset\\Elements\\Link;\nuse Josantonius\\Asset\\Elements\\BodyScript;\nuse Josantonius\\Asset\\Elements\\HeadScript;\n\nnew BodyScript(src: 'foo.js', async: true);\nnew BodyScript(src: 'bar.js', type: 'text/javascript');\n\nnew HeadScript(src: 'https://example.com/foo.js', type: 'module');\nnew HeadScript(src: 'https://example.com/bar.js', defer: true);\n\nnew Link(href: 'https://example.com/foo.css', rel: 'stylesheet');\nnew Link(href: 'https://example.com/bar.css', rel: 'stylesheet', media: 'all');\n```\n\n**`page.html`**\n\n```html\n\u003c?php\nuse Josantonius\\Asset\\Asset;\n$asset = new Asset();\n?\u003e\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003c?= $asset-\u003eoutputLinks() ?\u003e\n    \u003c?= $asset-\u003eoutputHeadScripts() ?\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003c?= $asset-\u003eoutputBodyScripts() ?\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n**Result:**\n\n```html\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003clink href=\"https://example.com/foo.css\" rel=\"stylesheet\"\u003e\n    \u003clink href=\"https://example.com/bar.css\" rel=\"stylesheet\" media=\"all\"\u003e\n    \u003cscript src=\"https://example.com/foo.js\" type=\"module\"\u003e\u003c/script\u003e\n    \u003cscript defer src=\"https://example.com/bar.js\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cscript async src=\"foo.js\"\u003e\u003c/script\u003e\n    \u003cscript src=\"bar.js\" type=\"text/javascript\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Full example using the facade\n\n**`index.php`**\n\n```php\nuse Josantonius\\Asset\\Facades\\Asset;\n\nAsset::addBodyScript(src: 'foo.js', async: true);\nAsset::addBodyScript(src: 'bar.js', type: 'text/javascript');\n\nAsset::addHeadScript(src: 'https://example.com/foo.js', type: 'module');\nAsset::addHeadScript(src: 'https://example.com/bar.js', defer: true);\n\nAsset::addLink(href: 'https://example.com/foo.css', rel: 'stylesheet');\nAsset::addLink(href: 'https://example.com/bar.css', rel: 'stylesheet', media: 'all');\n```\n\n**`page.html`**\n\n```html\n\u003c?php\nuse Josantonius\\Asset\\Facades\\Asset;\n?\u003e\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003c?= Asset::outputLinks() ?\u003e\n    \u003c?= Asset::outputHeadScripts() ?\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003c?= Asset::outputBodyScripts() ?\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n**Result:**\n\n```html\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003clink href=\"https://example.com/foo.css\" rel=\"stylesheet\"\u003e\n    \u003clink href=\"https://example.com/bar.css\" rel=\"stylesheet\" media=\"all\"\u003e\n    \u003cscript src=\"https://example.com/foo.js\" type=\"module\"\u003e\u003c/script\u003e\n    \u003cscript defer src=\"https://example.com/bar.js\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cscript async src=\"foo.js\"\u003e\u003c/script\u003e\n    \u003cscript src=\"bar.js\" type=\"text/javascript\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Tests\n\nTo run [tests](tests) you just need [composer](http://getcomposer.org/download/)\nand to execute the following:\n\n```console\ngit clone https://github.com/josantonius/php-asset.git\n```\n\n```console\ncd php-asset\n```\n\n```console\ncomposer install\n```\n\nRun unit tests with [PHPUnit](https://phpunit.de/):\n\n```console\ncomposer phpunit\n```\n\nRun code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):\n\n```console\ncomposer phpcs\n```\n\nRun [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:\n\n```console\ncomposer phpmd\n```\n\nRun all previous tests:\n\n```console\ncomposer tests\n```\n\n## TODO\n\n- [ ] Add new feature\n- [ ] Improve tests\n- [ ] Improve documentation\n- [ ] Improve English translation in the README file\n- [ ] Refactor code for disabled code style rules (see [phpmd.xml](phpmd.xml) and [phpcs.xml](phpcs.xml))\n- [ ] Add other HTML elements\n- [ ] Add feature to add code between the `\u003cscript\u003e` tags\n\n## Changelog\n\nDetailed changes for each release are documented in the\n[release notes](https://github.com/josantonius/php-asset/releases).\n\n## Contribution\n\nPlease make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull\nrequest, start a discussion or report a issue.\n\nThanks to all [contributors](https://github.com/josantonius/php-asset/graphs/contributors)! :heart:\n\n## Sponsor\n\nIf this project helps you to reduce your development time,\n[you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work :blush:\n\n## License\n\nThis repository is licensed under the [MIT License](LICENSE).\n\nCopyright © 2016-present, [Josantonius](https://github.com/josantonius#contact)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-asset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosantonius%2Fphp-asset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-asset/lists"}