在 Laravel 中安全地编辑第三方作曲家(供应商)包并防止在发布新版本的包时丢失自定义更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31080696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Safely edit a third party composer (vendor) package in Laravel & prevent losing customized changes on release of a new version of the package
提问by TimothyBuktu
I want to edit a package I pulled from composer in my Laravel 5 project, however i believe that if I ran composer update
and a new version of this package has been released, I will lose all of my changes. How should I go about editing the package? Is there a way to copy package out of the vendor directory so I can use it somewhere else in my project?
我想编辑我在 Laravel 5 项目中从 composer 中提取的包,但是我相信如果我运行composer update
并发布了该包的新版本,我将丢失所有更改。我应该如何编辑包?有没有办法从供应商目录中复制包,以便我可以在我的项目中的其他地方使用它?
采纳答案by whoacowboy
It actually isn't safe to edit composer packages, for the very reason you point out.
出于您指出的原因,编辑作曲家包实际上并不安全。
What I do is extends the classes that I want/need to change.
我所做的是扩展我想要/需要更改的类。
I have done it here with the Filesystem class. It doesn't ensure that it won't break, but it does let you update without overwriting your changes.
我在这里用 Filesystem 类完成了它。它不能确保它不会中断,但它可以让您在不覆盖更改的情况下进行更新。
config/app.php
配置/app.php
<?php
return [
'providers' => [
// 'Illuminate\Filesystem\FilesystemServiceProvider',
'MyApp\Filesystem\FilesystemServiceProvider',
],
'aliases' => [
...
],
];
MyApp\Filesystem\FilesystemServiceProvider.php
MyApp\Filesystem\FilesystemServiceProvider.php
<?php namespace MyApp\Filesystem;
use Config;
use Storage;
use League\Flysystem\Filesystem;
use Dropbox\Client as DropboxClient;
use League\Flysystem\Dropbox\DropboxAdapter;
use Illuminate\Filesystem\FilesystemManager as LaravelFilesystemManager;
class FilesystemManager extends LaravelFilesystemManager{
public function createDropboxDriver(array $config)
{
$client = new DropboxClient($config['token'], $config['app']);
return $this->adapt(
new Filesystem(new DropboxAdapter($client))
);
}
}
回答by Morteza Rajabi
The simple, fast and safe method:
简单、快速、安全的方法:
- Create a directory in the Laravel's root directory and name it packages or whatever you like.
- Move the modified package from vendor directory to your packages directory.
- Update composer.json to load the package from your packages directory instead of vendor directory.
- 在 Laravel 的根目录中创建一个目录并将其命名为包或任何你喜欢的名称。
- 将修改后的包从供应商目录移动到您的包目录。
- 更新 composer.json 以从您的包目录而不是供应商目录加载包。
first remove it from require
首先从要求中删除它
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"laravelcollective/html": "^5.3.0", <==== remove this line
"barryvdh/laravel-debugbar": "^2.3",
"doctrine/dbal": "^2.5"
},
and then add it to autoload
然后将其添加到自动加载
"autoload": {
"psr-4": {
"App\": "app/",
"Collective\Html\": "packages/laravelcollective/html/src", <==== add this line
},
}
Please do not forget to run
请不要忘记运行
composer dumpauto
Alternative for step 3.
步骤 3 的替代方法。
There is also a new alternative if you're using latest version of composer.
如果您使用的是最新版本的作曲家,还有一个新的选择。
Add this to you composer.json
将此添加到您的 composer.json
"repositories": [
{
"type": "path",
"url": "./packages/laravelcollective"
}
]
And then modify the version of package to dev-master
然后将包的版本修改为 dev-master
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"laravelcollective/html": "dev-master", <==== this line
"barryvdh/laravel-debugbar": "^2.3",
"doctrine/dbal": "^2.5"
},
Finally
最后
composer update
回答by Mostafa Attia
if you want to keep your changes AND update the package from the original repo at the same time, you can fork this package and point composer to pull from your fork, not the original repo.
如果您想保留更改并同时从原始存储库更新包,您可以分叉这个包并指向 composer 从您的分叉中拉取,而不是原始存储库。
All you have to do is add your fork as a repository and update the version constraint to point to your custom branch. Your custom branch name mustbe prefixed with dev-.
您所要做的就是将您的 fork 添加为存储库并更新版本约束以指向您的自定义分支。您的自定义分支名称必须以 dev- 为前缀。
update your composer.json
file as follows:
更新您的composer.json
文件如下:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/MyGithub/html"
}
],
"require": {
"laravelcollective/html": "dev-bugfix"
}
}
Note that you don't change the require statement except to specify your bugfix branch. You still reference the upstream package (laravelcollective/html), not your personal fork (MyGithub/html).
请注意,除了指定错误修复分支外,您不会更改 require 语句。您仍然引用上游包 (laravelcollective/html),而不是您的个人分支 (MyGithub/html)。
also, note that dev-
is automatically added so branch name is bugfix
not dev-bugfix
. if you named your branch as dev-bugfix
you will require it as dev-dev-bugfix
.
另外,请注意dev-
是自动添加的,因此分支名称bugfix
不是dev-bugfix
。如果你将你的分支命名为dev-bugfix
你需要它作为dev-dev-bugfix
.
回答by Adam
If you want to make changes to a class of a package you have to
如果你想改变一个包的类,你必须
Create a class that extends the package class and make your changes
Create a service provider that extends the service provider of the class and change the
registerBinding
class to bind your extended classPut that new service provider in
providers
array inconfig\app
Disable package discovery for this package, you may list the package name in the extra section of your application's composer.json file:
"extra": { "laravel": { "dont-discover": [ "barryvdh/laravel-debugbar" ] } },
创建一个扩展包类的类并进行更改
创建扩展类的服务提供者的服务提供者并更改
registerBinding
类以绑定您的扩展类把那个新的服务提供者放在
providers
数组中config\app
禁用此包的包发现,您可以在应用程序的 composer.json 文件的额外部分中列出包名称:
"extra": { "laravel": { "dont-discover": [ "barryvdh/laravel-debugbar" ] } },
回答by herry swastika
- use strict version of the vendor package, e.g. instead of "vendor/package": "~1.3", or "vendor/package": "^1.3.2", you may use "vendor/package": "1.3.2",
- copy the modified source file into public (if open source), or storage/app for private file (eg laravel)
- simply code this in Controller : File::copy('foldername/filename.php', '../vendor/namevendor/subfolderwhatever/filename.php');
- make this as deployment routine , shared to deployment colleagues
- have a coffee, have a life, done
- 使用供应商包的严格版本,例如代替 "vendor/package": "~1.3" 或 "vendor/package": "^1.3.2",你可以使用 "vendor/package": "1.3.2" ,
- 将修改后的源文件复制到公共(如果是开源)或私有文件的存储/应用程序(例如 laravel)
- 只需在 Controller 中编码: File::copy('foldername/filename.php', '../vendor/namevendor/subfolderwhatever/filename.php');
- 将此作为部署例程,共享给部署同事
- 喝杯咖啡,享受生活,完成