1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Modify; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Helper\Builder\BuilderAwareInterface; |
17
|
|
|
use Graze\DataFile\Helper\GetOptionTrait; |
18
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
19
|
|
|
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface; |
20
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
21
|
|
|
use Graze\DataFile\Node\LocalFile; |
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
use Psr\Log\LoggerAwareInterface; |
24
|
|
|
use Psr\Log\LogLevel; |
25
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
26
|
|
|
|
27
|
|
View Code Duplication |
class Tail implements FileModifierInterface, LoggerAwareInterface, BuilderAwareInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
use OptionalLoggerTrait; |
30
|
|
|
use FileProcessTrait; |
31
|
|
|
use GetOptionTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $lines; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Can this file be modified by this modifier |
40
|
|
|
* |
41
|
|
|
* @param FileNodeInterface $file |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
1 |
|
public function canModify(FileNodeInterface $file) |
46
|
|
|
{ |
47
|
1 |
|
return (($file instanceof LocalFile) && |
48
|
1 |
|
($file->exists())); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Modify the file |
53
|
|
|
* |
54
|
|
|
* @param FileNodeInterface $file |
55
|
|
|
* @param array $options List of options: |
56
|
|
|
* -lines <string> Number of lines to tail (accepts +/- modifiers) |
57
|
|
|
* -postfix <string> (Default: replace) Set this to blank to replace inline |
58
|
|
|
* -keepOldFile <bool> (Default: true) |
59
|
|
|
* |
60
|
|
|
* @return FileNodeInterface |
61
|
|
|
*/ |
62
|
4 |
|
public function modify(FileNodeInterface $file, array $options = []) |
63
|
|
|
{ |
64
|
4 |
|
$this->options = $options; |
65
|
4 |
|
$lines = $this->requireOption('lines'); |
66
|
3 |
|
unset($options['lines']); |
67
|
|
|
|
68
|
3 |
|
if (!($file instanceof LocalFile)) { |
69
|
1 |
|
throw new InvalidArgumentException("Supplied: $file is not a LocalFile"); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return $this->tail($file, $lines, $options); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tail a file |
77
|
|
|
* |
78
|
|
|
* @param LocalFile $file |
79
|
|
|
* @param string $lines Number of lines to tail (accepts +/- modifiers) |
80
|
|
|
* @param array $options List of options: |
81
|
|
|
* -postfix <string> (Default: tail) |
82
|
|
|
* -keepOldFile <bool> (Default: true) |
83
|
|
|
* |
84
|
|
|
* @throws ProcessFailedException |
85
|
|
|
* @return LocalFile |
86
|
|
|
*/ |
87
|
8 |
|
public function tail(LocalFile $file, $lines, array $options = []) |
88
|
|
|
{ |
89
|
8 |
|
$this->options = $options; |
90
|
8 |
|
$this->lines = $lines; |
91
|
|
|
|
92
|
8 |
|
$postfix = $this->getOption('postfix', 'tail'); |
93
|
8 |
|
$output = $this->getTargetFile($file, $postfix); |
94
|
|
|
|
95
|
8 |
|
$this->log(LogLevel::INFO, "Retrieving the last {lines} from file {file}", [ |
96
|
8 |
|
'lines' => $this->lines, |
97
|
8 |
|
'file' => $file, |
98
|
|
|
]); |
99
|
|
|
|
100
|
8 |
|
$cmd = sprintf('tail -n %s %s > %s', $this->lines, $file->getPath(), $output->getPath()); |
101
|
|
|
|
102
|
8 |
|
return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.