hx / fusion-php
Fusion PHP 资产管道
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: 3.7@stable
README
Fusion是一个基础的PHP资产管道库。它提供以下功能:
- 简单的依赖,其中资产可以要求其他资产。
- 对CoffeeScript、SASS/SCSS和LESS文件的预处理。
- 通过UglifyJS将大量文件编译成单个文件并进行压缩。
依赖
通过在资产文件顶部注释中要求其他文件来指定依赖项
/** *= require brother.js *= require ../uncle/cousin.js */
您还可以要求文件的全局变量(注意,为了防止*/
中断注释,使用交替语法)
//= require_glob=children/* //= require_glob=children/*/*
通常,您可以使用任何注释样式,只要格式支持即可。例如,纯CSS不支持单行//
注释。
处理
<?php $file = Hx\Fusion::file('/path/to/file.scss');
此代码将新的Hx\Fusion\Asset\StyleSheet\Sass
实例分配给$file
,它是Hx\Fusion\Asset
的子类。子类的选择基于文件扩展名。
要指定基本目录,包括第二个参数。依赖路径将相对于给定的基本路径进行评估。如果省略此参数,则使用文件的目录作为基本目录。
<?php $file = Hx\Fusion::file('file.scss', '/path/to'); // Equivalent to previous example
Hx\Fusion\Asset
类为您提供了几个基本方法来检索有关文件的信息
<?php // Test if the represented file exists echo $file->exists() ? 'found' : 'missing'; // The path of the file, relative to the given base bath echo $file->path(); // The file's base path echo $file->basePath(); // The file's absolute path (base + path) echo $file->absolutePath(); // The file's content type (in this case, "text/css") echo $file->contentType();
您可以获取原始的、过滤的(处理的)和压缩的(最小化的)文档字符串
<?php // Raw contents of the file echo $file->raw(); // Filtered version of the file (processed by Sass, in this case) echo $file->filtered(); // Filtered AND minified version of the file echo $file->compressed();
集合
上述方法也适用于Hx\Fusion\AssetCollection
类,该类表示资产的有序集合。
考虑这三个文件a.coffee
、b.coffee
和c.coffee
alert 'File A'
#= require a.coffee alert 'File B'
#= require b.coffee alert 'File C'
我们可以以几种方式创建集合
<?php // Manually $files = new Hx\Fusion\AssetCollection([ Hx\Fusion::file('a.coffee'), Hx\Fusion::file('b.coffee'), Hx\Fusion::file('c.coffee') ]); // Using the glob() method $files = Hx\Fusion::glob('*.coffee'); // $files is an instance of Hx\Fusion\AssetCollection, which extends ArrayObject echo count($files); // 3 echo $files[0]->path(); // a.coffee
要获取依赖项集合
<?php $file = Hx\Fusion::file('c.coffee'); $deps = $file->dependencies(); echo count($deps); // 2 echo $deps[0]->path(); // a.coffee echo $deps[1]->path(); // b.coffee $all = $file->dependenciesAndSelf(); echo count($all); // 3 echo $all[2]->path(); // c.coffee
如上所示,dependenciesAndSelf()
方法返回一个包含调用实例的集合。这在将资产与其依赖项组合时非常有用。集合支持与单个资产相同的输出方法(raw()
、filtered()
和compressed()
)
<?php echo $all->compressed();
这将输出以下合并、压缩的JavaScript
alert('File A');alert('File B');alert('File C');
异常
Fusion异常位于Hx\Fusion\Exceptions
命名空间中,并且是Hx\Fusion\Exception
的子类。
处理文件
这些异常可能在处理文件时抛出,使用filtered()
或compressed()
方法时使用外部二进制文件。
BadInterpreter
当外部二进制文件找不到或不可执行时抛出。
SyntaxError
当外部二进制文件以非0状态退出时抛出,这可能是由于您的资产中的语法错误。异常消息将包括解释器发送到STDERR
的数据。
合并依赖项
这些异常可能在尝试使用dependencies()
或dependenciesAndSelf()
方法合并文件依赖项时抛出。
CircularDependency
当文件的依赖项依赖于该文件时抛出,例如,如果A需要B,而B又需要A。
MissingDependency
当通过require
指定的依赖项不存在时抛出。