Safely edit 3rd party composer (vendor) package in Laravel and prevent losing custom changes when a new version of the package is released

I want to edit a package that I pulled from composer in my Laravel 5 project, however, I believe that if I run composer update

and a new version of this package is released, I will lose all my changes. How do I need to edit a package? Is there a way to copy a package from the vendor directory so I can use it somewhere else in my project?

+7


source to share


5 answers


It is actually unsafe to edit composer packages for the reason you specify.

What I am doing is extending the classes I want / need to change.

I did it here with the filesystem class. It doesn't guarantee it won't break, but it allows you to update without overwriting your changes.

config / app.php



<?php

return [

    'providers' => [

//        'Illuminate\Filesystem\FilesystemServiceProvider',
        'MyApp\Filesystem\FilesystemServiceProvider',
    ],

    'aliases' => [
        ...
    ],

];

      

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))
        );
    }
}

      

+3


source


Simple, fast and safe method:

  1. Create a directory in your Laravel root directory and name it packages or whatever you like.
  2. Move the modified package from the vendor directory to your package directory.
  3. Update composer.json to download the package from the packages directory instead of the vendor directory.

first remove it from the request

"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 startup

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Collective\\Html\\": "packages/laravelcollective/html/src", <==== add this line
    },
}

      

Please don't forget to run

composer dumpauto

      



Alternative for step 3.

There is also a new alternative if you are using the latest version of composer.

Add this to you composer.json

"repositories": [
    {
        "type": "path",
        "url": "./packages/laravelcollective"
    }
]

      

And then change the package version to 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

      

+13


source


If you want to make changes to the package class, you must

  1. Create a class that extends the package class and make your changes

  2. Create a service provider that extends the service provider class and change the class registerBinding

    to bind your extended class

  3. Place this new service provider in an array providers

    atconfig\app

  4. Disable package detection for this package, you can specify the package name in an additional section of the composer.json application file:

    "extra": {
        "laravel": {
            "dont-discover": [
                "barryvdh/laravel-debugbar"
            ]
        }
    }, 
    
          

+1


source


if you want to keep your changes and update a package from the original repo at the same time, you can fork that package and tell the linker to checkout from your fork and not the original repo.

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 must be prefixed with dev-.

update the file composer.json

like this:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/MyGithub/html"
        }
    ],
    "require": {
        "laravelcollective/html": "dev-bugfix"
    }
}

      

Note that you are not changing the require statement, except to indicate your bug fix branch. You are still linking to the original package (laravelcollective / html) and not your personal fork (MyGithub / html).

Also note that it is dev-

added automatically, so the branch name is bugfix

not dev-bugfix

. if you name your branch as dev-bugfix

required dev-dev-bugfix

.

+1


source


  1. use the strict version of the vendor package, for example instead of "vendor / package": "~ 1.3" or "vendor / package": "^ 1.3.2", you can use "vendor / package": "1.3.2",
  2. copy the modified source file to open (if open) or to repository / app for a personal file (like laravel)
  3. just code it in Controller: File :: copy ('foldername / filename.php', '../vendor/namevendor/subfolderwhwhat/filename.php');
  4. do it as a deployment routine provided to fellow deployers
  5. drink coffee, have a life, done
-3


source







All Articles