Passed
Push — master ( 9c811f...1ee935 )
by Attila
03:55
created

publishAllSubModuleMigrations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Contains the BaseBoxServiceProvider class.
4
 *
5
 * @copyright   Copyright (c) 2016 Attila Fulop
6
 * @author      Attila Fulop
7
 * @license     MIT
8
 * @since       2016-12-29
9
 *
10
 */
11
12
namespace Konekt\Concord;
13
14
abstract class BaseBoxServiceProvider extends BaseServiceProvider
15
{
16
    protected $configFileName = 'box.php';
17
18
    protected static $_kind = 'box';
19
20
    public function register()
21
    {
22
        parent::register();
23
24
        $modules = $this->config("modules");
25
        $modules = $modules ?: [];
26
27
        foreach ($modules as $module => $configuration) {
28
            if (is_int($module) && is_string($configuration)) { // means no configuration was set for module
29
                $module        = $configuration;
30
                $configuration = $this->getDefaultModuleConfiguration();
31
            } else {
32
                $configuration = array_merge($this->getDefaultModuleConfiguration(),
33
                    is_array($configuration) ? $configuration : []);
34
                $configuration = array_replace_recursive($configuration, $this->getCascadeModuleConfiguration());
35
            }
36
37
            $this->concord->registerModule($module, $configuration);
38
        }
39
    }
40
41
    public function boot()
42
    {
43
        parent::boot();
44
45
        $this->publishOwnMigrationsInASeparateGroup();
46
        $this->publishAllSubModuleMigrations();
47
    }
48
49
    /**
50
     * Returns the "cascade" configuration: the "apply to all submodules" config override array
51
     *
52
     * @return array
53
     */
54
    protected function getCascadeModuleConfiguration(): array
55
    {
56
        $result = $this->config('cascade_config', []);
57
58
        return is_array($result) ? $result : [];
59
    }
60
61
    /**
62
     * Returns the default configuration settings for modules loaded within boxes
63
     *
64
     * @return array
65
     */
66
    protected function getDefaultModuleConfiguration()
67
    {
68
        return [
69
            'implicit'   => true,
70
            'migrations' => $this->areMigrationsEnabled(),
71
            'views'      => $this->areViewsEnabled(),
72
            'routes'     => $this->areRoutesEnabled()
73
        ];
74
    }
75
76
    private function publishAllSubModuleMigrations(): void
77
    {
78
        $folder = '/' . $this->convention->migrationsFolder();
79
        $moduleMigrationPaths = [
80
            $this->getBasePath() . $folder => database_path('migrations')
81
        ];
82
        foreach ($this->config("modules", []) as $moduleClass => $config) {
83
            $module = $this->concord->module($this->getModuleId($moduleClass));
84
            $moduleMigrationPaths[$module->getBasePath() . $folder] = database_path('migrations');
85
        }
86
87
        $this->publishes($moduleMigrationPaths, 'migrations');
88
    }
89
90
    private function publishOwnMigrationsInASeparateGroup()
91
    {
92
        $this->publishes([
93
            $this->getBasePath() . '/' . $this->convention->migrationsFolder() =>
94
                database_path('migrations')
95
        ], 'own-migrations-only');
96
    }
97
}
98