Completed
Push — master ( eadd48...bbe6d6 )
by ARCANEDEV
9s
created

PackageServiceProvider::loadViews()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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       = '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 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
     * Publish the config file.
153
     */
154
    protected function publishConfig()
155
    {
156
        $this->publishes([
157
            $this->getConfigFile() => config_path("{$this->package}.php"),
158
        ], 'config');
159
    }
160
161
    /**
162
     * Publish the migration files.
163
     */
164
    protected function publishMigrations()
165
    {
166
        $this->publishes([
167
            $this->getBasePath() . '/database/migrations/' => database_path('migrations'),
168
        ], 'migrations');
169
    }
170
171
    /**
172
     * Publish and load the views if $load argument is true.
173
     *
174
     * @param  bool  $load
175
     */
176
    protected function publishViews($load = true)
177
    {
178
        $this->publishes([
179
            $this->getBasePath() . '/resources/views' => base_path("resources/views/vendor/{$this->package}"),
180
        ], 'views');
181
182
        if ($load) $this->loadViews();
183
    }
184
185
    /**
186
     * Publish and load the translations if $load argument is true.
187
     *
188
     * @param  bool  $load
189
     */
190
    protected function publishTranslations($load = true)
191
    {
192
        $this->publishes([
193
            $this->getBasePath() . '/resources/lang' => base_path("resources/lang/vendor/{$this->package}"),
194
        ], 'lang');
195
196
        if ($load) $this->loadTranslations();
197
    }
198
199
    /**
200
     * Publish all the package files.
201
     *
202
     * @param  bool  $load
203
     */
204
    protected function publishAll($load = true)
205
    {
206
        $this->publishConfig();
207
        $this->publishMigrations();
208
        $this->publishViews($load);
209
        $this->publishTranslations($load);
210
    }
211
212
    /**
213
     * Load the views files.
214
     */
215
    protected function loadViews()
216
    {
217
        $this->loadViewsFrom($this->getBasePath() . '/resources/views', $this->package);
218
    }
219
220
    /**
221
     * Load the translations files.
222
     */
223
    protected function loadTranslations()
224
    {
225
        $this->loadTranslationsFrom($this->getBasePath() . '/resources/lang', $this->package);
226
    }
227
228
    /* ------------------------------------------------------------------------------------------------
229
     |  Check Functions
230
     | ------------------------------------------------------------------------------------------------
231
     */
232
    /**
233
     * Check package name
234
     *
235
     * @throws PackageException
236
     */
237 24
    private function checkPackageName()
238
    {
239 24
        if (empty($this->package) || empty($this->package)) {
240 8
            throw new PackageException('You must specify the vendor/package name.');
241
        }
242 24
    }
243
244
    /**
245
     * Check if has the base config.
246
     *
247
     * @return bool
248
     */
249
    protected function hasPackageConfig()
250
    {
251
        return $this->getConfigFile() !== false;
252
    }
253
254
    /* ------------------------------------------------------------------------------------------------
255
     |  Other Functions
256
     | ------------------------------------------------------------------------------------------------
257
     */
258
    /**
259
     * Setup paths.
260
     *
261
     * @return self
262
     */
263 24
    private function setupPaths()
264
    {
265 24
        $this->basePath = $this->getBasePath();
266 24
        $this->paths    = [
267
            'config'    => [
268 24
                'src'       => $this->getSourcePath('config'),
269 24
                'dest'      => config_path('%s'),
270 18
            ],
271
            'migrations' => [
272 24
                'src'       => $this->getSourcePath('database/migrations'),
273 24
                'dest'      => database_path('migrations/%s_%s'),
274 18
            ],
275
            'views'     => [
276 24
                'src'       => $this->getSourcePath('resources/views'),
277 24
                'dest'      => base_path('resources/views/' . $this->vendor . '/%s'),
278 18
            ],
279
            'trans'     => [
280 24
                'src'       => $this->getSourcePath('resources/lang'),
281 24
                'dest'      => base_path('resources/lang/%s'),
282 18
            ],
283
            'assets'    => [
284 24
                'src'       => $this->getSourcePath('resources/assets'),
285 24
                'dest'      => public_path('vendor/%s'),
286 18
            ],
287
            'seeds'     => [
288 24
                'src'       => $this->getSourcePath('src/Seeds'),
289 24
                'dest'      => database_path('seeds/%s'),
290 18
            ],
291
        ];
292
293 24
        return $this;
294
    }
295
296
    /**
297
     * Get source path.
298
     *
299
     * @param  string  $path
300
     *
301
     * @return string
302
     */
303 24
    private function getSourcePath($path)
304
    {
305 24
        return str_replace('/', DS, $this->basePath . DS . $path);
306
    }
307
}
308