Passed
Push — master ( bd3cb9...127ddb )
by Jean-Christophe
17:45
created

UFileSystemWriter::openReplaceInTemplateFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.032
1
<?php
2
3
namespace Ubiquity\utils\base\traits;
4
5
trait UFileSystemWriter {
6
	
7 6
	public static function openReplaceInTemplateFile($source, $keyAndValues) {
8 6
		if (\file_exists($source)) {
9 6
			$str=\file_get_contents($source);
10 6
			return self::replaceFromTemplate($str, $keyAndValues);
11
		}
12
		return false;
13
	}
14
	
15 6
	public static function openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues) {
16 6
		if (($str=self::openReplaceInTemplateFile($source, $keyAndValues))) {
17 6
			return \file_put_contents($destination, $str, LOCK_EX);
18
		}
19
		return false;
20
	}
21
	
22 6
	public static function replaceFromTemplate($content, $keyAndValues) {
23 6
		array_walk($keyAndValues, function (&$item) {
24 6
			if (\is_array($item))
25
				$item=\implode("\n", $item);
26
		});
27 6
			$str=\str_replace(array_keys($keyAndValues), array_values($keyAndValues), $content);
28 6
			return $str;
29
	}
30
	
31 1
	public static function replaceWriteFromContent($content, $destination, $keyAndValues) {
32 1
		return \file_put_contents($destination, self::replaceFromTemplate($content, $keyAndValues), LOCK_EX);
33
	}
34
	
35 8
	public static function save($filename,$content,$flags=LOCK_EX){
36 8
		return \file_put_contents($filename, $content, $flags);
37
	}
38
	
39 8
	public static function xcopy($source, $dest, $permissions = 0755){
40 8
		if (is_link($source)) {
41
			return symlink(readlink($source), $dest);
42
		}
43 8
		if (is_file($source)) {
44 8
			return copy($source, $dest);
45
		}
46 8
		if (!is_dir($dest)) {
47 3
			mkdir($dest, $permissions,true);
48
		}
49 8
		$dir = dir($source);
50 8
		while (false !== $entry = $dir->read()) {
51 8
			if ($entry == '.' || $entry == '..') {
52 8
				continue;
53
			}
54 8
			self::xcopy("$source/$entry", "$dest/$entry", $permissions);
55
		}
56 8
		$dir->close();
57 8
		return true;
58
	}
59
}
60
61