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