@@ 27-104 (lines=78) @@ | ||
24 | use Psr\Log\LogLevel; |
|
25 | use Symfony\Component\Process\Exception\ProcessFailedException; |
|
26 | ||
27 | class Head 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 | public function canModify(FileNodeInterface $file) |
|
46 | { |
|
47 | return (($file instanceof LocalFile) && |
|
48 | ($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 | public function modify(FileNodeInterface $file, array $options = []) |
|
63 | { |
|
64 | $this->options = $options; |
|
65 | $lines = $this->requireOption('lines'); |
|
66 | unset($options['lines']); |
|
67 | ||
68 | if (!($file instanceof LocalFile)) { |
|
69 | throw new InvalidArgumentException("Supplied: $file is not a LocalFile"); |
|
70 | } |
|
71 | ||
72 | return $this->head($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 | public function head(LocalFile $file, $lines, array $options = []) |
|
88 | { |
|
89 | $this->options = $options; |
|
90 | $this->lines = $lines; |
|
91 | ||
92 | $postfix = $this->getOption('postfix', 'head'); |
|
93 | $output = $this->getTargetFile($file, $postfix); |
|
94 | ||
95 | $this->log(LogLevel::INFO, "Retrieving the last {lines} from file {file}", [ |
|
96 | 'lines' => $this->lines, |
|
97 | 'file' => $file, |
|
98 | ]); |
|
99 | ||
100 | $cmd = sprintf('head -n %s %s > %s', $this->lines, $file->getPath(), $output->getPath()); |
|
101 | ||
102 | return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true)); |
|
103 | } |
|
104 | } |
|
105 |
@@ 27-104 (lines=78) @@ | ||
24 | use Psr\Log\LogLevel; |
|
25 | use Symfony\Component\Process\Exception\ProcessFailedException; |
|
26 | ||
27 | 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 | public function canModify(FileNodeInterface $file) |
|
46 | { |
|
47 | return (($file instanceof LocalFile) && |
|
48 | ($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 | public function modify(FileNodeInterface $file, array $options = []) |
|
63 | { |
|
64 | $this->options = $options; |
|
65 | $lines = $this->requireOption('lines'); |
|
66 | unset($options['lines']); |
|
67 | ||
68 | if (!($file instanceof LocalFile)) { |
|
69 | throw new InvalidArgumentException("Supplied: $file is not a LocalFile"); |
|
70 | } |
|
71 | ||
72 | 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 | public function tail(LocalFile $file, $lines, array $options = []) |
|
88 | { |
|
89 | $this->options = $options; |
|
90 | $this->lines = $lines; |
|
91 | ||
92 | $postfix = $this->getOption('postfix', 'tail'); |
|
93 | $output = $this->getTargetFile($file, $postfix); |
|
94 | ||
95 | $this->log(LogLevel::INFO, "Retrieving the last {lines} from file {file}", [ |
|
96 | 'lines' => $this->lines, |
|
97 | 'file' => $file, |
|
98 | ]); |
|
99 | ||
100 | $cmd = sprintf('tail -n %s %s > %s', $this->lines, $file->getPath(), $output->getPath()); |
|
101 | ||
102 | return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true)); |
|
103 | } |
|
104 | } |
|
105 |