Completed
Push — master ( e43114...eadd48 )
by ARCANEDEV
06:13
created

PackageServiceProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.27%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 14
c 7
b 0
f 0
lcom 1
cbo 2
dl 0
loc 222
ccs 45
cts 59
cp 0.7627
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigFolder() 0 4 1
getBasePath() 0 1 ?
A getConfigKey() 0 4 1
A getConfigFile() 0 4 1
A hasPackageConfig() 0 4 1
A boot() 0 6 1
A setup() 0 6 1
A registerConfig() 0 10 2
A registerMultipleConfigs() 0 8 2
A checkPackageName() 0 8 2
B setupPaths() 0 32 1
A getSourcePath() 0 4 1
1
<?php namespace Arcanedev\Support;
2
3
use Arcanedev\Support\Exceptions\PackageException;
4
5
/**
6
 * Class     PackageServiceProvider
7
 *
8
 * @package  Arcanedev\Support\Laravel
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @todo     Redo the PackageServiceProvider
12
 */
13
abstract class PackageServiceProvider extends ServiceProvider
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Vendor name.
21
     *
22
     * @var string
23
     */
24
    protected $vendor       = 'vendor';
25
26
    /**
27
     * Package name.
28
     *
29
     * @var string
30
     */
31
    protected $package      = '';
32
33
    /**
34
     * Package base path.
35
     *
36
     * @var string
37
     */
38
    protected $basePath     = '';
39
40
    /**
41
     * Paths collection.
42
     *
43
     * @var array
44
     */
45
    protected $paths        = [];
46
47
    /**
48
     * Merge multiple config files into one instance (package name as root key)
49
     *
50
     * @var bool
51
     */
52
    protected $multiConfigs = false;
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  Getters & Setters
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * Get config folder.
60
     *
61
     * @return string
62
     */
63 24
    protected function getConfigFolder()
64 9
    {
65 24
        return realpath($this->getBasePath() . DS .'config');
66 9
    }
67
68
    /**
69
     * Get the base path of the package.
70
     *
71
     * @return string
72
     */
73
    abstract public function getBasePath();
74
75
    /**
76
     * Get config key.
77
     *
78
     * @return string
79
     */
80 24
    protected function getConfigKey()
81
    {
82 24
        return str_slug($this->package);
83
    }
84
85
    /**
86
     * Get config file path
87
     *
88
     * @return string
89
     */
90 24
    protected function getConfigFile()
91
    {
92 24
        return $this->getConfigFolder() . DS . $this->package . '.php';
93
    }
94
95
    /* ------------------------------------------------------------------------------------------------
96
     |  Main Functions
97
     | ------------------------------------------------------------------------------------------------
98
     */
99
    /**
100
     * Boot the service provider.
101
     */
102
    public function boot()
103
    {
104
        parent::boot();
105
106
        $this->checkPackageName();
107
    }
108
109
    /* ------------------------------------------------------------------------------------------------
110
     |  Package Functions
111
     | ------------------------------------------------------------------------------------------------
112
     */
113
    /**
114
     * Setup package path and stuff.
115
     */
116 24
    protected function setup()
117
    {
118 24
        $this->checkPackageName();
119 24
        $this->setupPaths();
120 24
        $this->registerConfig();
121 24
    }
122
123
    /**
124
     * Register configs.
125
     *
126
     * @param  string  $separator
127
     */
128 24
    protected function registerConfig($separator = '.')
129
    {
130 24
        if ($this->multiConfigs) {
131
            $this->registerMultipleConfigs($separator);
132
133
            return;
134
        }
135
136 24
        $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
137 24
    }
138
139
    /**
140
     * Register all package configs.
141
     *
142
     * @param  string  $separator
143
     */
144
    private function registerMultipleConfigs($separator = '.')
145
    {
146
        foreach (glob($this->getConfigFolder() . '/*.php') as $configPath) {
147
            $key = $this->getConfigKey() . $separator . basename($configPath, '.php');
148
149
            $this->mergeConfigFrom($configPath, $key);
150
        }
151
    }
152
153
    /* ------------------------------------------------------------------------------------------------
154
     |  Check Functions
155
     | ------------------------------------------------------------------------------------------------
156
     */
157
    /**
158
     * Check package name
159
     *
160
     * @throws PackageException
161
     */
162 24
    private function checkPackageName()
163
    {
164 24
        if ( ! empty($this->package) ) {
165 24
            return;
166
        }
167
168 8
        throw new PackageException('You must specify the name of the package');
169
    }
170
171
    /**
172
     * Check if has the base config.
173
     *
174
     * @return bool
175
     */
176
    protected function hasPackageConfig()
177
    {
178
        return $this->getConfigFile() !== false;
179
    }
180
181
    /* ------------------------------------------------------------------------------------------------
182
     |  Other Functions
183
     | ------------------------------------------------------------------------------------------------
184
     */
185
    /**
186
     * Setup paths.
187
     *
188
     * @return self
189
     */
190 24
    private function setupPaths()
191
    {
192 24
        $this->basePath = $this->getBasePath();
193 24
        $this->paths    = [
194
            'config'    => [
195 24
                'src'       => $this->getSourcePath('config'),
196 24
                'dest'      => config_path('%s'),
197 18
            ],
198
            'migrations' => [
199 24
                'src'       => $this->getSourcePath('database/migrations'),
200 24
                'dest'      => database_path('migrations/%s_%s'),
201 18
            ],
202
            'views'     => [
203 24
                'src'       => $this->getSourcePath('resources/views'),
204 24
                'dest'      => base_path('resources/views/' . $this->vendor . '/%s'),
205 18
            ],
206
            'trans'     => [
207 24
                'src'       => $this->getSourcePath('resources/lang'),
208 24
                'dest'      => base_path('resources/lang/%s'),
209 18
            ],
210
            'assets'    => [
211 24
                'src'       => $this->getSourcePath('resources/assets'),
212 24
                'dest'      => public_path('vendor/%s'),
213 18
            ],
214
            'seeds'     => [
215 24
                'src'       => $this->getSourcePath('src/Seeds'),
216 24
                'dest'      => database_path('seeds/%s'),
217 18
            ],
218
        ];
219
220 24
        return $this;
221
    }
222
223
    /**
224
     * Get source path.
225
     *
226
     * @param  string  $path
227
     *
228
     * @return string
229
     */
230 24
    private function getSourcePath($path)
231
    {
232 24
        return str_replace('/', DS, $this->basePath . DS . $path);
233
    }
234
}
235