Completed
Push — dev ( af367c...33f3c0 )
by Marc
03:05
created

AutoPublishable::publishes()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 8.2222
cc 7
eloc 7
nc 5
nop 2
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);
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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
}