ajgl / composer-symlinker
Composer 脚本用于创建符号链接。
Requires
- composer-plugin-api: ^2.0
Requires (Dev)
README
AjglComposerSymlinker 组件提供了一个 Composer 插件,可以将使用 composer 安装的包的路径符号链接到不同的位置。它适用于网页资源。
此插件允许您将网页资源安装到 vendor
目录,并将它们符号链接到通过 HTTP 服务器公开的目录,而无需提供具有自定义安装器的特殊 composer 包类型。
假设以下项目布局,我们想要安装 twbs/bootstrap
包
project/
├── src/
│ ├── Controller.php
│ ├── Model.php
│ └── View.php
├── vendor/
└── www/
├── assets/
| ├── css
| ├── fonts
| ├── images
| └── js
├── index.php
└── .htaccess
使用 composer 安装的任何网页资源都将存储在 vendor
目录中,但我们需要将它们存储在 www/assets
目录中。在“用法”部分,我们将看到如何实现这一点。
有一些替代方案,但它们通常需要在所需的包定义中定义自定义包类型。
安装
要安装此组件的最新稳定版本,请打开控制台并执行以下命令
$ composer require ajgl/composer-symlinker
用法
1. 需求源包
将所需的包添加到 composer.json 文件的 require
部分
{ "require": { "twbs/bootstrap": "^3.3" } }
2. 定义符号链接
在 composer.json 文件的 extra
部分中创建符号链接定义,添加一个 ajgl-symlinks
部分
{ "extra": { "ajgl-symlinks": { "twbs/boostrap": { "dist/css": "web/assets/css/bootstrap", "dist/js": "web/assets/js/bootstrap", "dist/fonts/glyphicons-halflings-regular.eot": "web/assets/fonts/glyphicons-halflings-regular.eot", "dist/fonts/glyphicons-halflings-regular.svg": "web/assets/fonts/glyphicons-halflings-regular.svg", "dist/fonts/glyphicons-halflings-regular.ttf": "web/assets/fonts/glyphicons-halflings-regular.ttf", "dist/fonts/glyphicons-halflings-regular.woff": "web/assets/fonts/glyphicons-halflings-regular.woff", "dist/fonts/glyphicons-halflings-regular.woff2": "web/assets/fonts/glyphicons-halflings-regular.woff2" } } } }
如你所见,你可以链接目录或文件。在 Windows 平台上,链接将通过 NTFS 的 junction 功能创建,因此你只能链接目录。
3. 执行 composer
一旦 composer.json 文件完整
{ "require": { "ajgl/composer-symlinker": "^0.3", "twbs/bootstrap": "^3.3" }, "extra": { "ajgl-symlinks": { "twbs/bootstrap": { "dist/css": "web/assets/css/bootstrap", "dist/js": "web/assets/js/bootstrap", "dist/fonts/glyphicons-halflings-regular.eot": "web/assets/fonts/glyphicons-halflings-regular.eot", "dist/fonts/glyphicons-halflings-regular.svg": "web/assets/fonts/glyphicons-halflings-regular.svg", "dist/fonts/glyphicons-halflings-regular.ttf": "web/assets/fonts/glyphicons-halflings-regular.ttf", "dist/fonts/glyphicons-halflings-regular.woff": "web/assets/fonts/glyphicons-halflings-regular.woff", "dist/fonts/glyphicons-halflings-regular.woff2": "web/assets/fonts/glyphicons-halflings-regular.woff2" } } } }
打开控制台并执行 composer update
或 composer install
命令
$ composer update -v
你将在 composer 输出中看到以下消息
Creating symlinks
Symlinking package "twbs/bootstrap"
Symlinking "/home/aj/tmp/lala/vendor/twbs/bootstrap/dist/css" to "/home/aj/tmp/lala/web/assets/css/bootstrap": symlink created
Symlinking "/home/aj/tmp/lala/vendor/twbs/bootstrap/dist/js" to "/home/aj/tmp/lala/web/assets/js/bootstrap": symlink created
Symlinking "/home/aj/tmp/lala/vendor/twbs/bootstrap/dist/fonts/glyphicons-halflings-regular.eot" to "/home/aj/tmp/lala/web/assets/fonts/glyphicons-halflings-regular.eot": symlink created
Symlinking "/home/aj/tmp/lala/vendor/twbs/bootstrap/dist/fonts/glyphicons-halflings-regular.svg" to "/home/aj/tmp/lala/web/assets/fonts/glyphicons-halflings-regular.svg": symlink created
Symlinking "/home/aj/tmp/lala/vendor/twbs/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf" to "/home/aj/tmp/lala/web/assets/fonts/glyphicons-halflings-regular.ttf": symlink created
Symlinking "/home/aj/tmp/lala/vendor/twbs/bootstrap/dist/fonts/glyphicons-halflings-regular.woff" to "/home/aj/tmp/lala/web/assets/fonts/glyphicons-halflings-regular.woff": symlink created
Symlinking "/home/aj/tmp/lala/vendor/twbs/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2" to "/home/aj/tmp/lala/web/assets/fonts/glyphicons-halflings-regular.woff2": symlink created
4. 包不在 packagist.org 上可用
如果你想要安装不在主要 composer 仓库中的包,你可以在 composer.json 文件的 repositories
部分中定义一个新的包。
{ "repositories": [ { "type": "package", "package": { "name": "dojo/dojo", "version": "1.11.1", "dist": { "type": "zip", "url": "http://download.dojotoolkit.org/release-1.11.1/dojo-release-1.11.1.zip" }, "type": "library" } } ] }
然后,你可以在 ajgl-symlinks
部分中定义所需的符号链接,就像平常一样
{ "extra": { "ajgl-symlinks": { "dojo/dojo": { ".": "www/dojo" } } } }
许可证
此组件受 MIT 许可证的保护。完整的许可证请参阅 LICENSE 文件。
报告问题或功能请求
问题和功能请求在 Github 问题跟踪器 中跟踪。
作者信息
由 Antonio J. García Lagar 开发。
如果你觉得这个组件很有用,请在 GitHub 仓库页面 和/或 Packagist 包页面 上添加一个 ★。