1 | <?php |
||
27 | class ReplaceText implements FileModifierInterface, LoggerAwareInterface, BuilderAwareInterface |
||
28 | { |
||
29 | use OptionalLoggerTrait; |
||
30 | use FileProcessTrait; |
||
31 | use GetOptionTrait; |
||
32 | |||
33 | /** |
||
34 | * Can this file be modified by this modifier |
||
35 | * |
||
36 | * @param FileNodeInterface $file |
||
37 | * |
||
38 | * @return bool |
||
39 | */ |
||
40 | 1 | public function canModify(FileNodeInterface $file) |
|
41 | { |
||
42 | return ( |
||
43 | 1 | ($file instanceof localFile) && |
|
44 | 1 | ($file->exists()) |
|
45 | ); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Modify the file |
||
50 | * |
||
51 | * @param FileNodeInterface $file |
||
52 | * @param array $options List of options: |
||
53 | * -fromText <string|array> Text to be replace |
||
54 | * -toText <string|array> Text to replace |
||
55 | * -postifx <string> (Default: replace) Set this to blank to replace inline |
||
56 | * -keepOldFile <bool> (Default: true) |
||
57 | * |
||
58 | * @return FileNodeInterface |
||
59 | */ |
||
60 | 5 | public function modify(FileNodeInterface $file, array $options = []) |
|
75 | |||
76 | /** |
||
77 | * @param LocalFile $file |
||
78 | * @param string|string[] $fromText |
||
79 | * @param string|string[] $toText |
||
80 | * @param array $options List of options: |
||
81 | * -postfix <string> (Default: replace) Set this to blank to replace inline |
||
82 | * -keepOldFile <bool> (Default: true) |
||
83 | * |
||
84 | * @throws InvalidArgumentException |
||
85 | * @throws ProcessFailedException |
||
86 | * @return LocalFile |
||
87 | */ |
||
88 | 12 | public function replaceText(LocalFile $file, $fromText, $toText, array $options = []) |
|
120 | |||
121 | /** |
||
122 | * @param string|string[] $from |
||
123 | * @param string|string[] $to |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 12 | private function getReplacementString($from, $to) |
|
146 | |||
147 | /** |
||
148 | * Get the string replacement command for a single item |
||
149 | * |
||
150 | * @param string $fromText |
||
151 | * @param string $toText |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | 10 | private function getReplacementCommand($fromText, $toText) |
|
165 | } |
||
166 |