This repository was archived by the owner on Oct 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathContext.php
More file actions
164 lines (138 loc) · 3.58 KB
/
Copy pathContext.php
File metadata and controls
164 lines (138 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Docker\Context;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
/**
* Docker\Context\Context.
*/
class Context implements ContextInterface
{
public const FORMAT_STREAM = 'stream';
public const FORMAT_TAR = 'tar';
/**
* @var bool Whether to remove the context directory
*/
private $cleanup = false;
/**
* @var string
*/
private $directory;
/**
* @var process Tar process
*/
private $process;
/**
* @var Filesystem
*/
private $fs;
/**
* @var resource Tar stream
*/
private $stream;
/**
* @var string Format of the context (stream or tar)
*/
private $format = self::FORMAT_STREAM;
/**
* @param string $directory Directory of context
* @param string $format Format to use when sending the call (stream or tar: string)
* @param Filesystem $fs filesystem object for cleaning the context directory on destruction
*/
public function __construct($directory, $format = self::FORMAT_STREAM, Filesystem $fs = null)
{
$this->directory = $directory;
$this->format = $format;
$this->fs = $fs ?? new Filesystem();
}
/**
* Get directory of Context.
*
* @return string
*/
public function getDirectory()
{
return $this->directory;
}
/**
* Set directory of Context.
*
* @param string $directory Targeted directory
*/
public function setDirectory($directory): void
{
$this->directory = $directory;
}
/**
* Return content of Dockerfile of this context.
*
* @return string Content of dockerfile
*/
public function getDockerfileContent()
{
return \file_get_contents($this->directory.DIRECTORY_SEPARATOR.'Dockerfile');
}
/**
* @return bool
*/
public function isStreamed()
{
return self::FORMAT_STREAM === $this->format;
}
/**
* @return resource|string
*/
public function read()
{
return $this->isStreamed() ? $this->toStream() : $this->toTar();
}
/**
* Return the context as a tar archive.
*
* @throws \Symfony\Component\Process\Exception\ProcessFailedException
*
* @return string Tar content
*/
public function toTar()
{
$process = new Process('/usr/bin/env tar c .', $this->directory);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
/**
* Return a stream for this context.
*
* @return resource Stream resource in memory
*/
public function toStream()
{
if (!\is_resource($this->process)) {
$this->process = \proc_open('/usr/bin/env tar c .', [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes, $this->directory);
$this->stream = $pipes[1];
}
return $this->stream;
}
public function __destruct()
{
if (\is_resource($this->stream)) {
\fclose($this->stream);
}
if (\is_resource($this->process)) {
\proc_close($this->process);
}
if ($this->cleanup) {
$this->fs->remove($this->directory);
}
}
/**
* @param bool $value whether to remove the context directory
*/
public function setCleanup(bool $value): void
{
$this->cleanup = $value;
}
}