PHPackages                             mindplay/implant - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mindplay/implant

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mindplay/implant
================

Simple packaging and dependency sorting for embedded JS and CSS assets

2.0.2(8y ago)216.4k[1 issues](https://github.com/mindplay-dk/implant/issues)LGPL-3.0+PHPPHP &gt;=5.5

Since Jul 29Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mindplay-dk/implant)[ Packagist](https://packagist.org/packages/mindplay/implant)[ RSS](/packages/mindplay-implant/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (5)Used By (0)

mindplay/implant
----------------

[](#mindplayimplant)

Simple packaging and dependency sorting for embedded JS and CSS assets.

[![PHP Version](https://camo.githubusercontent.com/5f0c3c8d952d50a65a0e14d33d6a368f80eed503b2f95d15e93d845edd0776bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d352e352532422d626c75652e737667)](https://packagist.org/packages/mindplay/implant)[![Build Status](https://camo.githubusercontent.com/255247fa93408e1d22b571936567d38113dde72d48fa8e5c5e2dabe4c06518b6/68747470733a2f2f7472617669732d63692e6f72672f6d696e64706c61792d646b2f696d706c616e742e737667)](https://travis-ci.org/mindplay-dk/implant)[![Code Coverage](https://camo.githubusercontent.com/05baca5b450a9f1992acd4bb46b77a85b83ae0a6e36f815b79b04788bec6d8d8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f696d706c616e742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/implant/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b16e381d98542dc11eba775871f0e9829ac7da6bf617c4a144bfe6a9a524b1e9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f696d706c616e742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/implant/?branch=master)

Introduction
------------

[](#introduction)

This library provides a simple, open mechanism for packaging assets, e.g. Javascript and CSS files, and managing dependencies between them.

Asset packages are (singleton) classes, which define their dependencies on other asset packages, and populate a view-model with lists of JS and CSS asset URLs.

This library *does not* define any fixed set of asset types or locations - it's not limited to any specific model shape, which means you can use it not only for JS and CSS assets, but for anything you can imagine as an asset, such as inline scripts, web-fonts menus, images, locations in a layout, whatever.

Package granularity is also your choice - for example, you could choose to package related scripts with required CSS files as a combined asset package, or you could choose to package them individually, say, if the dependency order of scripts and CSS files differ.

It also does not output HTML tags or render anything, as this is actually the easy part, as you will see in the examples below.

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

[](#installation)

With composer:

```
composer require mindplay/implant

```

Tutorial
--------

[](#tutorial)

Asset packages are classes implementing the [AssetPackage](src/AssetPackage.php) interface, which enables them to declare their dependencies on other package types, and to define the assets associated with the package, by populating an (arbitrary) model object.

Package classes must have an empty constructor, because the [AssetManager](src/AssetManager.php)constructs packages automatically as needed. (note that this doesn't mean you can't inject dependencies into you package classes - see "peppering" explained below.)

As a case example, let's say you wanted to package JQuery and Bootstrap - first off, you're going to need a model that supports JS and CSS:

```
class AssetModel
{
    public $js = [];
    public $css = [];
}
```

Let's assume you wanted to use JQuery from the CDN, rather than hosting it on your server:

```
class JQueryPackage implements AssetPackage
{
    /**
     * @param AssetModel $model
     */
    public function defineAssets($model)
    {
        $model->js[] = 'https://code.jquery.com/jquery-1.11.3.min.js';
    }

    public function listDependencies()
    {
        return []; // JQuery has no dependencies
    }
}
```

Note that we cannot use a static type-hint in `defineAssets()` because this would violate the interface signature - we use `@param` to type-hint for IDE support instead.

Also note that `listDependencies()` must be implemented, and must return an empty array, to explicitly define that this package has no dependencies.

Next, let's package your locally-hosted bootstrap assets:

```
class BootstrapPackage implements AssetPackage
{
    /**
     * @param AssetModel $model
     */
    public function defineAssets($model)
    {
        $root = '/assets/bootstrap';

        $model->js[] =  "{$root}/js/bootstrap.min.js";
        $model->css[] = "{$root}/css/bootstrap.min.css";
        $model->css[] = "{$root}/css/bootstrap-theme.min.css";
    }

    public function listDependencies()
    {
        return [JQueryPackage::class];
    }
}
```

Pay attention to `listDependencies()`, which defines a dependency on our `JQueryPackage`, which must always be loaded *before* our `BootstrapPackage`.

Now that you assets are packaged, you're ready to roll:

```
$manager = new AssetManager();

$manager->add(BootstrapPackage::class);

$model = new AssetModel();

$manager->populate($model);
```

Notice that we didn't need to manually add the `JQueryPackage` - and more importantly, if you *had* added it manually, the order in which you add things makes no difference; the order in which the packages are applied to your model is based on defined dependencies, not on the order in which your packages are added. Sweet!

Finally, take your model to a view/template somewhere and render it:

```
