bukashk0zzz/liip-imagine-serialization-bundle
Composer 安装命令:
composer require bukashk0zzz/liip-imagine-serialization-bundle
包简介
Provides integration between LiipImagineBundle and JMSSerializerBundle.
关键字:
README 文档
README
About
Provides integration between LiipImagineBundle and
JMSSerializerBundle.
Allows to generate full or relative URIs to entity fields mapped with @Bukashk0zzz and @JMS annotations during the serialization.
Also bundle supports VichUploaderBundle field type.
Installation Symfony Flex
composer config extra.symfony.allow-contrib true
composer require bukashk0zzz/liip-imagine-serialization-bundle
Installation without Symfony Flex
composer require bukashk0zzz/liip-imagine-serialization-bundle
Add the bundle to app/AppKernel.php
$bundles = array( // ... other bundles new Bukashk0zzz\LiipImagineSerializationBundle\Bukashk0zzzLiipImagineSerializationBundle(), );
Configuration
Add this to your config.yml:
bukashk0zzz_liip_imagine_serialization: # Set true for generating url for vichUploader fields vichUploaderSerialize: false # Set true for generating url with host for vichUploader fields includeHost: false # Set true for adding original field value to object includeOriginal: false # Set true for adding host url to original value for vichUploader fields includeHostForOriginal: false # You can pass there your UrlNormalizer class that implements UrlNormalizerInterface originUrlNormalizer: null # You can pass there your UrlNormalizer class that implements UrlNormalizerInterface filteredUrlNormalizer: null
Usage
Add the next class to the use section of your entity class.
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation as Bukashk0zzz;
Bundle provides two annotations which allow the serialization of url or @Vich\UploadableField fields in your entities.
At first you have to add @Bukashk0zzz\LiipImagineSerializableClass to the entity class which has image fields.
Then you have to add @Bukashk0zzz\LiipImagineSerializableField annotation to the field you want to serialize.
Annotation @Bukashk0zzz\LiipImagineSerializableClass does not have any option.
Annotation @Bukashk0zzz\LiipImagineSerializableField has one required option filter which value should link to the LiipImagine filter.
It can be set like this @Bukashk0zzz\LiipImagineSerializableField("photoFile") or @Bukashk0zzz\LiipImagineSerializableField(filter="photoFile").
filter can be array of filters in this case serialized field will be also array.
For example if you add annotation @Bukashk0zzz\LiipImagineSerializableField(filter={"big", "small"}) for field image then you get:
{
"image": {
"big": "/uploads/users/big/5659828fa80a7.jpg",
"small": "/uploads/users/small/5659828fa80a7.jpg"
}
}
Also there is another two options:
vichUploaderField- If you use VichUploaderBundle for your uploads you must specify link to the field with@Vich\UploadableFieldannotationvirtualField- By default serializer will override field value with link to filtered image. If you addvirtualFieldoption serializer will add to serialized object new field with name that you provided in this option and url to filtered image, original field in this case will be unattached. This option are required if you're using an array of filters.
Don't forget that to serialize image fields they also should be marked with @JMS annotations to be serialized.
The generated URI by default:
{
"photo": "http://example.com/uploads/users/photos/5659828fa80a7.jpg",
"cover": "http://example.com/uploads/users/covers/456428fa8g4a8.jpg"
}
The generated URI with includeHost set to false:
{
"photo": "/uploads/users/photos/5659828fa80a7.jpg",
"cover": "/uploads/users/covers/456428fa8g4a8.jpg"
}
If you need to change url before passing it to LiipImagine, for example you need to swap origin name, you can use originUrlNormalizer option in bundle config.
bukashk0zzz_liip_imagine_serialization: originUrlNormalizer: AppBundle\Normalizer\UrlNormalizer
If you need to change url after LiipImagine processing, for example you need to swap origin domain, you can use filteredUrlNormalizer option in bundle config.
bukashk0zzz_liip_imagine_serialization: filteredUrlNormalizer: AppBundle\Normalizer\UrlNormalizer
UrlNormalizer class must implement UrlNormalizerInterface
<?php namespace AppBundle\Normalizer; use Bukashk0zzz\LiipImagineSerializationBundle\Normalizer\UrlNormalizerInterface; /** * Url normalizer */ class UrlNormalizer implements UrlNormalizerInterface { /** * {@inheritdoc} */ public function normalize($url){ return str_replace('photo.jpg', 'my_photo.jpg', $url); } }
Events
There are two events:
- bukashk0zzz_liip_imagine.event_pre_origin_normalize // Dispatch before origin url normalization
- bukashk0zzz_liip_imagine.event_pre_filtered_normalize // Dispatch before filtered url normalization
Example subscriber:
services: app.liip_imagine_serialization_subscriber: class: AppBundle\Subscribers\LiipImagineSerializationEventSubscriber tags: - { name: bukashk0zzz_liip_imagine_subscriber }
<?php namespace AppBundle\Subscribers; use Bukashk0zzz\LiipImagineSerializationBundle\Event\UrlNormalizerEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * LiipImagineSerializationEventSubscriber */ class LiipImagineSerializationEventSubscriber implements EventSubscriberInterface { /** * @return array */ public static function getSubscribedEvents() { return [ UrlNormalizerEvent::ORIGIN => [ ['normalizeOrigin', 10], ], UrlNormalizerEvent::FILTERED => [ ['normalizeFiltered', 10], ], ]; } /** * @param UrlNormalizerEvent $event */ public function normalizeOrigin(UrlNormalizerEvent $event) { $event->setUrl(str_replace('photo', 'newPhoto', $event->getUrl())); } /** * @param UrlNormalizerEvent $event */ public function normalizeFiltered(UrlNormalizerEvent $event) { $event->setUrl(str_replace('example.com', 'img.example.com', $event->getUrl())); } }
Example
<?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation as JMS; use Vich\UploaderBundle\Mapping\Annotation as Vich; use Bukashk0zzz\LiipImagineSerializationBundle\Annotation as Bukashk0zzz; use Symfony\Component\HttpFoundation\File\File; /** * User Entity * * @ORM\Table(name="users") * @ORM\Entity() * * @Vich\Uploadable * @Bukashk0zzz\LiipImagineSerializableClass */ class User { /** * @var string $coverUrl Cover url * * @ORM\Column(type="string", length=255) * * @JMS\Expose * @JMS\SerializedName("cover") * * @Bukashk0zzz\LiipImagineSerializableField("thumb_filter") */ public $coverUrl; /** * @var string $photoName Photo name * * @ORM\Column(type="string", length=255) * * @JMS\Expose * @JMS\SerializedName("photo") * * @Bukashk0zzz\LiipImagineSerializableField("thumb_filter", vichUploaderField="photoFile") */ public $photoName; /** * @var File $photoFile Photo file * * @JMS\Exclude * * @Vich\UploadableField(mapping="user_photo_mapping", fileNameProperty="photoName") */ public $photoFile; }
Copyright / License
See LICENSE
bukashk0zzz/liip-imagine-serialization-bundle 适用场景与选型建议
bukashk0zzz/liip-imagine-serialization-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.56k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2016 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「serialization」 「bundle」 「imagine」 「JMSSerializerBundle」 「integration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bukashk0zzz/liip-imagine-serialization-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bukashk0zzz/liip-imagine-serialization-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bukashk0zzz/liip-imagine-serialization-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fork of jms/serializer 1.14.1 with support for modern PHP versions.
2lenet/EasyAdminPlusBundle
De/normalize business objects without tightly coupling them to your normalization format
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
统计信息
- 总下载量: 17.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-09