|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\AdminLTETemplateLaravel\Console; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Adapter\Local as LocalAdapter; |
|
6
|
|
|
use League\Flysystem\Filesystem as Flysystem; |
|
7
|
|
|
use League\Flysystem\MountManager; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Installable. |
|
11
|
|
|
* |
|
12
|
|
|
* @package Acacha\AdminLTETemplateLaravel\Console |
|
13
|
|
|
*/ |
|
14
|
|
|
trait Installable |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Install files from array. |
|
18
|
|
|
* |
|
19
|
|
|
* @param $files |
|
20
|
|
|
*/ |
|
21
|
|
|
private function install($files) |
|
22
|
|
|
{ |
|
23
|
|
|
foreach ($files as $fileSrc => $fileDst) { |
|
24
|
|
|
if (file_exists($fileDst) && !$this->force && !$this->confirmOverwrite(basename($fileDst))) { |
|
|
|
|
|
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
if ($this->files->isFile($fileSrc)) { |
|
28
|
|
|
$this->publishFile($fileSrc, $fileDst); |
|
29
|
|
|
} elseif ($this->files->isDirectory($fileSrc)) { |
|
|
|
|
|
|
30
|
|
|
$this->publishDirectory($fileSrc, $fileDst); |
|
31
|
|
|
} else { |
|
32
|
|
|
$this->error("Can't locate path: <{$fileSrc}>"); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param $fileName |
|
39
|
|
|
* @param string $prompt |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function confirmOverwrite($fileName, $prompt = '') |
|
44
|
|
|
{ |
|
45
|
|
|
$prompt = (empty($prompt)) |
|
46
|
|
|
? $fileName.' already exists. Do you want to overwrite it? [y|N]' |
|
47
|
|
|
: $prompt; |
|
48
|
|
|
|
|
49
|
|
|
return $this->confirm($prompt, false); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create the directory to house the published files if needed. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $directory |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function createParentDirectory($directory) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$this->files->isDirectory($directory)) { |
|
62
|
|
|
$this->files->makeDirectory($directory, 0755, true); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Publish the file to the given path. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $from |
|
70
|
|
|
* @param string $to |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function publishFile($from, $to) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->createParentDirectory(dirname($to)); |
|
77
|
|
|
$this->files->copy($from, $to); |
|
78
|
|
|
$this->status($from, $to, 'File'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Publish the directory to the given directory. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $from |
|
85
|
|
|
* @param string $to |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function publishDirectory($from, $to) |
|
90
|
|
|
{ |
|
91
|
|
|
$manager = new MountManager([ |
|
92
|
|
|
'from' => new Flysystem(new LocalAdapter($from)), |
|
93
|
|
|
'to' => new Flysystem(new LocalAdapter($to)), |
|
94
|
|
|
]); |
|
95
|
|
|
foreach ($manager->listContents('from://', true) as $file) { |
|
96
|
|
|
if ($file['type'] === 'file' && (!$manager->has('to://'.$file['path']) || $this->force)) { |
|
97
|
|
|
$manager->put('to://'.$file['path'], $manager->read('from://'.$file['path'])); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
$this->status($from, $to, 'Directory'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Write a status message to the console. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $from |
|
107
|
|
|
* @param string $to |
|
108
|
|
|
* @param string $type |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function status($from, $to, $type) |
|
113
|
|
|
{ |
|
114
|
|
|
$from = str_replace(base_path(), '', realpath($from)); |
|
115
|
|
|
$to = str_replace(base_path(), '', realpath($to)); |
|
116
|
|
|
$this->line('<info>Copied '.$type.'</info> <comment>['.$from.']</comment> <info>To</info> <comment>['.$to.']</comment>'); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
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: