Home > Software design >  Is there a way to use one Vendor for multiple projects with different seeder
Is there a way to use one Vendor for multiple projects with different seeder

Time:06-21

i was having issues on how vendor take too much place. Since my host has a limit on number of files and folders (INODES). So i found a solution to use one Vendor for multiple project in laravel through the following link : Using one Vendor Folder for Multiple Projects in Laravel 5.2.

In the following lines I put the vendor in projectA, then link the others projects to it.

In projectA everything works well when i do php artisan migrate:fresh --seed When i try the same thing to projectB for example, the above command works till it arrives where the seeders should be executed. There, some error occur due to the fact that the command is trying to launch a seeder of projectA in the projectB as shown in the following screenshot.

Seeder Error Screenshot

So i want to know if there's a way to make the seeder separately.

I tried to create a symlink on the composer.json file as it was done for the vendor folder, but it doesn't work.

I'm using Laravel 8

CodePudding user response:

I thínk this is a terrible idea, except in the case that both projects share exact same codebase.

Composer use composer.json as you know. How does your B project composer file determines how to resolve PSR4 autoload entries in Laravel, If it is symlinked to A project? That is probably the reason why your seed command is trying to locate Classes in A project.

Composer is clever enough to cache downloaded packages and reuse them, but I think that every project has its own dependencies and state, which is maintained by composer.json in the first case and composer.lock on the state case.

What happen if you update composer in A but not in B, will B work?

And last, composer autoload file reference all satisfied dependencies in your project, and in this case that (unique) autoload file will be loaded in both projects but what happen if your required packages are not exactly the same? ie You have Laravel Debug Bar in one project but not in the other. The autoload generated file will reference that package that will not exist on your other project.

Is not this the way composer work? Am I wrong?

CodePudding user response:

In Composer the Vendor Directory (vendor in the project tree by default) is per project.

You ask about how to use one vendor folder for different projects.

Now first of all, this is absolutely not what Composer expects nor how it works. See Manuel Glez answer. In short a terrible idea.

When it comes to Composer, the place to share the actual PHP code across projects is not in the vendor directory but in repositories.

And as long as the dependencies are compatible, you could make one project depend on another and use its vendor/<vendor>/<name> folders as repositories. The remarks in Manuel Glez answer are still the same, this need to be same compatible versions across the board.

But to give the example, see Composer Path Repository which has this layout:

...
├── apps
│   └── my-app
│       └── composer.json
├── packages
│   └── my-package
│       └── composer.json
...
{
    "repositories": [
        {
            "type": "path",
            "url": "../../packages/my-package"
        }
    ]
}

It can be adopted for each ../../project/A/vendor/<pkg-vendor>/<pkg-name> in ../../project/B/composer.json so that the vendor folder in project/A can act as a path repository for project/B.

As dependencies composer.json files normally do not contain the version, the documented remarks about repositories.options.versions apply:

When the version cannot be inferred from the local VCS repository, or when you want to override the version, you can use the versions option when declaring the repository:

{
    "repositories": [
        {
            "type": "path",
            "url": "../../packages/my-package",
            "options": {
                "versions": {
                    "my/package": "4.2-dev"
                }
            }
        }
    ]
}

To prevent the duplication of the files the default strategy for Composer is to symlink the package directories. Ensure it works, then you only have one symbolic link per dependency in project B.

Okay how cool is that? Well IMHO while you still give up much of what Composer can do for you for dependency management, this at least makes use of local Composer repositories which I'd recommend for sharing instead of completely symlinking the overall vendor folder. Each project still have its own vendor/composer setup and overall what is done is much more well defined and in line with Composer itself.

Whether this works or not depends on the individual case. Key point here is as these local repositories only provide a single version per each package, you can only have that one. So these versions must all be version compatible on API level.

The system where it runs needs to support (relative) symbolic links, this should be commonly available for the situation described.

You could then automate the production of the repositories configuration and adopt it to the file-system layout. You could even generate the repositories and update them in the global configuration file so that each project would automatically prefer those packages from local.

$ echo "$(composer config --global home)/config.json"
/home/user/.config/composer/config.json

Take care all projects and their dependencies have a portable path-profile and then I'd say this should be quite straight forward shell processing.

To obtain the actual versions of the dependencies installed check per each vendor folder inside vendor/composer/*installed* files.

$ (echo "PACKAGE VERSION"; find .. -type f -path '*/vendor/composer/installed.json' -exec jq -r '.packages[] | .name   " "   .version_normalized ' {} \; | sort -u | sort -k 1b,2V) | cols
PACKAGE                                        VERSION
composer/ca-bundle                             1.3.2.0
composer/composer                              2.3.7.0
composer/metadata-minifier                     1.0.0.0
composer/pcre                                  3.0.0.0
composer/semver                                3.3.2.0
composer/spdx-licenses                         1.5.7.0
composer/xdebug-handler                        3.0.3.0
...
phar-io/manifest                               1.0.1.0
phar-io/manifest                               1.0.3.0
phar-io/manifest                               2.0.1.0
phar-io/manifest                               2.0.3.0
...

(very old installations don't have the packages keyword, you'll likely want to filter)

Finally you may want to have something to smoke-test the setup easily so that you can have guards against the dependency incompatibility problems when you take notice of them.

  • Related