1
|
|
|
<?php namespace Mascame\Artificer; |
2
|
|
|
use Illuminate\Support\Facades\App; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* How it works: Simply we wait until app is ready to publish whatever is in the vendor's publishable files |
6
|
|
|
* |
7
|
|
|
* Class AutoPublishable |
8
|
|
|
* @package Mascame\Artificer |
9
|
|
|
*/ |
10
|
|
|
trait AutoPublishable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var App |
14
|
|
|
*/ |
15
|
|
|
protected $app; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Should it run only in development mode? |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $onlyDevelopment = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Determines if we are missing one or more publishable directories |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $needsPublish = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Is it going to autopublish? |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $willPublish = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param null $fileToCheck |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
protected function isPublished($fileToCheck = null) { |
43
|
|
|
if (! $fileToCheck) $fileToCheck = config_path($this->name); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$isPublished = \File::exists($fileToCheck); |
46
|
|
|
|
47
|
|
|
if (! $isPublished) $this->needsPublish = true; |
48
|
|
|
|
49
|
|
|
return $isPublished; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Add files to publishable array & autopublish them in case directory does not exist |
54
|
|
|
* |
55
|
|
|
* @param array $paths |
56
|
|
|
* @param null $group |
57
|
|
|
*/ |
58
|
|
|
protected function publishes(array $paths, $group = null) { |
59
|
|
|
parent::publishes($paths, $group); |
60
|
|
|
|
61
|
|
|
if ($this->onlyDevelopment && App::environment() != 'local') return; |
62
|
|
|
|
63
|
|
|
if ($this->willPublish) return; |
64
|
|
|
|
65
|
|
|
foreach ($paths as $path) { |
66
|
|
|
if ($this->needsPublish || ! $this->isPublished($path)) { |
67
|
|
|
$this->autoPublish(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Publish vendor files when app is ready |
74
|
|
|
*/ |
75
|
|
|
protected function autoPublish() { |
76
|
|
|
if ($this->willPublish) return; |
77
|
|
|
|
78
|
|
|
$this->app->booted(function () { |
79
|
|
|
\Artisan::call('vendor:publish', ['--provider' => self::class]); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Little "hack" because we are not in a Controller so we can not use Redirect. |
83
|
|
|
* |
84
|
|
|
* If we don't refresh we won't have files ready for this request |
85
|
|
|
*/ |
86
|
|
|
header('Location: '. \URL::current()); |
87
|
|
|
die(); |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
$this->willPublish = true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: