1 | <?php |
||
16 | class ReplaceText implements FileModifierInterface, LoggerAwareInterface, ProcessFactoryAwareInterface |
||
17 | { |
||
18 | use OptionalLoggerTrait; |
||
19 | use FileProcessTrait; |
||
20 | use GetOptionTrait; |
||
21 | |||
22 | /** |
||
23 | * Can this file be modified by this modifier |
||
24 | * |
||
25 | * @param FileNodeInterface $file |
||
26 | * |
||
27 | * @return bool |
||
28 | */ |
||
29 | 1 | public function canModify(FileNodeInterface $file) |
|
30 | { |
||
31 | return ( |
||
32 | 1 | ($file instanceof localFile) && |
|
33 | 1 | ($file->exists()) |
|
34 | ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Modify the file |
||
39 | * |
||
40 | * @param FileNodeInterface $file |
||
41 | * @param array $options List of options: |
||
42 | * -fromText <string|array> Text to be replace |
||
43 | * -toText <string|array> Text to replace |
||
44 | * -postifx <string> (Default: replace) Set this to blank to replace inline |
||
45 | * -keepOldFile <bool> (Default: true) |
||
46 | * |
||
47 | * @return FileNodeInterface |
||
48 | */ |
||
49 | 5 | public function modify(FileNodeInterface $file, array $options = []) |
|
64 | |||
65 | /** |
||
66 | * @extend Graze\DataFile\Node\File\LocalFile |
||
67 | * |
||
68 | * @param LocalFile $file |
||
69 | * @param string|string[] $fromText |
||
70 | * @param string|string[] $toText |
||
71 | * @param array $options List of options: |
||
72 | * -postfix <string> (Default: replace) Set this to blank to replace inline |
||
73 | * -keepOldFile <bool> (Default: true) |
||
74 | * |
||
75 | * @throws InvalidArgumentException |
||
76 | * @throws ProcessFailedException |
||
77 | * @return LocalFile |
||
78 | */ |
||
79 | 11 | public function replaceText(LocalFile $file, $fromText, $toText, array $options = []) |
|
136 | |||
137 | /** |
||
138 | * Get the string replacement command for a single item |
||
139 | * |
||
140 | * @param $fromText |
||
141 | * @param $toText |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | 10 | private function getReplacementCommand($fromText, $toText) |
|
153 | } |
||
154 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: