/** * prependix: str|callable * * str: The prefix * callable: str:prefix callable ( str:fileAbsolutePath, str:fileContent ) * */ public static function prependFiles(array $files, $prependix) { MikeFileFreeHandTool::updateFiles($files, function ($c, $file) use($prependix) { if (is_callable($prependix)) { $prependix = call_user_func($prependix, $file, $c); } return $prependix . $c; }); }
/** * Remove only the lines containing the needle. */ public static function strip(array $files, $needle) { MikeFileFreeHandTool::updateFiles($files, function ($c, $file) use($needle) { $lines = file($file); foreach ($lines as $k => $v) { if (false !== strpos($v, $needle)) { unset($lines[$k]); } } return implode('', $lines); }); }
/** * Remove the x first lines of the given set of files. */ public static function removeFirstXLines($x, array $files) { $x = (int) $x; if ($x > 0) { MikeFileFreeHandTool::updateFiles($files, function ($c, $file) use($x) { $lines = file($file); for ($i = 0; $i < $x; $i++) { array_shift($lines); } if (is_array($lines)) { return implode('', $lines); } return ""; // assuming x > nbLines }); } else { trigger_error("x must be positive", E_USER_WARNING); } }
/** * Reduce consecutive end of lines to one single end of line */ public static function reduce(array $files) { MikeFileFreeHandTool::updateFiles($files, function ($c, $file) { return preg_replace("!\n+!", PHP_EOL, $c); }); }