1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Oliverde8\PhpEtlBundle\Etl\Operation\Cleanup; |
6
|
|
|
|
7
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation; |
8
|
|
|
use Oliverde8\Component\PhpEtl\Item\DataItemInterface; |
9
|
|
|
use Oliverde8\Component\PhpEtl\Item\ItemInterface; |
10
|
|
|
use Oliverde8\Component\PhpEtl\Model\ExecutionContext; |
11
|
|
|
use Oliverde8\PhpEtlBundle\Entity\EtlExecution; |
12
|
|
|
use Oliverde8\PhpEtlBundle\Services\ChainWorkDirManager; |
13
|
|
|
use Oliverde8\PhpEtlBundle\Services\FileSystemFactoryInterface; |
14
|
|
|
|
15
|
|
|
class DeleteFilesForOldExecutionOperation extends AbstractChainOperation |
16
|
|
|
{ |
17
|
|
|
protected ChainWorkDirManager $chainWorkDirManager; |
18
|
|
|
|
19
|
|
|
protected FileSystemFactoryInterface $fileSystemFactory; |
20
|
|
|
|
21
|
|
|
public function __construct(ChainWorkDirManager $chainWorkDirManager, FileSystemFactoryInterface $fileSystemFactory) |
22
|
|
|
{ |
23
|
|
|
$this->chainWorkDirManager = $chainWorkDirManager; |
24
|
|
|
$this->fileSystemFactory = $fileSystemFactory; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** @var EtlExecution $entity */ |
30
|
|
|
$entity = $item->getData(); |
31
|
|
|
|
32
|
|
|
$fileSystem = $this->fileSystemFactory->get($entity); |
33
|
|
|
foreach ($fileSystem->listContents("") as $file) { |
34
|
|
|
if (!in_array($file, ['.', '..'])) { |
35
|
|
|
$fileSystem->delete($file); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
try { |
39
|
|
|
$fileSystem->delete(""); |
40
|
|
|
} catch (\Exception $exception){} |
|
|
|
|
41
|
|
|
|
42
|
|
|
$executionWorkDir = $this->chainWorkDirManager->getLocalTmpWorkDir($entity); |
43
|
|
|
if (!file_exists($executionWorkDir)) { |
44
|
|
|
return $item; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$parentDir = dirname($executionWorkDir); |
48
|
|
|
$this->deleteDirectory($executionWorkDir); |
49
|
|
|
|
50
|
|
|
while (basename($parentDir) !== 'var' && empty($this->getDirFiles($parentDir))) { |
51
|
|
|
@rmdir($parentDir); |
|
|
|
|
52
|
|
|
$parentDir = dirname($parentDir); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $item; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function deleteDirectory(string $dir) { |
59
|
|
|
if (!file_exists($dir)) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!is_dir($dir)) { |
64
|
|
|
return unlink($dir); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($this->getDirFiles($dir) as $item) { |
68
|
|
|
if (!$this->deleteDirectory("$dir/$item")) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return rmdir($dir); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getDirFiles(string $dir) |
77
|
|
|
{ |
78
|
|
|
if (!file_exists($dir) || !is_dir($dir)) { |
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$files = []; |
83
|
|
|
foreach (scandir($dir) as $item) { |
84
|
|
|
if ($item == '.' || $item == '..') { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$files[] =$item; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $files; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.