waughj/file-loader
Composer 安装命令:
composer require waughj/file-loader
包简介
Object for easily generating image sources with autoversioning for handling cache corruption.
README 文档
README
Object for easily generating image sources with autoversioning for handling cache corruption.
Loader object takes in an optional hash map as its 1 argument ( though using this object without any arguments will just return the very string you give it from getSource ). The object will recognize any & all o' 4 o' the following optional keys for that hash map:
- "directory-url": URL directory in which to look for file. If set, automatically appends this before given local filename. If none set, URL will just be the local.
- "directory-server": Server directory in which file is located. Used for finding modified time for this file, used for determining the "version" o' this file automatically for easy, automatic cache clearing when a file changes ( & needs to be reloaded ). If not set, no versioning is used.
- "shared-directory": Directory that goes after both URL & Server directories listed before.
- "extension": File extension to automatically add to end o' local file.
After setting this object up, you can use it to get sources for local filenames easily.
Example
All CSS files are kept in /home/example.com/public_html/assets/css & are loaded from https://www.example.com/assets/css:
use WaughJ\FileLoader\FileLoader;
$loader = new FileLoader
([
'directory-url' => 'https://www.example.com',
'directory-server' => '/home/example.com/public_html',
'shared-directory' => 'assets/css',
'extension' => 'css'
]);
Then you can easily load files with just the local name with the "getSourceWithVersion" method:
// Get main CSS file
$loader->getSourceWithVersion( 'main' );
This will output "https://www.example.com/assets/css/main.css?m={#######}", with the #s being a long # representing the last modified time.
You can make new versions o' a loader with 1 o' the 4 attributes changed with the following methods, which return a new instance o' the loader object with all the other attributes the same:
- changeURLDirectory
- changeServerDirectory
- changeSharedDirectory
- changeExtension
To change multiple attributes easily, just chain them:
$loader2 = $loader1->changeURLDirectory( 'https:/www.new-website.com' )->changeExtension( 'sass' );
If you want to change the same loader, just set the loader equal to change, replacing the ol' instance with the new:
$loader1 = $loader1->changeURLDirectory( 'https:/www.new-website.com' )->changeExtension( 'sass' );
Error Handling
The "getSourceWithVersion" method analyzes the file on the server's modified date to create the version string. If it can't access the file, it throws a WaughJ\FileLoader\MissingFileException exception, which includes the server & url paths in it. If you want a safe way to fallback to versionless source without breaking the website or program, run a simple try & catch & in the catch use the exception object's "getFallbackContent" method to get the versionless source:
$file_url = null;
try
{
$file_url = $loader->getSourceWithVersion( 'main' );
}
catch ( MissingFileException $e )
{
// Perhaps run some logging code that logs that $e->getFilename() couldn't be found.
// If exception is thrown, $file_url will be the equivalent o' $loader->getSource( 'main' ).
$file_url = $e->getFallbackContent();
}
Changelog
0.5.0
- Add Getter Methods & Revamp Error Handling
0.4.0
- Add Ability to Get File Version
0.3.0
- Add Ability to Get Extension from Filename
0.2.0
- Add Change Methods
0.1.0
- Initial Version
waughj/file-loader 适用场景与选型建议
waughj/file-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 290 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「loader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 waughj/file-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 waughj/file-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 waughj/file-loader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony2 bundle that provides features to load assets
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
An environment variable convenience library extension for vlucas/phpdotenv
PhpStratum SQLite: A stored procedure loader and wrapper generator for SQLite
Zend Framework 1 Loader package
A data loader command base class for Laravel
统计信息
- 总下载量: 290
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: AGPL-3.0-or-later
- 更新时间: 2018-11-27