|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kiboko\Component\Dockerfile\Dockerfile; |
| 6 | + |
| 7 | +final class Copy implements LayerInterface |
| 8 | +{ |
| 9 | + private string $source; |
| 10 | + private string $destination; |
| 11 | + |
| 12 | + public function __construct(string $source, string $destination) |
| 13 | + { |
| 14 | + $this->source = $source; |
| 15 | + $this->destination = $destination; |
| 16 | + } |
| 17 | + |
| 18 | + /** @return \Iterator|self[] */ |
| 19 | + public static function directory(string $sourcePath, string $destinationPath): \Iterator |
| 20 | + { |
| 21 | + $iterator = new \RecursiveDirectoryIterator( |
| 22 | + $sourcePath, |
| 23 | + \RecursiveDirectoryIterator::SKIP_DOTS |
| 24 | + | \RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
| 25 | + | \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO |
| 26 | + | \RecursiveDirectoryIterator::KEY_AS_PATHNAME |
| 27 | + ); |
| 28 | + |
| 29 | + /** @var \SplFileInfo $fileInfo */ |
| 30 | + foreach (new \RecursiveIteratorIterator($iterator) as $fileInfo) { |
| 31 | + if (!$fileInfo->isFile()) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + yield new self( |
| 36 | + preg_replace('/^'.preg_quote($sourcePath, '/').'/', '', $fileInfo->getPathname()), |
| 37 | + preg_replace('/^'.preg_quote($sourcePath, '/').'/', $destinationPath, $fileInfo->getPathname()), |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param \Iterator<array<string,string>> $iterator |
| 44 | + * @return \Iterator|self[] |
| 45 | + */ |
| 46 | + public static function iterator(\Iterator $iterator): \Iterator |
| 47 | + { |
| 48 | + foreach ($iterator as [$sourcePath, $destinationPath]) { |
| 49 | + yield new self($sourcePath, $destinationPath); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function __toString() |
| 54 | + { |
| 55 | + return sprintf('COPY %s %s', $this->source, $this->destination); |
| 56 | + } |
| 57 | +} |
0 commit comments