-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathHasTempDirTrait.php
51 lines (43 loc) · 1.08 KB
/
HasTempDirTrait.php
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
<?php
namespace Platformsh\Cli\Tests;
trait HasTempDirTrait
{
protected $tempDir;
protected function tempDirSetUp()
{
if (!isset($this->tempDir)) {
$this->tempDir = $this->createTempDir(sys_get_temp_dir(), 'pshCliTmp');
}
}
/**
* @param string $parentDir
* @param string $prefix
*
* @return string
*/
protected function createTempDir($parentDir, $prefix = '')
{
if (!($tempDir = tempnam($parentDir, $prefix))
|| !unlink($tempDir)
|| !mkdir($tempDir, 0755)) {
throw new \RuntimeException('Failed to create temporary directory in: ' . $parentDir);
}
return $tempDir;
}
/**
* @param string $prefix
*
* @return string
*/
protected function createTempSubDir($prefix = '')
{
$this->tempDirSetUp();
return $this->createTempDir($this->tempDir, $prefix);
}
public function tearDown()
{
if (!empty($this->tempDir)) {
exec('rm -Rf ' . escapeshellarg($this->tempDir));
}
}
}