1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tworzenieweb\SqlProvisioner\Filesystem; |
4
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Luke Adamczewski |
11
|
|
|
* @package Tworzenieweb\SqlProvisioner\Filesystem |
12
|
|
|
*/ |
13
|
|
|
class WorkingDirectory |
14
|
|
|
{ |
15
|
|
|
const MANDATORY_ENV_VARIABLES = [ |
16
|
|
|
'DATABASE_USER', |
17
|
|
|
'DATABASE_PASSWORD', |
18
|
|
|
'DATABASE_NAME', |
19
|
|
|
'DATABASE_PORT', |
20
|
|
|
'DATABASE_HOST', |
21
|
|
|
'PROVISIONING_TABLE', |
22
|
|
|
'PROVISIONING_TABLE_CANDIDATE_NUMBER_COLUMN', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $currentDirectory; |
27
|
|
|
|
28
|
|
|
/** @var Filesystem */ |
29
|
|
|
private $filesystem; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $currentDirectoryAbsolute; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $currentDirectory |
38
|
|
|
* @param CandidatesFinder $finder |
39
|
|
|
*/ |
40
|
|
|
public function __construct($currentDirectory, CandidatesFinder $finder) |
41
|
|
|
{ |
42
|
|
|
$this->currentDirectory = $currentDirectory; |
43
|
|
|
$this->filesystem = new Filesystem(); |
44
|
|
|
$this->currentDirectoryAbsolute = $this->buildAbsolutePath($currentDirectory); |
45
|
|
|
$this->finder = $finder; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $newPath |
52
|
|
|
* @return WorkingDirectory |
53
|
|
|
*/ |
54
|
|
|
public function cd($newPath) |
55
|
|
|
{ |
56
|
|
|
return new WorkingDirectory($newPath, $this->finder); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
public function touchDotEnv() |
62
|
|
|
{ |
63
|
|
|
$this->filesystem->dumpFile( |
64
|
|
|
$this->getDotEnvFilepath(), |
65
|
|
|
<<<DRAFT |
66
|
|
|
DATABASE_USER=[user] |
67
|
|
|
DATABASE_PASSWORD=[password] |
68
|
|
|
DATABASE_HOST=[host] |
69
|
|
|
DATABASE_PORT=[port] |
70
|
|
|
DATABASE_NAME=[database] |
71
|
|
|
PROVISIONING_TABLE=changelog_database_deployments |
72
|
|
|
PROVISIONING_TABLE_CANDIDATE_NUMBER_COLUMN=deploy_script_number |
73
|
|
|
DRAFT |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getDotEnvFilepath() |
83
|
|
|
{ |
84
|
|
|
return $this->currentDirectoryAbsolute . '/.env'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
public function loadDotEnv() |
90
|
|
|
{ |
91
|
|
|
(new Dotenv($this->currentDirectoryAbsolute))->load(); |
92
|
|
|
|
93
|
|
|
$hasAllKeys = count( |
94
|
|
|
array_intersect_key( |
95
|
|
|
array_flip(self::MANDATORY_ENV_VARIABLES), |
96
|
|
|
$_ENV |
97
|
|
|
) |
98
|
|
|
) === count(self::MANDATORY_ENV_VARIABLES); |
99
|
|
|
|
100
|
|
|
if (!$hasAllKeys) { |
101
|
|
|
throw new \LogicException('Provided .env is missing the mandatory keys'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Finder |
109
|
|
|
*/ |
110
|
|
|
public function getCandidates() |
111
|
|
|
{ |
112
|
|
|
return $this->finder->find($this->currentDirectoryAbsolute); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $path |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private function buildAbsolutePath($path) |
122
|
|
|
{ |
123
|
|
|
$absolutePath = $path; |
124
|
|
|
|
125
|
|
|
if (!$this->filesystem->isAbsolutePath($path)) { |
126
|
|
|
$absolutePath = realpath($path); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $absolutePath; |
130
|
|
|
} |
131
|
|
|
} |
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: