|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Handlers\GenericFileHandler |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Handlers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Exceptions\LineNotFoundException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A generic file handler implementation. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
class GenericFileHandler implements GenericFileHandlerInterface |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Remove's the passed line from the file with the passed name. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $line The line to be removed |
|
41
|
|
|
* @param string $filename The name of the file the line has to be removed from |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
* @throws \TechDivision\Import\Exceptions\LineNotFoundException Is thrown, if the requested line can not be found in the file |
|
45
|
|
|
* @throws \Exception Is thrown, if the file can not be written, after the line has been removed |
|
46
|
|
|
* @see \TechDivision\Import\Handlers\GenericFileHandlerInterface::removeLineFromFile() |
|
47
|
|
|
*/ |
|
48
|
|
|
public function removeLineFromFilename(string $line, string $filename) : void |
|
49
|
|
|
{ |
|
50
|
|
|
$fh = fopen($filename, 'r+'); |
|
51
|
|
|
$this->removeLineFromFile($line, $fh); |
|
52
|
|
|
fclose($fh); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Remove's the passed line from the file with the passed file handle. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $line The line to be removed |
|
59
|
|
|
* @param resource $fh The file handle of the file the line has to be removed |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
* @throws \TechDivision\Import\Exceptions\LineNotFoundException Is thrown, if the requested line can not be found in the file |
|
63
|
|
|
* @throws \Exception Is thrown, if the file can not be written, after the line has been removed |
|
64
|
|
|
*/ |
|
65
|
|
|
public function removeLineFromFile(string $line, $fh) : void |
|
66
|
|
|
{ |
|
67
|
|
|
|
|
68
|
|
|
// initialize the array for the PIDs found in the PID file |
|
69
|
|
|
$lines = array(); |
|
70
|
|
|
|
|
71
|
|
|
// initialize the flag if the line has been found |
|
72
|
|
|
$found = false; |
|
73
|
|
|
|
|
74
|
|
|
// rewind the file pointer |
|
75
|
|
|
rewind($fh); |
|
76
|
|
|
|
|
77
|
|
|
// read the lines with the PIDs from the PID file |
|
78
|
|
|
while (($buffer = fgets($fh, 4096)) !== false) { |
|
79
|
|
|
// remove the new line |
|
80
|
|
|
$buffer = trim($buffer); |
|
81
|
|
|
// if the line is the one to be removed, ignore the line |
|
82
|
|
|
if ($line === $buffer) { |
|
83
|
|
|
$found = true; |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// add the found PID to the array |
|
88
|
|
|
$lines[] = $buffer; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// query whether or not, we found the line |
|
92
|
|
|
if ($found === false) { |
|
93
|
|
|
throw new LineNotFoundException(sprintf('Line "%s" can not be found', $line)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// empty the file and rewind the file pointer |
|
97
|
|
|
ftruncate($fh, 0); |
|
98
|
|
|
rewind($fh); |
|
99
|
|
|
|
|
100
|
|
|
// append the existing lines to the file |
|
101
|
|
|
foreach ($lines as $ln) { |
|
102
|
|
|
if (fwrite($fh, $ln . PHP_EOL) === false) { |
|
103
|
|
|
throw new \Exception(sprintf('Can\'t write "%s" to file', $ln)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// clear the file status cache |
|
108
|
|
|
clearstatcache(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|