Completed
Push — master ( daa8c5...9dfe98 )
by ARCANEDEV
9s
created

PackageServiceProvider::publishFactories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 1
cp 0
crap 6
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
abstract class PackageServiceProvider extends ServiceProvider
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Vendor name.
19
     *
20
     * @var string
21
     */
22
    protected $vendor       = 'arcanedev';
23
24
    /**
25
     * Package name.
26
     *
27
     * @var string
28
     */
29
    protected $package      = '';
30
31
    /**
32
     * Package base path.
33
     *
34
     * @var string
35
     */
36
    protected $basePath     = '';
37
38
    /**
39
     * Merge multiple config files into one instance (package name as root key)
40
     *
41
     * @var bool
42
     */
43
    protected $multiConfigs = false;
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Getters & Setters
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * Get the base path of the package.
51
     *
52
     * @return string
53
     */
54
    abstract public function getBasePath();
55
56
    /**
57
     * Get config folder.
58
     *
59
     * @return string
60
     */
61
    protected function getConfigFolder()
62
    {
63
        return realpath($this->getBasePath() . DS .'config');
64
    }
65
66
    /**
67
     * Get config key.
68
     *
69
     * @return string
70 24
     */
71
    protected function getConfigKey()
72 24
    {
73
        return str_slug($this->package);
74
    }
75
76
    /**
77
     * Get config file path.
78
     *
79
     * @return string
80 24
     */
81
    protected function getConfigFile()
82 24
    {
83
        return $this->getConfigFolder() . DS . "{$this->package}.php";
84
    }
85
86
    /* ------------------------------------------------------------------------------------------------
87
     |  Main Functions
88
     | ------------------------------------------------------------------------------------------------
89
     */
90 24
    /**
91
     * Boot the service provider.
92 24
     */
93
    public function boot()
94
    {
95
        parent::boot();
96
97
        $this->checkPackageName();
98
    }
99
100
    /* ------------------------------------------------------------------------------------------------
101
     |  Package Functions
102
     | ------------------------------------------------------------------------------------------------
103
     */
104
    /**
105
     * Setup package path and stuff.
106
     */
107
    protected function setup()
108
    {
109
        $this->checkPackageName();
110
        $this->registerConfig();
111
    }
112
113
    /**
114
     * Register configs.
115
     *
116 24
     * @param  string  $separator
117
     */
118 24
    protected function registerConfig($separator = '.')
119 24
    {
120 24
        if ($this->multiConfigs)
121 24
            $this->registerMultipleConfigs($separator);
122
        else
123
            $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
124
    }
125
126
    /**
127
     * Register all package configs.
128 24
     *
129
     * @param  string  $separator
130 24
     */
131 18
    private function registerMultipleConfigs($separator = '.')
132
    {
133 24
        foreach (glob($this->getConfigFolder() . '/*.php') as $configPath) {
134 24
            $this->mergeConfigFrom(
135
                $configPath,
136
                $this->getConfigKey() . $separator . basename($configPath, '.php')
137
            );
138
        }
139
    }
140
141
    /**
142
     * Register commands service provider.
143
     *
144
     * @param  \Illuminate\Support\ServiceProvider|string  $provider
145
     */
146
    protected function registerCommands($provider)
147
    {
148
        if ($this->app->runningInConsole())
149
            $this->app->register($provider);
150
    }
151
152
    /**
153
     * Publish the config file.
154
     */
155
    protected function publishConfig()
156
    {
157
        $this->publishes([
158
            $this->getConfigFile() => config_path("{$this->package}.php"),
159
        ], 'config');
160
    }
161
162
    /**
163
     * Publish the migration files.
164
     */
165
    protected function publishMigrations()
166
    {
167
        if (is_dir($path = ($this->getBasePath() . '/database/migrations/'))) {
168
            $this->publishes([
169
                $path => database_path('migrations'),
170
            ], 'migrations');
171
        }
172
    }
173
174
    /**
175
     * Publish and load the views if $load argument is true.
176
     *
177
     * @param  bool  $load
178
     */
179
    protected function publishViews($load = true)
180
    {
181
        if (is_dir($path = ($this->getBasePath() . '/resources/views'))) {
182
            $this->publishes([
183
                $path => base_path("resources/views/vendor/{$this->package}"),
184
            ], 'views');
185
186
            if ($load) $this->loadViews();
187
        }
188
    }
189
190
    /**
191
     * Publish and load the translations if $load argument is true.
192
     *
193
     * @param  bool  $load
194
     */
195
    protected function publishTranslations($load = true)
196
    {
197
        if (is_dir($path = ($this->getBasePath() . '/resources/lang'))) {
198
            $this->publishes([
199
                $path => base_path("resources/lang/vendor/{$this->package}"),
200
            ], 'lang');
201
202
            if ($load) $this->loadTranslations();
203
        }
204
    }
205
206
    /**
207
     * Publish the factories.
208
     */
209
    protected function publishFactories()
210
    {
211
        if (is_dir($path = ($this->getBasePath() . '/database/factories'))) {
212
            $this->publishes([
213
                $path => database_path('factories'),
214
            ], 'factories');
215
        }
216
    }
217
218
    /**
219
     * Publish all the package files.
220
     *
221
     * @param  bool  $load
222
     */
223
    protected function publishAll($load = true)
224
    {
225
        $this->publishConfig();
226
        $this->publishMigrations();
227
        $this->publishViews($load);
228
        $this->publishTranslations($load);
229
        $this->publishFactories();
230
    }
231
232
    /**
233
     * Load the views files.
234
     */
235
    protected function loadViews()
236
    {
237
        $this->loadViewsFrom($this->getBasePath() . '/resources/views', $this->package);
238
    }
239
240
    /**
241
     * Load the translations files.
242
     */
243
    protected function loadTranslations()
244
    {
245
        $this->loadTranslationsFrom($this->getBasePath() . '/resources/lang', $this->package);
246
    }
247
248 24
    /* ------------------------------------------------------------------------------------------------
249
     |  Check Functions
250 24
     | ------------------------------------------------------------------------------------------------
251 8
     */
252
    /**
253 24
     * Check package name.
254
     *
255
     * @throws PackageException
256
     */
257
    private function checkPackageName()
258
    {
259
        if (empty($this->package) || empty($this->package)) {
260
            throw new PackageException('You must specify the vendor/package name.');
261
        }
262
    }
263
264
    /**
265
     * Check if has the base config.
266
     *
267
     * @return bool
268
     */
269
    protected function hasPackageConfig()
270
    {
271
        return $this->getConfigFile() !== false;
272
    }
273
}
274