-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextNrep.php
116 lines (99 loc) · 2.67 KB
/
textNrep.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Created by PhpStorm.
* User: Mike
* Date: 2/6/2020
* Time: 1:20 AM
*/
require_once 'replacements.php';
class textNrep {
const DEBUG = FALSE;
/** @var null|\RecursiveIteratorIterator $item */
public $item = NULL;
/**
* Loads and replaces all text in file.
*
* @param \replacement $replacement
* @param array $exclusions
* @param string $ext
* @param \SplFileInfo|null $item
* @return mixed
*/
function textReplaceFile(replacement $replacement, array $exclusions, string $ext = '*', ?SplFileInfo $item = NULL) {
if(isset($item)) {
$this->item = $item;
}
$pathName = $this->item->getPathName();
$excluded = self::excluded($exclusions, $pathName);
//echo "excluded: $excluded";
if(!$excluded && $this->item->isFile() && (stripos($pathName, $ext) !== FALSE || $ext == '*')) {
$file_contents = file_get_contents($pathName);
$return = $this->textReplaceString($replacement, $file_contents);
} else {
$return = FALSE;
}
if($return) {
file_put_contents($pathName, $file_contents);
}
return $return;
}
/**
* Go through all of the replacements to perform, and perform the text custom Replacement.
* @param $string
* @return int
*/
public function textReplace(&$string) {
$replacements = new replacements();
$totalCount = 0;
foreach($replacements->values as $replacement) {
$totalCount += $this->textReplaceString($replacement, $string);
}
return $totalCount;
}
/**
* @param $quoteToChange
* @return string
*/
private function getOpQuote($quoteToChange) {
if($quoteToChange == '"') {
$replace = "'";
} else {
$replace = '"';
}
return $replace;
}
/**
* @param array $exclusions
* @param string $path
* @return bool
*/
private static function excluded(array $exclusions, string $path) {
$path = realpath($path);
foreach($exclusions as $exclusion) {
$exclusion = realpath($exclusion);
if(strpos($path, $exclusion) > 0) {
$excluded = TRUE;
//echo "Excluded:$exclusion\n";
return TRUE;
}
}
return FALSE;
}
/**
* Replace both the Regular String and the Reverse String.
*
* @param \replacement $replacement ->searchFor
* @param $jQueryString
* @return int
*/
public function textReplaceString(replacement $replacement, &$jQueryString) {
$firstCount = 0;
$jQueryString = $replacement->strReplace($jQueryString, $firstCount);
$secondCount = 0;
//replace the regular string.
if(strpos($replacement->searchFor(), "'") !== FALSE || strpos($replacement->searchFor(), '"') !== FALSE) {
$jQueryString = $replacement->strReplace($jQueryString, $secondCount, TRUE);
}
return $firstCount + $secondCount;
}
}