1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reallyli\LaravelDeployer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Reallyli\LaravelDeployer\Concerns\DeployBuilder; |
8
|
|
|
use Reallyli\LaravelDeployer\Concerns\RendersCode; |
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
10
|
|
|
|
11
|
|
|
class ConfigFile implements Arrayable |
12
|
|
|
{ |
13
|
|
|
use RendersCode; |
14
|
|
|
use DeployBuilder; |
15
|
|
|
|
16
|
|
|
const REPLACEMENT_KEYS = [ |
17
|
|
|
'default', |
18
|
|
|
'strategies', |
19
|
|
|
'hooks.start', |
20
|
|
|
'hooks.build', |
21
|
|
|
'hooks.ready', |
22
|
|
|
'hooks.done', |
23
|
|
|
'hooks.success', |
24
|
|
|
'hooks.fail', |
25
|
|
|
'options', |
26
|
|
|
'hosts', |
27
|
|
|
'localhost', |
28
|
|
|
'include', |
29
|
|
|
'custom_deployer_file', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
protected $configs; |
33
|
|
|
protected $filesystem; |
34
|
|
|
|
35
|
|
|
public function __construct($configs) |
36
|
|
|
{ |
37
|
|
|
$this->configs = collect($configs); |
38
|
|
|
$this->filesystem = app(Filesystem::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return the config file as a string after it has been parsed |
43
|
|
|
* from the `config.stub` file with the `configs` property. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function __toString() |
48
|
|
|
{ |
49
|
|
|
$ds = DIRECTORY_SEPARATOR; |
50
|
|
|
$stub = $this->filesystem->get(__DIR__."{$ds}stubs{$ds}config.stub"); |
51
|
|
|
|
52
|
|
|
foreach (static::REPLACEMENT_KEYS as $key) { |
53
|
|
|
$indent = substr_count($key, '.') + 1; |
54
|
|
|
$value = $this->render(array_get($this->configs, $key), $indent); |
55
|
|
|
$stub = preg_replace('/{{'.$key.'}}/', $value, $stub); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $stub; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function get($key, $default = null) |
62
|
|
|
{ |
63
|
|
|
return $this->configs->get($key, $default); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function toArray() |
67
|
|
|
{ |
68
|
|
|
return $this->configs->toArray(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function toDeployFile() |
72
|
|
|
{ |
73
|
|
|
return new DeployFile($this->configs->toArray()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Method description:toYml. |
78
|
|
|
* |
79
|
|
|
* @author reallyli <[email protected]> |
80
|
|
|
* @since 18/9/28 |
81
|
|
|
* @return mixed |
82
|
|
|
* 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值) |
83
|
|
|
*/ |
84
|
|
|
public function toYaml() : string |
85
|
|
|
{ |
86
|
|
|
return Yaml::dump($this->toArray()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Parse the `config.stub` file and copy its content onto a new |
91
|
|
|
* `deploy.php` file in the config folder of the Laravel project. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function store() |
96
|
|
|
{ |
97
|
|
|
$path = $this->getConfigFullPath(); |
98
|
|
|
|
99
|
|
|
$this->filesystem->put($path, $this->toYaml()); |
100
|
|
|
|
101
|
|
|
return $path; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|