erfans / asset-bundle
用于安装第三方资源的 Symfony 扩展包。
Requires
- php: >=7.1
- beelab/bowerphp: ~0.5
- symfony/framework-bundle: ~3.0|~4.0
This package is auto-updated.
Last update: 2022-03-29 00:21:52 UTC
README
此扩展包的目的是管理可重用扩展包的第三方资源。
动机
如 Symfony 文档所述
扩展包也不应嵌入使用 JavaScript、CSS 或任何其他语言编写的第三方库。
此外,由于许可限制和冲突,扩展包可能无法包含第三方资源(例如,FOSCKEditorBundle)。
为了解决这个问题,扩展包只能包含配置,并通过运行命令行,此扩展包将安装配置的资源。
此扩展包允许定义多个代理(下载器或安装器)服务,并使用 erfans_asset.agent
标记它们,以便在安装过程中使用。扩展包可以包含资产配置文件,通过适当的代理安装资产。(目前只实现了 Bower 和 File 代理。)
安装
使用 Symfony Flex 的应用程序
打开命令行,进入您的项目目录,并执行以下命令
$ composer require erfans/asset-bundle
不使用 Symfony Flex 的应用程序
步骤 1:下载扩展包
打开命令行,进入您的项目目录,并执行以下命令以下载此扩展包的最新稳定版本
$ composer require erfans/asset-bundle
此命令要求您全局安装了 Composer,如 Composer 文档中的安装章节所述。
步骤 2:启用扩展包
然后,通过将其添加到项目 app/AppKernel.php
文件中注册的扩展包列表中来启用扩展包
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Erfans\AssetBundle\ErfansAssetBundle(), ); // ... } // ... }
步骤 3:配置
"ErfansAssetBundle" 的默认配置
#app\config\config.yml or #app\packages\erfans_asset.yml erfans_asset: enabled: false # Consider all bundles to check their asset config. all_bundles: false # Bundles to check their asset config file. bundles: # Default: - AppBundle # The default install directory. The default value is ?bundle/Resources/public; ?bundle will be replaced with the bundle directory. default_install_directory: '?bundle/Resources/public' # Configurations to pass to each agent by their alias. agents: # File agent to download defined assets by url. file: # Default directory to install assets. default directory is '?bundle/Resources/public'. default_install_directory: '?bundle/Resources/public' # Create a new directory for each file with name of alias in download directory. create_directory: true # Bower agent to download defined assets. bower: # Directory path to cache assets before installing. You need to change it if you use Symfony2. cache_path: '%kernel.root_dir%/../var/erfans_asset/bower_cache/%kernel.environment%' # Default directory to install assets. The default value is '?bundle/Resources/public'. default_install_directory: '?bundle/Resources/public' # Github token to extend limitation of 60 repository per hour to 5000. github_token: ~
然而,通常必要的配置是
#app\config\config.yml or #app\packages\erfans_asset.yml erfans_asset: all_bundles: true default_install_directory: '?bundle/Resources/public' agents: file: create_directory: true bower: github_token: github_token_to_extend_limitation
请注意,如果您使用 Symfony2,您需要更改 bower 配置的缓存目录。
步骤 3:添加资产配置文件
要为每个扩展包定义所需的第三方资源,在扩展包的 Resources/config
目录中创建一个 asset.yml
文件。
#AppBundle\Resources\config\asset.yml assets: jquery: # alias of asset installer: bower # name of installer id: jquery # id of repository which passes to the installer version: ~1.9 # version of repository jquery_easing: installer: file id: http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js
步骤 4:安装资产
要下载并将定义的资产复制到目标文件夹,请在Symfony控制台中运行命令 erfans:asset:install
。您可以通过传递包名作为参数来限制安装的资产到特定的包,例如 erfans:asset:install AppBundle
。
此包的bower代理基于 bowerphp,目前不支持通过URL下载资产。因此,在包中添加了一个文件安装器,用于下载远程文件并将它们放入目标目录。
第5步:将资产添加到前端
现在您可以将已安装的资产添加到您的twig或其他资产加载器,如RequireJs。如果您将包的公共目录用作安装目录,则还需要通过 assets:install
命令将它们转移到公共文件夹。