Completed
Push — master ( 629b98...606f80 )
by Brian
11:50
created

ServiceProvider::publishConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Laravel Service Provider.
5
 *
6
 * (c) DraperStudio <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DraperStudio\ServiceProvider;
13
14
use Illuminate\Support\Facades\File;
15
use Illuminate\Contracts\Foundation\Application;
16
17
/**
18
 * Class ServiceProvider.
19
 */
20
abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider
21
{
22
    use FilesTrait;
23
24
    protected $packagePath;
25
    protected $packageName;
26
27
    /**
28
     * Bootstrap the application services.
29
     */
30
    public function boot()
31
    {
32
        //
33
    }
34
35
    /**
36
     * Register the application services.
37
     */
38
    public function register()
39
    {
40
        $this->packagePath = $this->getPackagePath();
41
        $this->packageName = $this->getPackageName();
42
43
        $this->registerAssetPublisher();
44
45
        $this->registerConfigPublisher();
46
47
        $this->registerViewPublisher();
48
49
        $this->registerMigrationPublisher();
50
51
        $this->registerSeedPublisher();
52
53
        $this->registerTranslationPublisher();
54
55
        $this->registerViewLoader();
56
57
        $this->registerRouteLoader();
58
59
        $this->registerTranslationLoader();
60
    }
61
62
    /**
63
     * Register configuration paths to be published by the publish command.
64
     */
65
    protected function publishConfig()
66
    {
67
        $this->publishes(
68
            $this->app['publisher.config']->getFileList($this->packagePath),
69
            'config'
70
        );
71
    }
72
73
    /**
74
     * Register migration paths to be published by the publish command.
75
     */
76
    protected function publishMigrations()
77
    {
78
        $this->publishes(
79
            $this->app['publisher.migrations']->getFileList($this->packagePath),
80
            'migrations'
81
        );
82
    }
83
84
    /**
85
     * Register views paths to be published by the publish command.
86
     */
87
    protected function publishViews()
88
    {
89
        $this->publishes(
90
            $this->app['publisher.views']->getFileList($this->packagePath),
91
            'views'
92
        );
93
    }
94
95
    /**
96
     * Register assets paths to be published by the publish command.
97
     */
98
    protected function publishAssets()
99
    {
100
        $this->publishes(
101
            $this->app['publisher.assets']->getFileList($this->packagePath),
102
            'assets'
103
        );
104
    }
105
106
    /**
107
     * Register seeds paths to be published by the publish command.
108
     */
109
    protected function publishSeeds()
110
    {
111
        $this->publishes(
112
            $this->app['publisher.seeds']->getFileList($this->packagePath),
113
            'seeds'
114
        );
115
    }
116
117
    /**
118
     * Register a view file namespace.
119
     */
120
    protected function loadViews()
121
    {
122
        $this->loadViewsFrom(
123
            $this->app['loader.views']->getFileList($this->packagePath),
124
            $this->packageName
125
        );
126
    }
127
128
    /**
129
     * Register a translation file namespace.
130
     */
131
    protected function loadTranslations()
132
    {
133
        $this->loadTranslationsFrom(
134
            $this->app['loader.translations']->getFileList($this->packagePath),
135
            $this->packageName
136
        );
137
    }
138
139
    /**
140
     * Register a route file namespace.
141
     */
142
    protected function loadRoutes()
143
    {
144
        if (!$this->app->routesAreCached()) {
145
            require $this->app['loader.routes']->getFileList($this->packagePath);
146
        }
147
    }
148
149
    /**
150
     * Merge the given configuration with the existing configuration.
151
     */
152
    protected function mergeConfig()
153
    {
154
        $this->mergeConfigFrom(
155
            $this->packagePath.'/resources/config/'.$this->getFileName($this->packageName),
156
            $this->packageName
157
        );
158
    }
159
160
    /**
161
     * Get the default package path.
162
     *
163
     * @return string
164
     */
165
    protected function getPackagePath()
166
    {
167
        return dirname((new \ReflectionClass($this))->getFileName()).'/..';
168
    }
169
170
    /**
171
     * Get the default package name.
172
     *
173
     * @return string
174
     */
175
    abstract protected function getPackageName();
176
177
    /**
178
     * Register the asset publisher service and command.
179
     */
180
    protected function registerAssetPublisher()
181
    {
182
        $packagePath = $this->packagePath;
183
        $packageName = $this->packageName;
184
185
        $this->app->singleton('publisher.asset', function (Application $app) use ($packagePath, $packageName) {
186
            $publicPath = $app->publicPath();
187
188
            $publisher = new Publisher\AssetPublisher($app->make('files'), $publicPath);
189
190
            $publisher->setPackagePath($packagePath);
191
            $publisher->setPackageName($packageName);
192
193
            return $publisher;
194
        });
195
    }
196
197
    /**
198
     * Register the configuration publisher class and command.
199
     */
200
    protected function registerConfigPublisher()
201
    {
202
        $packagePath = $this->packagePath;
203
        $packageName = $this->packageName;
204
205
        $this->app->singleton('publisher.config', function (Application $app) use ($packagePath, $packageName) {
206
            $path = $app->configPath();
207
208
            $publisher = new Publisher\ConfigPublisher($app->make('files'), $path);
209
210
            $publisher->setPackagePath($packagePath);
211
            $publisher->setPackageName($packageName);
212
213
            return $publisher;
214
        });
215
    }
216
217
    /**
218
     * Register the view publisher class and command.
219
     */
220
    protected function registerViewPublisher()
221
    {
222
        $packagePath = $this->packagePath;
223
        $packageName = $this->packageName;
224
225
        $this->app->singleton('publisher.views', function (Application $app) use ($packagePath, $packageName) {
226
            $viewPath = $app->basePath().'/resources/views/vendor';
227
228
            $publisher = new Publisher\ViewPublisher($app->make('files'), $viewPath);
229
230
            $publisher->setPackagePath($packagePath);
231
            $publisher->setPackageName($packageName);
232
233
            return $publisher;
234
        });
235
    }
236
237
    /**
238
     * Register the migration publisher class and command.
239
     */
240
    protected function registerMigrationPublisher()
241
    {
242
        $packagePath = $this->packagePath;
243
        $packageName = $this->packageName;
244
245
        $this->app->singleton('publisher.migrations', function (Application $app) use ($packagePath, $packageName) {
246
            $viewPath = $app->databasePath().'/migrations';
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
247
248
            $publisher = new Publisher\MigrationPublisher($app->make('files'), $viewPath);
249
250
            $publisher->setPackagePath($packagePath);
251
            $publisher->setPackageName($packageName);
252
253
            return $publisher;
254
        });
255
    }
256
257
    /**
258
     * Register the migration publisher class and command.
259
     */
260
    protected function registerSeedPublisher()
261
    {
262
        $packagePath = $this->packagePath;
263
        $packageName = $this->packageName;
264
265
        $this->app->singleton('publisher.seeds', function (Application $app) use ($packagePath, $packageName) {
266
            $viewPath = $app->databasePath().'/seeds';
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
267
268
            $publisher = new Publisher\SeedPublisher($app->make('files'), $viewPath);
269
270
            $publisher->setPackagePath($packagePath);
271
            $publisher->setPackageName($packageName);
272
273
            return $publisher;
274
        });
275
    }
276
277
    /**
278
     * Register the migration publisher class and command.
279
     */
280
    protected function registerTranslationPublisher()
281
    {
282
        $packagePath = $this->packagePath;
283
        $packageName = $this->packageName;
284
285
        $this->app->singleton('publisher.translations', function (Application $app) use ($packagePath, $packageName) {
286
            $viewPath = $app->basePath().'/resources/lang/vendor';
287
288
            $publisher = new Publisher\TranslationPublisher($app->make('files'), $viewPath);
289
290
            $publisher->setPackagePath($packagePath);
291
            $publisher->setPackageName($packageName);
292
293
            return $publisher;
294
        });
295
    }
296
297
    /**
298
     * Register the view loader class and command.
299
     */
300
    protected function registerViewLoader()
301
    {
302
        $packagePath = $this->packagePath;
303
304
        $this->app->singleton('loader.views', function (Application $app) use ($packagePath) {
305
            $publisher = new Loader\ViewLoader($app->make('files'));
306
307
            $publisher->setPackagePath($packagePath);
308
309
            return $publisher;
310
        });
311
    }
312
313
    /**
314
     * Register the view loader class and command.
315
     */
316
    protected function registerRouteLoader()
317
    {
318
        $packagePath = $this->packagePath;
319
320
        $this->app->singleton('loader.routes', function (Application $app) use ($packagePath) {
321
            $publisher = new Loader\RouteLoader($app->make('files'));
322
323
            $publisher->setPackagePath($packagePath);
324
325
            return $publisher;
326
        });
327
    }
328
329
    /**
330
     * Register the view loader class and command.
331
     */
332
    protected function registerTranslationLoader()
333
    {
334
        $packagePath = $this->packagePath;
335
336
        $this->app->singleton('loader.translations', function (Application $app) use ($packagePath) {
337
            $publisher = new Loader\TranslationLoader($app->make('files'));
338
339
            $publisher->setPackagePath($packagePath);
340
341
            return $publisher;
342
        });
343
    }
344
}
345