sam-it/yii2-static-assets
Composer 安装命令:
composer require sam-it/yii2-static-assets
包简介
Generate static assets and build a docker nginx container
README 文档
README
Prevent publishing of assets at production time without changing your application.
Motivation
Nowadays docker is getting more and more attention and PHP applications are seeing different deployment scenarios. Where a few years ago if you split your nodes up at all you'd only split up the database server and the webserver that runs PHP as module or more recently via PHP-FPM, nowadays you want to split everything up.
Challenges
Having your webserver, for example nginx, running on a different server than PHP-FPM comes with several challenges, pros and cons:
- PHP can't write files and create publicly accessible URLs for them
- Cannot use local session storage
- File uploads that need to be publicly accessible need to be published to the webserver.
- File uploads that need to be protected have to accessible by all PHP nodes (so some kind of central storage is needed).
Solution
This extension will provide a solution for number 1 when it comes to Yii assets. The asset management system is nice when pushing changed assets directly to a server, it doesn't really work well in distributed environments though, so we need another approach.
This extension provides:
- A console command that will scan your source code and vendor directory and will extract all your assets.
- An AssetManager for use in production that will generate URLs for assets without checking for touching storage.
The workflow then becomes a bit different. During development the asset manager will act like a normal asset manager, publishing assets to the asset directory. When using docker-compose for the test environment you would mount the same host directory to the asset folder in the PHP container and in the webserver container.
During deployment the assetmanager simply returns URLs for assets on the assumption that they exist. Before deploying a new version of your app, you will rebuild your webserver container. This extension provides a console command that will publish all your assets to a directory of your choosing, this can then be used as part of the docker build context.
To publish your assets run the following command:
yii staticAssets/asset/publish build12345
This will create the directory build12345 inside your runtime directory and publish all assets there.
Asset discovery / publishing
Assets are discovered by recursively iterating over all folders and files.
Each file that ends with .php is then processed:
- The namespace is extracted via regular expression matching.
- The class name is extracted via regular expression matching.
- We use reflection to check if the class is an instance of
yii\web\AssetBundleand if so it is published.
Container building
To build an nignx container for server your application use this:
yii staticAssets/asset/build-container
You can configure the module to set some default values.
Configuration
For simple configuration use the ReadOnlyAssetManager in your application during production and development.
This asset manager will use a simpler "hash" function that keeps directory structure readable.
It supports $assetDevelopmentMode which allows for local asset development in a dockerized environment.
Asset development
The assumption is that you use docker-compose for local development, in which case you need to define a volume where assets are stored so that they are available in both the webserver as well as the phpfpm container:
volumes:
assets:
nginx:
image: [name of your nginx container, built by this module]
environment:
PHPFPM: "phpfpm:9000"
RESOLVER: "127.0.0.11"
ports:
- "12346:80" # Port where the application will be available
depends_on:
- phpfpm
volumes:
# Defines the named volume as read-only for the webserver.
# Note the dev-assets, which allows to easily identify development
# mode while using browsers' developer tools.
- type: volume
source: assets
target: /www/dev-assets
read_only: true
volume:
nocopy: true
phpfpm:
dns: 8.8.4.4
image: [ name of your PHPFPM docker image ]
environment:
DB_USER: root
DB_NAME: test
DB_PASS: secret
DB_HOST: mysql
depends_on:
- mysql
volumes:
# The source code is loaded into PHPFPM for local development,
# for production it should be baked into the image.
- .:/project:ro
# The asset volume, note the location which is where the ReadOnlyAssetManager
# will publish assets when in development mode.
- type: volume
source: assets
target: /tmp/assets
sam-it/yii2-static-assets 适用场景与选型建议
sam-it/yii2-static-assets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 82.28k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2017 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sam-it/yii2-static-assets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sam-it/yii2-static-assets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 82.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-02