Completed
Pull Request — master (#53)
by
unknown
01:32
created

PublishCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Wn\Generators\Commands;
2
3
use Illuminate\Console\Command;
4
use Illuminate\Filesystem\Filesystem;
5
use Illuminate\Support\ServiceProvider;
6
7
class PublishCommand extends Command {
8
9
	protected $signature = 'wn:publish
10
        {--f : force the publishing.}
11
    ';
12
	protected $description = 'Publishes the laravel-generators config file to the config folder of your project';
13
    protected $fs;
14
15
    public function __construct(Filesystem $fs)
16
    {
17
        parent::__construct();
18
19
        $this->fs = $fs;
20
    }
21
    
22
    public function handle()
23
    {
24
        $paths = ServiceProvider::pathsToPublish('Wn\Generators\CommandsServiceProvider');
25
        
26
        foreach($paths as $from => $to){
27
            $shortFrom = $this->shortPath($from);
28
            
29
            if($this->copyFile($from, $to)) {
30
                $this->info($shortFrom . ' has succesfully been copied to ' . $this->shortPath($to));
31
            }else{
32
                $this->warn($shortFrom . ' could not be copied. To try and force use the --f option.');
33
            }
34
        }
35
    }
36
    
37
    private function shortPath($path){
38
        return str_replace(base_path(), '', realpath($path));
39
    }
40
    
41
    private function copyFile($from, $to){
42
        if ($this->fs->exists($to) && !$this->option('f')) {
43
            return false;
44
        }
45
        
46
        $dir = dirname($to);
47
48
        if (!$this->fs->isDirectory($dir)) {
49
            $this->fs->makeDirectory($dir, 0755, true);
50
        }
51
52
        return $this->fs->copy($from, $to);
53
    }
54
}
55