0

I am developing several packages and would like to have a single config file for all of them if they are to be published.

Inside my service provider I did this:

public function boot()
{
    $this->publishes([
        __DIR__ . '/config/custom.php' => config_path('custom.php'),
    ]);
}

public function register()
{
    $this->mergeConfigFrom(
        __DIR__ . '/config/custom.php', 'custom'
    );
}

Config:

return [
    'containers' => [
        ...
    ]
];

And surely enough, if I publish it, it creates the file with values inside. But if a file already exists, having different keys:

return [
    'xxxyyy' => [
        ...
    ],
];

publishing doesn't do anything. I would expect it to look like:

return [
    'xxxyyy' => [
        ...
    ],

    'containers' => [
        ...
    ]
];

What am I doing wrong?

Norgul
  • 4,613
  • 13
  • 61
  • 144
  • If the same name of the config file existed, the `publish` command doesn't do anything. And it's also doesn't merge the config file, and only override the config file with option `--force` – bangnokia Jun 02 '20 at 15:06
  • If it doesn't do that, what is the point of whole `mergeConfigFrom` method? What does it "merge"? – Norgul Jun 02 '20 at 15:07
  • It allows user can override (merge config values from 2 files) some default config values from your package without touch the package source code. Example in file config of package set ['foo' => 'bar'], and if user want to change `['foo' => 'foobar']`, they should publish your configuration file in the laravel app to do that. – bangnokia Jun 02 '20 at 15:15
  • `vendor:publish --force` would do what you're describing without using `mergeConfigFrom` – Norgul Jun 02 '20 at 15:21
  • I mean that's function it merge config values from 2 files into 1 then load into app when bootstrapping, it doesn't merge the file content literal. And you do nothing wrong – bangnokia Jun 02 '20 at 15:27
  • Yes, and if `custom.php` already exists, it should merge published `custom.php` with one I am giving it inside of my package. Merging 2 files into 1. My files are not getting merged. – Norgul Jun 02 '20 at 15:30
  • I already said that in the first comment, that isn't how the publish command works. If you want to merge literally, you may write your own publish command. – bangnokia Jun 02 '20 at 15:38
  • Then again, I don't understand what's the purpose of `mergeConfigFrom`? Documentation https://laravel.com/docs/7.x/packages#configuration states that I can merge my config with published copy. – Norgul Jun 02 '20 at 16:13
  • They use `array_merge()` config values, not the file content. If you want know how it merge, you should read `mergeConfigFrom` function source code to see how it work :) – bangnokia Jun 02 '20 at 16:32

1 Answers1

3

In case anyone else is baffled by this, I have tested several cases and here is my explanation (official docs).

$this->publishes part enables the package config to be published by vendor:publish command. Nothing to explain here.

$this->mergeConfigFrom part enables the package config to be merged with currently published copy. Merge in this case means merged from Laravel config() scope.

This means that going to artisan tinker and running config('key') (where key is name of your config file) will return nothing if there is no published file and you don't have mergeConfigFrom. If a file is published and has other key value pairs which are not present in your package config and you have mergeConfigFrom present, config('key') will return merged array of values from a published file together with values from your config.

If a file with a same config name exists in your root config folder, and your package internally uses the same config name but you don't have mergeConfigFrom, config('key') will return only contents of a file inside root config folder, and will ignore everything from your package as you didn't provide a way for Laravel to see this as a "global" configuration. Your package will keep internally using the package config, but from app scope you will not be able to fetch it.

Norgul
  • 4,613
  • 13
  • 61
  • 144