nami-doc / sprockets-php

此包最新版本(2.1.5)没有可用的许可证信息。

Sprockets-PHP 是针对 PHP 的 Sprockets(Rails 资产管道)

2.1.5 2016-06-18 17:52 UTC

README

什么是 Sprockets-PHP

Sprockets-PHP 是 Sprockets 的移植,Sprockets 是一个著名的 Rails 资产管理器。Sprockets-PHP 允许您通过处理预处理器、依赖关系、压缩和缓存来管理您的资产。资产管道将读取您的主文件(通常是 "application.js" 或 "application.css"),读取指令,并应用所有文件的过滤器。以下是一个使用示例

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 <[base password]>

它主要用于处理 JS 和 CSS,但也可以用于 HTML(HAML、Twig、Slim...)。您可以通过非常灵活的方式添加自己的过滤器(见下文)。

如何使用它?!

您必须创建一个 Sprockets\Pipeline 实例。参数是从 Pipeline 中搜索文件的 "基本路径" 数组。

如果您想直接调用 Pipeline,则可以执行 $pipeline($asset_type)。例如 $pipeline('css');。框架将从一个基本路径加载 application.css。此文件必须包含 "指令",类似于 Sprockets 的指令。

// 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();

资产路径

资产路径被划分为 "模块",以尽可能灵活

{
  "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/"
    ]
  }
}

每个模块中有 2 个键:`directories`,它列出 Pipeline 必须在其中搜索文件的目录,以及 `prefixes`,它将路径扩展名添加到目录路径中(例如,`js` 文件将添加 `javascripts/` 路径扩展名)。

例如,如果我们执行 $pipeline('js'),则 Pipeline 将尝试查找以下文件

  • app/themes/%template%/assets/javascripts/application.js(在上面的示例中替换了 %template%
  • 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

此示例文件允许优雅地使用类似 Rails 的 javascripts/ 目录来处理 js 文件,也支持 //= require jquery/jquery 来查找 vendor/bower/jquery/jquery.js

只有 "有意义" 的扩展名才是重要的(使用白名单)。

/**
 * 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.
 */

选项

以下是选项及其默认值

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

只需将它们传递给路径即可。

缓存

需要注意的是:即使您没有使用 Sprockets\Cache,资产管道也会在缓存目录中保持文件列表缓存,以加快路径查找速度。

指令语法

目前支持三种语法。

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

支持的指令

可用的指令有:`require`、`require_directory`、`require_tree` 和 `depends_on`

require

直接从相对路径或基本路径要求一个文件。您还可以提供一个目录名,如果此目录中有一个名为 "index.$type"(在这里,是 "index.css")的文件,则支持中括号扩展。

require_directory

要求目录中的每个文件。非递归。

require_tree

递归要求目录树中的每个文件。

depends_on

即使文件没有包含,也会将文件添加到依赖关系中。例如,在 application.css 中

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

如果此文件更改,则整个样式表(及其依赖项)将重新编译(这适用于某些预处理器内联)。

过滤器

可用的过滤器有

语言

JavaScript

样式表

Html

  • .haml : Haml (通过 MtHaml,例如可以构建一个 Twig 版本)

添加过滤器非常简单(创建一个 .twig 过滤器或一个 .md,例如)。只需将其添加到管道

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

必须实现一个类似于 \Sprockets\Filter\Interface 的接口

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

您还可以继承 Sprockets\Filter\Base,这为您提供了访问

  • $this->pipeline 当前管道实例
  • $this->processNode() 方法,传递一个参数数组,自动引号,如下所示: array('modulename/bin/mod', '-c', $file)) 注意第一个参数会自动添加 NODE_MODULES_PATH

运行测试

要运行测试,首先需要安装依赖项。您可以通过 composer 使用以下命令来完成此操作

php composer.phar install

完成此操作后,您只需运行测试目录中的 "index.php" 文件。最简单的方法是使用内置的 PHP 服务器。

cd test
php -S localhost:5000

然后在您的网页浏览器中访问

https://:5000/index.php

或者您可以直接从命令行运行测试,尽管输出将包含一些 HTML 标签

cd test
php index.php