Completed
Pull Request — master (#90)
by Marcin
05:35 queued 02:13
created

ResponseBuilderServiceProvider::mergeConfig()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 17
rs 9.6111
1
<?php
2
/**
3
 * Disable return type hint inspection as we do not have it specified in that
4
 * class for a purpose. The base class is also not having return type hints.
5
 *
6
 * @noinspection ReturnTypeCanBeDeclaredInspection
7
 */
8
9
declare(strict_types=1);
10
11
namespace MarcinOrlowski\ResponseBuilder;
12
13
/**
14
 * Laravel API Response Builder
15
 *
16
 * @package   MarcinOrlowski\ResponseBuilder
17
 *
18
 * @author    Marcin Orlowski <mail (#) marcinorlowski (.) com>
19
 * @copyright 2016-2019 Marcin Orlowski
20
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
21
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
22
 */
23
24
use Illuminate\Support\ServiceProvider;
25
26
class ResponseBuilderServiceProvider extends ServiceProvider
27
{
28
	/**
29
	 * Register bindings in the container.
30
	 *
31
	 * @return void
32
	 */
33
	public function register()
34
	{
35
		$this->mergeConfigFrom(
36
			__DIR__ . '/../config/response_builder.php', 'response_builder'
37
		);
38
	}
39
40
	/**
41
	 * Sets up package resources
42
	 *
43
	 * @return void
44
	 */
45
	public function boot()
46
	{
47
		$this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder');
48
49
		$source = __DIR__ . '/../config/response_builder.php';
50
		$this->publishes([
51
			$source => config_path('response_builder.php'),
52
		]);
53
	}
54
55
	/** *******************************************************************************************
56
	 * Support for multi-dimensional config array. Built-in config merge only supports flat arrays.
57
	 *
58
	 */
59
60
	/**
61
	 * Merge the given configuration with the existing configuration.
62
	 *
63
	 * @param string $path
64
	 * @param string $key
65
	 *
66
	 * @return void
67
	 */
68
	protected function mergeConfigFrom($path, $key)
69
	{
70
		$config = $this->app['config']->get($key, []);
71
		$this->app['config']->set($key, $this->mergeConfig(require $path, $config));
72
	}
73
74
	/**
75
	 * Merges the configs together and takes multi-dimensional arrays into account.
76
	 *
77
	 * @param array $original
78
	 * @param array $merging
79
	 *
80
	 * @return array
81
	 */
82
	protected function mergeConfig(array $original, array $merging)
83
	{
84
		$array = array_merge($original, $merging);
85
		foreach ($original as $key => $value) {
86
			if (!is_array($value)) {
87
				continue;
88
			}
89
			if (!Arr::exists($merging, $key)) {
0 ignored issues
show
Bug introduced by
The type MarcinOrlowski\ResponseBuilder\Arr was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
90
				continue;
91
			}
92
			if (is_numeric($key)) {
93
				continue;
94
			}
95
			$array[ $key ] = $this->mergeConfig($value, $merging[ $key ]);
96
		}
97
98
		return $array;
99
	}
100
101
}
102