PHPackages                             nami-doc/sprockets-php - 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. nami-doc/sprockets-php

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

nami-doc/sprockets-php
======================

Sprockets-PHP is Sprockets (Rails Asset Pipeline) for PHP

2.1.5(9y ago)496.1k4[8 issues](https://github.com/vendethiel/Sprockets-PHP/issues)[1 PRs](https://github.com/vendethiel/Sprockets-PHP/pulls)PHP

Since Jun 12Pushed 8y ago7 watchersCompare

[ Source](https://github.com/vendethiel/Sprockets-PHP)[ Packagist](https://packagist.org/packages/nami-doc/sprockets-php)[ RSS](/packages/nami-doc-sprockets-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (25)Used By (0)

Sprockets-PHP
=============

[](#sprockets-php)

What is Sprockets-PHP
=====================

[](#what-is-sprockets-php)

Sprockets-PHP is a port of Sprockets, the well-known Asset Manager for Rails. Sprockets-PHP allows you to manage your assets by taking care of preprocessors, dependencies, minification and caching. The Asset Pipeline will read your main file (usually "application.js" or "application.css"), read directives, and apply filters for all the files. This is an example usage

`application.js`

```
/**
 * (see the "directive syntax" section below)
 *= require jquery
 *= require lib/form
 *= require lib/inputs/{text,password}
 *= require_directory lib/loaders
 */
```

`lib/form/index.js.coffee`

```
class @Form
  @Inputs = {}
  constructor: ->
//= require /base-input
```

`/lib/form/base-input.js.coffee`

```
class @Form.BaseInput
```

`lib/inputs/text.js.coffee`

```
class @Form.Inputs.Text extends @Form.BaseInput
  @type: 'Text'
```

`lib/inputs/password.js.ls`

```
class @Form.Inputs.Password extends @Form.BaseInput
  @type = 'Password'
  -> console.log
```

It's primarily meant to deal with JS and CSS but can as well be used for HTML (HAML, Twig, Slim...). You can add your own filters in a very flexible way (see below).

How can I use it ?!
===================

[](#how-can-i-use-it-)

You have to create an instance of `Sprockets\Pipeline`. The argument is the array of "base paths" from where the Pipeline has to search files.

If you want to call directly the Pipeline, you can then do `$pipeline($asset_type)`. For example `$pipeline('css');`. The framework will load `application.css` in one of the base paths. This file must contain "directives", like Sprockets's one.

```
// require your autoloader
...

// read paths.json - see below
// you can of course pass a normal array !
$paths = str_replace('%template%', 'MyTemplate', file_get_contents('paths.json'));
$paths = json_decode($paths, true);

// create a pipeline
$pipeline = new Sprockets\Pipeline($paths);

// finds `application.css` in the paths
echo $pipeline('css');

// uses `layout.css`
echo $pipeline('css', 'layout');

// same as the first example, but will cache it into a file
$cache = new Sprockets\Cache($pipeline, 'css', $vars = array(), $options = array());
// $options you can pass :
// `minify` whether you want to minify the output or not
// - `.js` : Minified through [Esmangle](https://github.com/Constellation/esmangle)
// - `.css` : Minified through [Clean-CSS](https://github.com/GoalSmashers/clean-css)
$content = $cache->getContent();
$filename = (string) $cache;
//or
$filename = $cache->getFilename();
```

Asset Paths
-----------

[](#asset-paths)

The asset paths are divided by "modules" to be as flexible as it can :

```
{
  "template": {
    "directories": [
      "app/themes/%template%/assets/",
      "app/themes/_shared/assets/",
      "lib/assets/",
      "vendor/assets/"
    ],
    "prefixes": {
      "js": "javascripts",
      "css": "stylesheets",
      "img": "images",
      "font": "fonts"
    }
  },
  "external": {
    "directories": [
      "vendor/bower/",
      "vendor/components/"
    ]
  }
}
```

You have 2 keys in each modules : the `directories`, which list directories where the Pipeline must search files, and `prefixes`, which will append the path for the extension to the directory (ie a `js` file will get `javascripts/` appended to its paths).

For example, if we run `$pipeline('js')`, the pipeline will try to find the following files :

- `app/themes/%template%/assets/javascripts/application.js` (`%template%` being replaced in the example above)
- `app/themes/_shared/assets/javascripts/application.js`
- `lib/assets/javascripts/application.js`
- `vendor/assets/javascripts/application.js`
- `vendor/bower/application.js`
- `vendor/components/application.js`

This example file, allowing to use a Rails-like `javascripts/` directory for js file gracefully, also supports `//= require jquery/jquery` to find `vendor/bower/jquery/jquery.js`

Only the "meaningful" extension matters (using a whitelist).

```
/**
 * for example
 *= require datatables/js/jquery.dataTables
 * will find correctly the file named
 * "vendor/bower/datatables/js/jquery.dataTables.js.coffee"
 * and the "coffee" filter will be correctly applied.
 */
```

Options
-------

[](#options)

Here are the options and their default values :

```
      'NODE_PATH' => 'node',
      'NPM_PATH' => __DIR__ . '/../../node_modules/',
      'CACHE_DIRECTORY' => 'cache/',
```

Just pass them along in paths.

Caching
-------

[](#caching)

Something to note : even if you're not using `Sprockets\Cache`, the asset pipeline will keep a file list cache in your cache directory, to speed up path lookups.

Directive Syntax
----------------

[](#directive-syntax)

There are three supported syntaxs at this moment.

```
//= only for js
#= only for js
/**
 *= for any
 */
```

Supported Directives
--------------------

[](#supported-directives)

The directives disponibles are : `require`, `require_directory`, `require_tree` and `depends_on`

### require

[](#require)

Requires a file directly, from the relative path OR one of the base path. You can also give a directory name, if this directory has a file named "index.$type" (here, "index.css") in. This directive supports bracket expansion.

### require\_directory

[](#require_directory)

Requires each file of the directory. Not recursive.

### require\_tree

[](#require_tree)

Recursively requires each file of the directory tree.

### depends\_on

[](#depends_on)

Adds the file to the dependencies, even if the file isn't included. For example, in application.css

```
//= depends_on image.png
//= depends_on layout
```

If this file change, the whole stylesheet (and the dependencies) will be recompiled (this is meant for inlining of some preprocessors).

Filters
-------

[](#filters)

The available filters are :

Languages :

- .php : [PHP](http://php.net)

JavaScript :

- .ls : [LiveScript](http://livescript.org)
- .coffee : [CoffeeScript](http://coffeescript.org) (through [coffeescript-php](github.com/alxlit/coffeescript-php))

Stylesheet :

- .styl : [Stylus](http://learnboost.github.io/stylus/)
- .sass .scss : [Sass](http://sass-lang.com/) (through [PHPSass](http://phpsass.com/))
- .less : [Less](http://lesscss.org) (through [lessphp](http://leafo.net/lessphp/))

Html :

- .haml : [Haml](http://haml.info) (through [MtHaml](https://github.com/arnaud-lb/MtHaml/), upon which you can build a Twig version, for example)

Adding filter is very easy (to create a `.twig` filter or a `.md`, for example). Just add it to the pipeline :

```
$pipeline->registerFilter('md', 'My\Markdown\Parser');
```

You must implement an interface like `\Sprockets\Filter\Interface` :

```
interface Interface
{
	/**
	 * @return string processed $content
	 */
	public function __invoke($content, $file, $dir, $vars);
}
```

You can also inherit `Sprockets\Filter\Base` which gives you access to :

- `$this->pipeline` current pipeline instance
- `$this->processNode()` passing an argument array, auto-quoted, like this : `array('modulename/bin/mod', '-c', $file))`Note that the first argument gets the `NODE_MODULES_PATH` prepended automatically.

Running Tests
=============

[](#running-tests)

To run the tests you need to first install the dependencies. You do this via composer with the following command:

```
php composer.phar install

```

Once that is done you just need to run the "index.php" file in the test directory. The easiest way to do this is to use the built-in PHP webserver.

```
cd test
php -S localhost:5000

```

Then in your web browser visit:

```
http://localhost:5000/index.php

```

Alternatively you can just run the tests from the command line although the output will contain a few HTML tags:

```
cd test
php index.php

```

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 81.7% 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 ~66 days

Recently: every ~162 days

Total

23

Last Release

3612d ago

Major Versions

1.7-rc1 → 2.02013-06-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/896136cf1c387050627051e5b3f0930540b45d1d9ed2484de11ac6f6a5004d69?d=identicon)[Nami-Doc](/maintainers/Nami-Doc)

---

Top Contributors

[![vendethiel](https://avatars.githubusercontent.com/u/199499?v=4)](https://github.com/vendethiel "vendethiel (67 commits)")[![david-sosa-valdes](https://avatars.githubusercontent.com/u/12551529?v=4)](https://github.com/david-sosa-valdes "david-sosa-valdes (7 commits)")[![eric1234](https://avatars.githubusercontent.com/u/43233?v=4)](https://github.com/eric1234 "eric1234 (7 commits)")[![kalys](https://avatars.githubusercontent.com/u/155511?v=4)](https://github.com/kalys "kalys (1 commits)")

---

Tags

asset-pipelinephpsprocketssprockets-php

### Embed Badge

![Health badge](/badges/nami-doc-sprockets-php/health.svg)

```
[![Health](https://phpackages.com/badges/nami-doc-sprockets-php/health.svg)](https://phpackages.com/packages/nami-doc-sprockets-php)
```

###  Alternatives

[nizsheanez/yii2-asset-converter

Less, Sass, Scss and Phamlp converter for Yii2. No system requires. yii2-composer support, Less autoupdate, customizing of output directory

64167.5k6](/packages/nizsheanez-yii2-asset-converter)[zizaco/lessy

Lessy is a simple and lean LESS compiler for Laravel

2316.7k](/packages/zizaco-lessy)[trentrichardson/cakephp-shrink

Compiles, combines, and minifies javascript, coffee, less, scss, and css

1619.3k](/packages/trentrichardson-cakephp-shrink)[jtgrimes/less4laravel

Bringing lessphp into Laravel

193.4k](/packages/jtgrimes-less4laravel)

PHPackages © 2026

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