该软件包已被弃用且不再维护。未建议替代软件包。

简单的资产管理器


README

简单的资产管理器

版本 : 0.1.7

Build Status Scrutinizer Code Quality Code Coverage Build Status GuardRails badge

Sam是你的朋友。Sam就像你一样,不喜欢处理复杂的资产相关事务。

Sam是一个简单的资产管理器,用于帮助PHP项目的资产管理。你将能够轻松描述希望将资产编译到何处。

Sam最初是为了与SamBundle一起使用而开发的,但它也可以独立使用。使用Sam独立需要编写构建任务代码。使用SamBundle,你将能够使用更方便的yml配置语法。

安装

使用composer

    composer require johnkrovitch/sam

使用方法

过滤器

过滤器是一个将一个或多个源文件转换为一个或多个目标文件的过程,使用二进制或非二进制方式。过滤器可以有选项,例如二进制路径。

目前Sam有以下内置过滤器。还将添加更多。你还可以添加自己的过滤器

  • Compass过滤器

Compass过滤器将使用Compass二进制文件将".scss"文件转换为".css"文件。

    $configuration = [
        'compass' => [
            // put here the compas binary path. By default it will assume
            // that the binary is loaded in $PATH
            'bin' => 'compass'
        }
    ];
  • Minify过滤器

Minify过滤器使用matthiasmullie/minify库来压缩css和js文件。目前没有可用的选项。

  • 合并过滤器

合并过滤器将多个文件合并为一个。它可以减少HTTP请求的数量。目前没有可用的选项。

  • 复制过滤器

复制过滤器将一个或多个文件复制到一个或多个目录中。

构建你的过滤器

为了使用编译资产,你必须使用FilterBuilder构建过滤器。它将检查给定的配置并创建符合此配置的过滤器实例。

    
    // enable the filters with default configuration
    $configuration = [
        'compass' => [],
        'merge' => [],
        'minify' => [],
    ];
    
    // an event dispatcher is required. Some events will be dispatched
    // before the application of each filter
    $eventDispatcher = new EventDispatcher();
    
    // build tasks using the builder
    $builder = new FilterBuilder($eventDispatcher);
    
    // filters are build, they can be passed to the runner
    $filters = $builder->build($configuration);

任务

任务将一个或多个过滤器应用到一组源文件到一个目录或一组目标文件中。它描述了你的资产编译过程。

构建你的任务

    $taskConfigurations = [
        // main.css is just a name, you can put what ever you want
        'main.css' => [
            // this filters will be applied in this order to the destination files 
            'filters' => [
                'compass',
                'minify',
                'merge',
            ],
            // sources file to be compiled
            'sources' => [
                'src/MyBundle/Resources/public/css/mycss.css',
                'app/Resources/public/css/mycss.css',
                'css/mycss.css',
                'vendor/a-library/css/lib.css'
            ],
            // all assets will be minified and merged to this file
            'destinations' => [
                'web/css/main.min.css'
            ],
        ],
        
        'main.js' => [
            'filters' => [
                'compass',
                'minify',
                'merge',
            ],
            'sources' => [
                'css/myjs.js',
                'css/mycss.css',
                'css/mycss.scss',
            ],
            // all assets will be minified and merged to this directory
            'destinations' => [
                'web/css'
            ],
        ]
    ];
    
    // build tasks using the builder
    $builder = new TaskBuilder();
    $tasks = $builder->build($taskConfigurations);
    

运行任务

一旦你的过滤器配置并构建,一旦你的任务创建,你应该使用TaskRunner运行任务。应该创建一个规范化器来为过滤器规范化文件名。

    // used to normalize file names because multiple pattern could be passed
    $normalizer = new Normalizer('/path/to/project_root');
    
    // used to locate the file
    $locator = new Locator($normalizer);
    
    // create the runner
    $runner = new TaskRunner(
        $filters,
        $locator,
        // debug
        false
    );

    // run your task
    foreach ($tasks as $task) {
        $runner->run($task);
    }

SamBundle

Sam是为与Symfony一起使用而设计的,SamBundle将Sam库与Symfony集成。它将允许你将资产配置放入yaml文件中。它将简化使用composer集成的供应商js/css库(如bootstrap、jquery...)。

您不再需要使用BootstrapBundle或jQueryBundle。您只需使用composer引入它们,然后轻松地复制、合并、压缩这些文件与您的自定义资源。

问题

如果您在使用这个库时遇到问题,请随时打开一个issue。

许可证

Sam库是在MIT许可证下发布的。