1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © Youwe. All rights reserved. |
5
|
|
|
* https://www.youweagency.com |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Youwe\Composer; |
11
|
|
|
|
12
|
|
|
use Composer\IO\IOInterface; |
13
|
|
|
use Youwe\FileMapping\FileMappingInterface; |
14
|
|
|
use Youwe\FileMapping\FileMappingReaderInterface; |
15
|
|
|
use SplFileObject; |
16
|
|
|
|
17
|
|
|
class FileInstaller |
18
|
|
|
{ |
19
|
|
|
/** @var FileMappingReaderInterface */ |
20
|
|
|
private $mappingReader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param FileMappingReaderInterface $mappingReader |
26
|
|
|
*/ |
27
|
|
|
public function __construct(FileMappingReaderInterface $mappingReader) |
28
|
|
|
{ |
29
|
|
|
$this->mappingReader = $mappingReader; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Install the deployer files. |
34
|
|
|
* |
35
|
|
|
* @param IOInterface $io |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
* |
39
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
40
|
|
|
*/ |
41
|
|
|
public function install(IOInterface $io) |
42
|
|
|
{ |
43
|
|
|
foreach ($this->mappingReader as $mapping) { |
44
|
|
|
if (file_exists($mapping->getDestination())) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->installFile($mapping); |
49
|
|
|
|
50
|
|
|
$io->write( |
51
|
|
|
sprintf( |
52
|
|
|
'<info>Installed:</info> %s', |
53
|
|
|
$mapping->getRelativeDestination() |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Install the given file if it does not exist. |
61
|
|
|
* |
62
|
|
|
* @param FileMappingInterface $mapping |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
* |
66
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
67
|
|
|
*/ |
68
|
|
|
public function installFile(FileMappingInterface $mapping) |
69
|
|
|
{ |
70
|
|
|
$inputFile = new SplFileObject($mapping->getSource(), 'r'); |
71
|
|
|
$targetFile = new SplFileObject($mapping->getDestination(), 'w+'); |
72
|
|
|
|
73
|
|
|
foreach ($inputFile as $input) { |
74
|
|
|
$targetFile->fwrite($input); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|