项目作者: Erfans

项目描述 :
A Symfony bundle to download and include thirdparty assets in reusable bundles
高级语言: PHP
项目地址: git://github.com/Erfans/AssetBundle.git
创建时间: 2016-07-14T14:20:15Z
项目社区:https://github.com/Erfans/AssetBundle

开源协议:MIT License

下载


The purpose of this bundle is to manage third party assets for reusable bundles.

Motivation

As it is mentioned in Symfony
documentation:

A bundle should also not embed third-party libraries written in JavaScript, CSS or any other language.

Additionally due to license restrictions and conflicts a bundle may could
not include third party assets
(e.g. FOSCKEditorBundle).

To solve this situation, a bundle could only contain a configuration and
by running a command line this bundle will install the configured assets.

This bundle allows to define multiple agents (downloader or installer)
service and tag them with erfans_asset.agent
and add alias attribute to use them in the installing process.
Bundles can contain asset config files to install assets by proper agent.
(By now only Bower and File agents have been implemented.)

Installation

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

  1. $ composer require erfans/asset-bundle

Applications that don’t use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

  1. $ composer require erfans/asset-bundle

This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:

  1. <?php
  2. // app/AppKernel.php
  3. // ...
  4. class AppKernel extends Kernel
  5. {
  6. public function registerBundles()
  7. {
  8. $bundles = array(
  9. // ...
  10. new Erfans\AssetBundle\ErfansAssetBundle(),
  11. );
  12. // ...
  13. }
  14. // ...
  15. }

Step 3: Configuration

Default configuration for “ErfansAssetBundle”:

  1. #app\config\config.yml or
  2. #app\packages\erfans_asset.yml
  3. erfans_asset:
  4. enabled: false
  5. # Consider all bundles to check their asset config.
  6. all_bundles: false
  7. # Bundles to check their asset config file.
  8. bundles:
  9. # Default:
  10. - AppBundle
  11. # The default install directory. The default value is ?bundle/Resources/public; ?bundle will be replaced with the bundle directory.
  12. default_install_directory: '?bundle/Resources/public'
  13. # Configurations to pass to each agent by their alias.
  14. agents:
  15. # File agent to download defined assets by url.
  16. file:
  17. # Default directory to install assets. default directory is '?bundle/Resources/public'.
  18. default_install_directory: '?bundle/Resources/public'
  19. # Create a new directory for each file with name of alias in download directory.
  20. create_directory: true
  21. # Bower agent to download defined assets.
  22. bower:
  23. # Directory path to cache assets before installing. You need to change it if you use Symfony2.
  24. cache_path: '%kernel.root_dir%/../var/erfans_asset/bower_cache/%kernel.environment%'
  25. # Default directory to install assets. The default value is '?bundle/Resources/public'.
  26. default_install_directory: '?bundle/Resources/public'
  27. # Github token to extend limitation of 60 repository per hour to 5000.
  28. github_token: ~

However, the usual necessary configurations are:

  1. #app\config\config.yml or
  2. #app\packages\erfans_asset.yml
  3. erfans_asset:
  4. all_bundles: true
  5. default_install_directory: '?bundle/Resources/public'
  6. agents:
  7. file:
  8. create_directory: true
  9. bower:
  10. github_token: github_token_to_extend_limitation

Please note, if you use Symfony2 you need to change the cash directory for bower configuration.

Step 3: Add asset config file

To define required third party assets for each bundle create an asset.yml file
in Resources/config directory of bundle.

  1. #AppBundle\Resources\config\asset.yml
  2. assets:
  3. jquery: # alias of asset
  4. installer: bower # name of installer
  5. id: jquery # id of repository which passes to the installer
  6. version: ~1.9 # version of repository
  7. jquery_easing:
  8. installer: file
  9. id: http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js

Step 4: Install assets

To download and copy defined assets to the target folder run command erfans:asset:install
in Symfony console. You can limit the installing assets to specific bundles, by passing the
bundle names as arguments, e.g. erfans:asset:install AppBundle.

The bower agent of this bundle is based on bowerphp
which does not currently support the downloading assets by url.
Therefore, a file installer is added to the bundle to download remote files and put them in the target directory.

Step 5: Add assets to frontend

Now you can add the installed assets to your twig or other asset loaders such
as RequireJs. If you used the public directory of a bundle as install directory,
then you need also to transfer them to public folder by assets:install command.