Completed
Pull Request — master (#3)
by ARCANEDEV
04:06
created

PackageServiceProvider::getConfigFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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     Clean/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       = 'arcanedev';
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 the base path of the package.
60
     *
61
     * @return string
62
     */
63
    abstract public function getBasePath();
64
65
    /**
66
     * Get config folder.
67
     *
68
     * @return string
69
     */
70 24
    protected function getConfigFolder()
71
    {
72 24
        return realpath($this->getBasePath() . DS .'config');
73
    }
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 18
            $this->registerMultipleConfigs($separator);
132
        else
133 24
            $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
134 24
    }
135
136
    /**
137
     * Register all package configs.
138
     *
139
     * @param  string  $separator
140
     */
141
    private function registerMultipleConfigs($separator = '.')
142
    {
143
        foreach (glob($this->getConfigFolder() . '/*.php') as $configPath) {
144
            $this->mergeConfigFrom(
145
                $configPath,
146
                $this->getConfigKey() . $separator . basename($configPath, '.php')
147
            );
148
        }
149
    }
150
151
    /**
152
     * Register commands service provider.
153
     *
154
     * @param  \Illuminate\Support\ServiceProvider|string  $provider
155
     */
156
    protected function registerCommands($provider)
157
    {
158
        if ($this->app->runningInConsole())
159
            $this->app->register($provider);
160
    }
161
162
    /**
163
     * Publish the config file.
164
     */
165
    protected function publishConfig()
166
    {
167
        $this->publishes([
168
            $this->getConfigFile() => config_path("{$this->package}.php"),
169
        ], 'config');
170
    }
171
172
    /**
173
     * Publish the migration files.
174
     */
175
    protected function publishMigrations()
176
    {
177
        $this->publishes([
178
            $this->getBasePath() . '/database/migrations/' => database_path('migrations'),
179
        ], 'migrations');
180
    }
181
182
    /**
183
     * Publish and load the views if $load argument is true.
184
     *
185
     * @param  bool  $load
186
     */
187
    protected function publishViews($load = true)
188
    {
189
        $this->publishes([
190
            $this->getBasePath() . '/resources/views' => base_path("resources/views/vendor/{$this->package}"),
191
        ], 'views');
192
193
        if ($load) $this->loadViews();
194
    }
195
196
    /**
197
     * Publish and load the translations if $load argument is true.
198
     *
199
     * @param  bool  $load
200
     */
201
    protected function publishTranslations($load = true)
202
    {
203
        $this->publishes([
204
            $this->getBasePath() . '/resources/lang' => base_path("resources/lang/vendor/{$this->package}"),
205
        ], 'lang');
206
207
        if ($load) $this->loadTranslations();
208
    }
209
210
    /**
211
     * Publish all the package files.
212
     *
213
     * @param  bool  $load
214
     */
215
    protected function publishAll($load = true)
216
    {
217
        $this->publishConfig();
218
        $this->publishMigrations();
219
        $this->publishViews($load);
220
        $this->publishTranslations($load);
221
    }
222
223
    /**
224
     * Load the views files.
225
     */
226
    protected function loadViews()
227
    {
228
        $this->loadViewsFrom($this->getBasePath() . '/resources/views', $this->package);
229
    }
230
231
    /**
232
     * Load the translations files.
233
     */
234
    protected function loadTranslations()
235
    {
236
        $this->loadTranslationsFrom($this->getBasePath() . '/resources/lang', $this->package);
237
    }
238
239
    /* ------------------------------------------------------------------------------------------------
240
     |  Check Functions
241
     | ------------------------------------------------------------------------------------------------
242
     */
243
    /**
244
     * Check package name
245
     *
246
     * @throws PackageException
247
     */
248 24
    private function checkPackageName()
249
    {
250 24
        if (empty($this->package) || empty($this->package)) {
251 8
            throw new PackageException('You must specify the vendor/package name.');
252
        }
253 24
    }
254
255
    /**
256
     * Check if has the base config.
257
     *
258
     * @return bool
259
     */
260
    protected function hasPackageConfig()
261
    {
262
        return $this->getConfigFile() !== false;
263
    }
264
265
    /* ------------------------------------------------------------------------------------------------
266
     |  Other Functions
267
     | ------------------------------------------------------------------------------------------------
268
     */
269
    /**
270
     * Setup paths.
271
     *
272
     * @return self
273
     */
274 24
    private function setupPaths()
275
    {
276 24
        $this->basePath = $this->getBasePath();
277 24
        $this->paths    = [
278
            'config'    => [
279 24
                'src'       => $this->getSourcePath('config'),
280 24
                'dest'      => config_path('%s'),
281 18
            ],
282
            'migrations' => [
283 24
                'src'       => $this->getSourcePath('database/migrations'),
284 24
                'dest'      => database_path('migrations/%s_%s'),
285 18
            ],
286
            'views'     => [
287 24
                'src'       => $this->getSourcePath('resources/views'),
288 24
                'dest'      => base_path('resources/views/' . $this->vendor . '/%s'),
289 18
            ],
290
            'trans'     => [
291 24
                'src'       => $this->getSourcePath('resources/lang'),
292 24
                'dest'      => base_path('resources/lang/%s'),
293 18
            ],
294
            'assets'    => [
295 24
                'src'       => $this->getSourcePath('resources/assets'),
296 24
                'dest'      => public_path('vendor/%s'),
297 18
            ],
298
            'seeds'     => [
299 24
                'src'       => $this->getSourcePath('src/Seeds'),
300 24
                'dest'      => database_path('seeds/%s'),
301 18
            ],
302
        ];
303
304 24
        return $this;
305
    }
306
307
    /**
308
     * Get source path.
309
     *
310
     * @param  string  $path
311
     *
312
     * @return string
313
     */
314 24
    private function getSourcePath($path)
315
    {
316 24
        return str_replace('/', DS, $this->basePath . DS . $path);
317
    }
318
}
319