crecket/dependency-manager

3.4 2016-12-14 17:03 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:19:24 UTC


README

Build Status Latest Release

介绍

这是一个简单小巧的 JavaScript 和 CSS 文件依赖管理器。所有文件将被合并、压缩并缓存。也支持 Less 和 Scss 文件,并且所有 CSS 输出都通过自动补全器处理。

需求

- matthiasmullie/minify ^1.3
- doctrine/cache ^1.6
- leafo/scssphp ^0.6.3
- tedivm/jshrink ^1.1
- leafo/lessphp ^0.5.0
- twig/twig ^1.23
- css-crush/css-crush ^dev-master

安装

  1. 使用 Composer 安装

    composer require crecket/dependency-manager

  2. 将 twig 扩展添加到你的 twig 视图

    $twig->addExtension(new Crecket\DependencyManager\Response());

用法

添加新文件

addFiles($fileList, $groupName);
  • 参数1:输入单个文件位置字符串或包含多个文件的数组。
  • 参数2:组名,这样你可以存储多个文件组
use Crecket\DependencyManager\Loader;
Loader::addFiles('testFile.css', 'cssGroupName');

// different group and multiple files this time
Loader::addFiles(array('testFile.css', '/some/folder/file_name.css'), 'cssGroupName2');

删除文件

removeFiles($groupName);
  • 参数1:包含文件组名。
Loader::removeFiles('cssGroupName2');

创建 URL

Twig: getFilesLink(minify, groupName)
  • 参数1:是否压缩文件
  • 参数2:组名
<script src="/minify.php{{ getFilesLink(true, 'jsGroupName') }}">
PHP: getFilesLink(
  • 参数1:是否压缩文件
  • 参数2:组名
<link href="/minify.php<?php echo Crecket\DependencyManager\Loader::getFilesLink(true, 'cssGroupName'); ?>" rel="stylesheet">

响应

标准 PHP 示例
$options = array(
    // Location that the default Doctrine/FilesystemCache will use. Location is based on the root
    // Required if no custom cache object is given
    'CacheLocation' => '/cache',

    // Optional, namespace to use for the doctrine file system cache
    'CacheNamespace'  => 'DependencyManagerNamespace',
);

define('ROOT', __DIR__); // Don't forget this!

try{
    $Response = new Crecket\DependencyManager\Response($options);

    echo $Response->getResult();
}catch(Crecket\DependencyManager\Exception $ex){ // catch errors
    echo $ex->getTitle();
    echo '<br>';
    echo $ex->getMessage();
}
Silex 路由示例

你可以很容易地使用 Silex 中的 URL 生成器创建指向此路由的 URL,使用

<script src="{{ url('dependency_minify', {id: GetFilesHash(false, 'jsGroupExample')}) }}"></script>

或没有 twig

<script src="/minify/<?php echo Loader::getHash(true, 'jsGroupExample'); ?>"

接下来创建一个路由

// ID contains the hash id
$app->get('/minify/{id}', function ($id) use ($app) {

    // Options
    $options = array(
        'CacheLocation' => '/cache',
        'CacheNamespace' => 'DependencyManagerNamespace'
    );

    try {
        // second parameter contains the hash id
        $Response = new Crecket\DependencyManager\Response($options, $id);

        // Get the response data
        $response_data = $Response->getResult();

        // Get the headers and status code for this request
        $header_data = Crecket\DependencyManager\Utilities::getHeaders();

    } catch (Crecket\DependencyManager\Exception $ex) { // catch errors
        // return a error
        return new Symfony\Component\HttpFoundation\Response(
            $ex->getTitle() . '<br>' . $ex->getMessage(),
            500,
            array()
        );
    }

    // Return content, headers and status code
    return new Symfony\Component\HttpFoundation\Response(
        $response_data,
        $header_data['status_code'],
        $header_data['headers']
    );
})->bind('dependency_minify');

调试

响应接受一个可选的第二个参数,该参数将确保忽略会话存储和密钥。这可以是一个哈希 ID,用于检索会话中存储的文件,或者是一个直接包含文件的数组

$file_list = array('/some/js/file.js', '/another/js/files.js');
$Response = new Crecket\DependencyManager\Response($options, $file_list);

或直接获取哈希值以用于路由/URL 生成器。

$file_hash = Loader::getHash($minify, $groupName);
$Response = new Crecket\DependencyManager\Response($options, $file_hash);

安全

为了确保只有你可以创建文件列表,请添加一个密钥。确保这个密钥是安全/足够长的!

Loader::Secret('some long secret passphrase');

问题

Css 中的字体文件

外部字体文件,例如 bootstrap 的 Glyphicons 可能会返回 404 错误,因为路径已更改。这些通常可以通过复制字体文件并将它们移动到你的网站根目录中的 /fonts 文件夹来解决。