idci/asset-loader-bundle
Composer 安装命令:
composer require idci/asset-loader-bundle
包简介
Symfony2 bundle that provides features to load assets
README 文档
README
Introduction
With the Symfony 2 (or 3) framework, we found it troublesome to load assets for custom form type widgets. A recurrent issue which comes up is the loading of front dependencies embedded to the widgets.
Issue 1 - dependencies order
Let's say you want to create a form type, child of the text form type, whose widget will color the background of the input type text; as soon as the input is empty. Your widget will look like this:
Form/fields.html.twig
{%- block colored_text_widget -%}
<div class="colored">
{{- form_widget(form) -}}
</div>
<style>
.colored input[type="text"] {
border-color: #c9302c,
background-color: #f3d9d9
}
</style>
<script type="text/javascript">
$(document).on('change', '.colored input[type="text"]', function () {
if ($input.val()) {
$input.css({
'border-color': '#cccccc',
'background-color': '#ffffff'
});
} else {
$input.css({
'border-color': '#c9302c',
'background-color': '#f3d9d9'
});
}
});
</script>
{%- endblock -%}
As you can see, this javascript code requires jQuery. JQuery will not be available when this script will be executed (unless you place the Jquery script in the head of the html document, but we don't want that)
A possible solution would be to wrap this code with the following vanilla function
window.addEventListener('load', function () { // code goes here ... });
This way, when the code will be executed, Jquery should be ready to be used. But what if your widget is based on an entire library?
Issue 2 - dependencies duplication
Given the same form type as in the example above, if you have a page in your web application that render this form 3 times, you will end up with your scripts and stylesheets duplicated 3 times in the DOM. You only need it once.
This bundle attempts to solve those issues.
Installation
Add the dependency in your composer.json file:
"require": { ... "idci/asset-loader-bundle": "dev-master" },
Install these new dependencies in your application using composer:
$ php composer.phar update
Or with docker and docker-compose:
$ make composer-update
Register needed bundles in your application kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new IDCI\Bundle\AssetLoaderBundle\IDCIAssetLoaderBundle(), ); }
If you want to activate the subscriber to load your assets automatically (more on that later, add the following in your config.yml file.
# app/config/config.yml idci_asset_loader: providers: load_all: true
Usage
Asset declaration
Adding assets to your form type is pretty simple:
- Your AbstractType must implements the method getAssetCollection() from the AssetProviderInterface interface. The getAssetCollection contains an array of Asset Objects.
- You must define your type as a service and add the tag with name idci_asset_loader.asset_provider
In the example below, we load assets from a form type. An AssetProvider does not necessarily have to be a form type, any service will do the job.
AbstractType
namespace MyBundle/Form/Type; use IDCI\Bundle\AssetLoaderBundle\AssetProvider\AssetProviderInterface; use IDCI\Bundle\AssetLoaderBundle\Model\Asset; use IDCI\Bundle\AssetLoaderBundle\Model\AssetCollection; class MyType extends AbstractType implements AssetProviderInterface { /** * @var AssetCollection */ private $assetCollection; /** * Constructor */ public function __construct() { $this->assetCollection = new AssetCollection(); } /** * {@inheritDoc} */ public function getAssetCollection() { return $this->assetCollection; } /** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { $this->assetCollection->add(new Asset('MyBundle:Form:form_type_asset.html.twig', $options)); ... return $view->vars; } .... }
If you have multiple assets which must be loaded in a predicatble order, you can add a priority to the Asset (-1 by default). The higher the priority, the sooner it will be load in the DOM.
$this->assetCollection->add(new Asset('MyBundle:Form:form_type_asset_1.html.twig', array(), 0)); $this->assetCollection->add(new Asset('MyBundle:Form:form_type_asset_2.html.twig', array( 'options' => $options, 'form' => $view ), 1));
Services.yml
services: my_type: class: MyBundle\Form\Type\MyType tags: - { name: form.type, alias: my_type } - { name: idci_asset_loader.asset_provider, alias: my_type }
Loading your assets manually
You can use the idci_asset_loader.asset_dom_loader service to load the asset for one or all the providers. This will register a subscriber which will append the assets to the dom (at the end of the body) on kernel response.
<?php // Load assets from all providers $this->get('idci_asset_loader.asset_dom_loader')->loadAll(); // Load assets from one provider $this->get('idci_asset_loader.asset_dom_loader')->load('my_type');
Loading your assets automatically
In most case, you will just want to let the subscriber load all the assets for you. To load all the assets from all providers:
# app/config/config.yml idci_asset_loader: providers: load_all: true
You can also load some specific providers. If load_all is set to true, the following will have no impact.
# app/config/config.yml idci_asset_loader: providers: load_only: - my_provider_service_alias_1 - my_provider_service_alias_2 # - ...
Tests
Run the tests:
$ php ./vendor/bin/phpunit --coverage-text
Or with docker and docker-compose:
$ make phpunit
idci/asset-loader-bundle 适用场景与选型建议
idci/asset-loader-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63.92k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「assets」 「form」 「loader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 idci/asset-loader-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 idci/asset-loader-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 idci/asset-loader-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Asset Management for PHP
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
统计信息
- 总下载量: 63.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-11