1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Antidot\Installer\Template; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
use function file_exists; |
10
|
|
|
use function file_put_contents; |
11
|
|
|
use function is_dir; |
12
|
|
|
use function is_readable; |
13
|
|
|
use function method_exists; |
14
|
|
|
use function mkdir; |
15
|
|
|
use function sprintf; |
16
|
|
|
use function unlink; |
17
|
|
|
|
18
|
|
|
abstract class CommonFileStructure implements FileStructureFactory |
19
|
|
|
{ |
20
|
|
|
public const COMMUNITY_FILES = [ |
21
|
|
|
'/CHANGELOG.md', |
22
|
|
|
'/CODE_OF_CONDUCT.md', |
23
|
|
|
'/CONTRIBUTING.md', |
24
|
|
|
'/PULL_REQUEST_TEMPLATE.md', |
25
|
|
|
'/LICENSE', |
26
|
|
|
]; |
27
|
|
|
|
28
|
3 |
|
protected function verifyInstallationPath(string $installationPath): void |
29
|
|
|
{ |
30
|
3 |
|
if (!is_dir($installationPath) && !mkdir($dir = $installationPath, 0755, true) && !is_dir($dir)) { |
31
|
|
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
if (!is_readable($installationPath)) { |
35
|
|
|
throw new RuntimeException(sprintf('Given directory "%s" is not readable.', $installationPath)); |
36
|
|
|
} |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $installationPath |
41
|
|
|
* @param array<string> $directories |
42
|
|
|
*/ |
43
|
3 |
|
protected function createDirectories(string $installationPath, array $directories): void |
44
|
|
|
{ |
45
|
3 |
|
foreach ($directories as $directory) { |
46
|
3 |
|
if (!mkdir($dir = sprintf('%s/%s', $installationPath, $directory), 0755, true) && !is_dir($dir)) { |
47
|
1 |
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $installationPath |
54
|
|
|
* @param array<string, string> $files |
55
|
|
|
*/ |
56
|
2 |
|
protected function createFiles(string $installationPath, array $files): void |
57
|
|
|
{ |
58
|
2 |
|
foreach ($files as $method => $filename) { |
59
|
2 |
|
if (false === method_exists($this, $method)) { |
60
|
|
|
throw new RuntimeException(sprintf('Method "%s" is not defined.', $method)); |
61
|
|
|
} |
62
|
2 |
|
file_put_contents(sprintf('%s/%s', $installationPath, $filename), $this->$method()); |
63
|
|
|
} |
64
|
2 |
|
} |
65
|
|
|
|
66
|
2 |
|
protected function removeCommunityFiles(string $installationPath): void |
67
|
|
|
{ |
68
|
2 |
|
foreach (self::COMMUNITY_FILES as $fileToDelete) { |
69
|
2 |
|
$filePath = $installationPath . $fileToDelete; |
70
|
2 |
|
if (file_exists($filePath)) { |
71
|
1 |
|
unlink($filePath); |
72
|
|
|
} |
73
|
|
|
} |
74
|
2 |
|
} |
75
|
|
|
} |
76
|
|
|
|