Skip to content

Commit 57b05dc

Browse files
committed
Initial commit, extracted from php-etl/satellite
0 parents  commit 57b05dc

22 files changed

+687
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "php-etl/dockerfile",
3+
"type": "library",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Grégory Planchat",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"minimum-stability": "dev",
12+
"prefer-stable": true,
13+
"require": {
14+
"php": "^8.0",
15+
"php-etl/packaging-contracts": "^0.1.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Kiboko\\Component\\Dockerfile\\": "src/"
20+
}
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"functional\\Kiboko\\Component\\Dockerfile\\": "tests/functional/"
25+
}
26+
},
27+
"extra": {
28+
"branch-alias": {
29+
"dev-main": "0.1.x-dev"
30+
}
31+
},
32+
"config": {
33+
"bin-dir": "bin"
34+
}
35+
}

composer.lock

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Dockerfile.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Dockerfile;
6+
7+
use Kiboko\Component\Dockerfile\Dockerfile\LayerInterface;
8+
use Kiboko\Contract\Packaging\FileInterface;
9+
10+
final class Dockerfile implements \IteratorAggregate, \Countable, FileInterface
11+
{
12+
/** @var iterable|Dockerfile\LayerInterface[] */
13+
private iterable $layers;
14+
15+
public function __construct(null|Dockerfile\LayerInterface ...$layers)
16+
{
17+
$this->layers = $layers;
18+
}
19+
20+
public function push(Dockerfile\LayerInterface ...$layers): void
21+
{
22+
array_push($this->layers, ...$layers);
23+
}
24+
25+
public function __toString()
26+
{
27+
return implode(PHP_EOL, array_map(function (LayerInterface $layer) {
28+
return (string) $layer . PHP_EOL;
29+
}, $this->layers));
30+
}
31+
32+
public function getIterator(): \ArrayIterator
33+
{
34+
return new \ArrayIterator($this->layers);
35+
}
36+
37+
public function count(): int
38+
{
39+
return count($this->layers);
40+
}
41+
42+
public function asResource()
43+
{
44+
$resource = fopen('php://temp', 'rb+');
45+
fwrite($resource, (string) $this);
46+
fseek($resource, 0, SEEK_SET);
47+
48+
return $resource;
49+
}
50+
51+
public function getPath(): string
52+
{
53+
return 'Dockerfile';
54+
}
55+
}

src/Dockerfile/Cmd.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Dockerfile\Dockerfile;
6+
7+
final class Cmd implements LayerInterface
8+
{
9+
private iterable $command;
10+
11+
public function __construct(string ...$command)
12+
{
13+
$this->command = $command;
14+
}
15+
16+
public function __toString()
17+
{
18+
return sprintf('CMD [%s]', implode(', ', $this->command));
19+
}
20+
}

src/Dockerfile/Copy.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

src/Dockerfile/Entrypoint.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Dockerfile\Dockerfile;
6+
7+
final class Entrypoint implements LayerInterface
8+
{
9+
private iterable $entrypoint;
10+
11+
public function __construct(string ...$entrypoint)
12+
{
13+
$this->entrypoint = $entrypoint;
14+
}
15+
16+
public function __toString()
17+
{
18+
return sprintf('ENTRYPOINT [%s]', implode(', ', $this->entrypoint));
19+
}
20+
}

src/Dockerfile/From.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Dockerfile\Dockerfile;
6+
7+
final class From implements LayerInterface
8+
{
9+
private string $source;
10+
11+
public function __construct(string $source)
12+
{
13+
$this->source = $source;
14+
}
15+
16+
public function __toString()
17+
{
18+
return sprintf('FROM %s', $this->source);
19+
}
20+
}

src/Dockerfile/Label.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Dockerfile\Dockerfile;
6+
7+
final class Label implements LayerInterface
8+
{
9+
private string $key;
10+
private string $value;
11+
12+
public function __construct(string $key, string $value)
13+
{
14+
$this->key = $key;
15+
$this->value = $value;
16+
}
17+
18+
public function __toString()
19+
{
20+
return sprintf('LABEL %s="%s"', $this->key, $this->value);
21+
}
22+
}

src/Dockerfile/LayerInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Kiboko\Component\Dockerfile\Dockerfile;
4+
5+
interface LayerInterface
6+
{
7+
public function __toString();
8+
}

0 commit comments

Comments
 (0)