.
676 |
--------------------------------------------------------------------------------
/replacement.php:
--------------------------------------------------------------------------------
1 | searchFor = new swapQuotes($searchFor);
29 | }
30 |
31 | /**
32 | * Set ReverseWith and the Cache.
33 | *
34 | * @param string $replaceWith
35 | */
36 | public function setReplaceWith(string $replaceWith) {
37 | $this->replaceWith = new swapQuotes($replaceWith);
38 | }
39 |
40 | /**
41 | * replacement constructor.
42 | *
43 | * @param string|NULL $searchFor
44 | * @param string|NULL $replaceWith
45 | */
46 | public function __construct(string $searchFor = NULL, string $replaceWith = NULL) {
47 | if(isset($searchFor)) {
48 | $this->setSearchFor($searchFor);
49 | }
50 | if(isset($replaceWith)) {
51 | $this->setReplaceWith($replaceWith);
52 | }
53 | }
54 |
55 |
56 | /**
57 | * Text value to search for.
58 | *
59 | * @return null|string
60 | */
61 | public function searchFor() {
62 | return $this->searchFor->string;
63 | }
64 |
65 |
66 | /**
67 | * Text value to replace with.
68 | *
69 | * @return null|string
70 | */
71 | public function replaceWith() {
72 | return $this->replaceWith->string;
73 | }
74 |
75 | /**
76 | * Replace the searchFor with the replaceWith. Allows for opposits to be generated.
77 | *
78 | * @param $file_contents
79 | * @param int|null $secondCount
80 | * @param bool $reverse
81 | * @return mixed
82 | */
83 | public function strReplace($file_contents, ?int &$secondCount = 0, $reverse = FALSE) {
84 | if($reverse && $this->searchFor->swap) {
85 | return str_replace(
86 | $this->searchFor->swap,
87 | $this->replaceWith->swap,
88 | $file_contents,
89 | $secondCount
90 | );
91 | } else {
92 | return str_replace(
93 | $this->searchFor->string,
94 | $this->replaceWith->string,
95 | $file_contents,
96 | $secondCount
97 | );
98 | }
99 |
100 | }
101 | }
--------------------------------------------------------------------------------
/replacements.php:
--------------------------------------------------------------------------------
1 | text find.
15 | *
16 | * [searchstring, replacementstring]
17 | *
18 | * @var array
19 | */
20 |
21 | private $default = [
22 | /**
23 | * SELECTORS
24 | */
25 | //$("body") => document.body
26 | ['$("body")', 'document.body'],
27 |
28 | //$("html") => document.documentElement
29 | ['$("html")', 'document.documentElement'],
30 |
31 | //$(selector) => document.querySelector(selector)
32 | //todo
33 | ['$(','document.querySelector('],
34 |
35 | //$elem.find(selector) => elem.querySelector(selector)
36 | ['.find(', '.querySelector('],
37 |
38 | // $elem.closest('.country') => elem.closest('.country')
39 | ['.closest("form")', '.forms'],
40 |
41 | // siblings: $el.prev() and $el.next() => el.previousElementSibling and el.nextElementSibling
42 | ['.prev()', '.previousElementSibling'],
43 | ['.prev("")', '.previousElementSibling'],
44 | ['.next()', '.nextElementSibling'],
45 | ['.next("")', '.nextElementSibling'],
46 | /**
47 | * Input Values: getting and setting
48 | */
49 | //$input.val() => input.value
50 | ['.val()', '.value'],
51 | ['.val("")', '.value'],
52 | //todo
53 | //$input.val("hello") => input.value = "hello"
54 |
55 | /**
56 | * Event Listeners
57 | */
58 | //todo
59 | //$elem.on(eventName, handler) => elem.addEventListener(eventName, handler)
60 | //Note: 3rd arg of false (for useCapture) not needed as not supporting IE8 - see addEventListener docs
61 | //$elem.off(eventName) => elem.removeEventListener(eventName, handler) // note you must keep a ref to handler for this
62 | //Key event listeners: use e.key === "+" instead of e.which === 43
63 | //See key docs
64 | ['.on(', '.addEventListener(' ],
65 | ['.off(', '.removeEventListener(' ],
66 | //Key event listeners: use e.key === "+" instead of e.which === 43 See key docs
67 |
68 | /**
69 | * Event handlers
70 | */
71 | // key events: (e.which === 45) => (e.key === "Tab") - docs
72 |
73 | /**
74 | * Class Manipulation
75 | */
76 | //$elem.addClass(c) => elem.classList.add(c)
77 | ['.addClass(', '.classList.add('],
78 |
79 | //['.removeClass(', '.classList.remove('],
80 | ['.toggleClass(', '.classList.toggle('],
81 |
82 | //$elem.hasClass(c) => elem.classList.contains(c)
83 | ['.hasClass(', '.classList.contains('],
84 |
85 | //$elem.attr('class') = 'some classes' => elem.className = 'some classes'
86 | ['.attr("class")', '.className'],
87 |
88 | /**
89 | * Styling todo: Much MORE ADVANCED
90 | */
91 | // $el.css({ top: "10px" }) => el.style.top = "10px"
92 |
93 | /**
94 | * Scroll Position
95 | */
96 | //$el.scrollTop() => el.scrollTop
97 | ['.scrollTop()', '.scrollTop'],
98 |
99 | //$el.scrollTop(10) => el.scrollTop = 10
100 |
101 | /**
102 | * Utils
103 | */
104 | //$.inArray(item, arr) > -1 => arr.indexOf(item) > -1 // note: could use arr.includes(item) but would require polyfill for IE11
105 |
106 | //$.extend({}, defaults, options) => Object.assign(defaults, options)
107 | //$.trim(s) => s.trim()
108 |
109 | //To append a HTML string:
110 | //$elem.append(htmlString) => elem.insertAdjacentHTML('beforeend', htmlString);
111 |
112 | ['.append(', '.insertAdjacentHTML("beforeend",'],
113 |
114 | //To add copy:
115 | //
116 | //$elem.text(s)
117 | //=>
118 | //
119 | //var elemText = document.createTextNode(text);
120 | //elem.appendChild(elemText)
121 |
122 | //$input.val() => input.value
123 |
124 | /**
125 | * Creating/appending elements
126 | */
127 | //$("", {"class": c}).appendTo(parent); =>
128 | //var elem = document.createElement("div");
129 | //elem.className = c;
130 | //container.appendChild(elem);
131 |
132 | //To add copy:
133 | //
134 | //$elem.text(s)
135 | //=>
136 | //
137 | //var elemText = document.createTextNode(text);
138 | //elem.appendChild(elemText)
139 | //To append a HTML string:
140 | //
141 | //$elem.append(htmlString) => elem.insertAdjacentHTML('beforeend', htmlString);
142 |
143 | /**
144 | * Attributes
145 | */
146 | //$elem.attr("placeholder") => elem.getAttribute("placeholder")
147 | //$elem.attr("placeholder", p) => elem.setAttribute("placeholder", p)
148 |
149 | /**
150 | * Properties
151 | */
152 | ['.props("readonly")', '.readOnly'],
153 | ['.props("disabled")', '.disabled'],
154 |
155 | ];
156 |
157 | /**
158 | * replacements constructor.
159 | *
160 | * Loads all the defaults into the collection.
161 | */
162 | public function __construct() {
163 | foreach($this->default as $value) {
164 | $this->add($value[0], $value[1]);
165 | }
166 | }
167 |
168 |
169 | public function add(string $searchFor, string $replaceWith) {
170 | $this->values[] = new replacement($searchFor, $replaceWith);
171 | }
172 |
173 | }
--------------------------------------------------------------------------------
/swapQuotes.php:
--------------------------------------------------------------------------------
1 | swapStrQuotes($string);
32 | }
33 |
34 | /**
35 | * Change the Quote from double to single, or vice versa. Will Alternate Quotes if more then one.
36 | *
37 | * We "reverse" or "swap" the quotes to handle two different situations. This is used on the replace and search
38 | * strings to reverse the options in case of alternative quotes used in code.
39 | *
40 | * @param $string
41 | * @return string
42 | */
43 | private function swapStrQuotes($string) {
44 | $this->string = $string;
45 | $this->swap = null;
46 |
47 | $found = FALSE;
48 | $searchArray = str_split($string);
49 | foreach($searchArray as &$value) {
50 | $found = self::swapChrQuote($value);
51 | }
52 | $final = implode($searchArray);
53 | if($found && self::DEBUG) {
54 | $this->swap = $final;
55 | echo "Original: $string Result: $final\n";
56 | }
57 | }
58 |
59 | /**
60 | * Turn a single character, quote to its opposite.
61 | *
62 | * @param $value
63 | * @return bool
64 | */
65 | private static function swapChrQuote(&$value) {
66 | if($value == '\'') {
67 | $value = '"';
68 | return TRUE;
69 | } elseif($value == '"') {
70 | $value = "'";
71 | return TRUE;
72 | }
73 | return FALSE;
74 | }
75 |
76 | }
--------------------------------------------------------------------------------
/text-and-replace-javascript.php:
--------------------------------------------------------------------------------
1 | values as $replacement) {
20 | textReplaceAll($replacement);
21 | }
22 |
23 | /**
24 | * Replace all text.
25 | *
26 | * @param \replacement $replacement
27 | * @param string $ext
28 | */
29 | function textReplaceAll(\replacement $replacement, $ext = '*') {
30 | $pathExclude = array();
31 | $pathArray = array();
32 | $pathArray[] = __DIR__ . "/../app/views/";
33 | //$pathArray[] = __DIR__ . "/../app/webpacker/src/javascript/staff/";
34 | //$pathArray[] = __DIR__ . "/../app/webpacker/src/javascript/video_helpers/";
35 | //$pathArray[] = __DIR__ . "/../app/webpacker/src/javascript/webchat/";
36 | $pathExclude[] = __DIR__ . "/../app/views/admins/";
37 |
38 | foreach($pathArray as $key => $path) {
39 | textReplaceAllInPath($replacement, $path, $pathExclude, $ext);
40 | }
41 | }
42 |
43 |
44 | /**
45 | * Text Replace all files in all paths.
46 | *
47 | * @param \replacement $replacement
48 | * @param array $exclusions
49 | * @param string $ext
50 | */
51 | function textReplaceAllInPath(\replacement $replacement, string $path, array $exclusions, $ext = '*') {
52 |
53 | //echo $path.PHP_EOL;
54 | $path = realpath($path); // Path to your textfiles
55 |
56 | //echo $path.PHP_EOL;
57 | $fileList = new RecursiveIteratorIterator(
58 | new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST
59 | );
60 |
61 | //print_r($fileList);
62 | //die();
63 | $count = 0;
64 | $total = iterator_count($fileList);
65 | /** @var ?\SplFileInfo $item */
66 | foreach($fileList as $item) {
67 | if ($item->getFileName() !== '..' && $item->getFileName()!=='.') {
68 | //print_r($item);
69 | $count++;
70 |
71 | $textNrep = new textNrep();
72 | $totalChanged = $textNrep->textReplaceFile($replacement, $exclusions, $ext, $item);
73 | if($totalChanged) {
74 | echo "(Files: $count/$total - $totalChanged) Replacing All ***" . $replacement->searchFor() .
75 | "*** with ***" . $replacement->replaceWith() . "*** in file:" . $item->getPathName() . PHP_EOL;
76 | }
77 | }
78 | }
79 |
80 | }
--------------------------------------------------------------------------------
/textNrep.php:
--------------------------------------------------------------------------------
1 | item = $item;
29 | }
30 | $pathName = $this->item->getPathName();
31 |
32 | $excluded = self::excluded($exclusions, $pathName);
33 |
34 | //echo "excluded: $excluded";
35 | if(!$excluded && $this->item->isFile() && (stripos($pathName, $ext) !== FALSE || $ext == '*')) {
36 |
37 | $file_contents = file_get_contents($pathName);
38 | $return = $this->textReplaceString($replacement, $file_contents);
39 |
40 | } else {
41 | $return = FALSE;
42 | }
43 | if($return) {
44 | file_put_contents($pathName, $file_contents);
45 | }
46 | return $return;
47 | }
48 |
49 | /**
50 | * Go through all of the replacements to perform, and perform the text custom Replacement.
51 | * @param $string
52 | * @return int
53 | */
54 | public function textReplace(&$string) {
55 | $replacements = new replacements();
56 | $totalCount = 0;
57 | foreach($replacements->values as $replacement) {
58 | $totalCount += $this->textReplaceString($replacement, $string);
59 | }
60 | return $totalCount;
61 | }
62 |
63 | /**
64 | * @param $quoteToChange
65 | * @return string
66 | */
67 | private function getOpQuote($quoteToChange) {
68 | if($quoteToChange == '"') {
69 | $replace = "'";
70 | } else {
71 | $replace = '"';
72 | }
73 | return $replace;
74 | }
75 |
76 |
77 | /**
78 | * @param array $exclusions
79 | * @param string $path
80 | * @return bool
81 | */
82 | private static function excluded(array $exclusions, string $path) {
83 | $path = realpath($path);
84 | foreach($exclusions as $exclusion) {
85 | $exclusion = realpath($exclusion);
86 | if(strpos($path, $exclusion) > 0) {
87 | $excluded = TRUE;
88 | //echo "Excluded:$exclusion\n";
89 | return TRUE;
90 | }
91 | }
92 | return FALSE;
93 |
94 | }
95 |
96 |
97 | /**
98 | * Replace both the Regular String and the Reverse String.
99 | *
100 | * @param \replacement $replacement ->searchFor
101 | * @param $jQueryString
102 | * @return int
103 | */
104 | public function textReplaceString(replacement $replacement, &$jQueryString) {
105 | $firstCount = 0;
106 | $jQueryString = $replacement->strReplace($jQueryString, $firstCount);
107 |
108 | $secondCount = 0;
109 | //replace the regular string.
110 | if(strpos($replacement->searchFor(), "'") !== FALSE || strpos($replacement->searchFor(), '"') !== FALSE) {
111 | $jQueryString = $replacement->strReplace($jQueryString, $secondCount, TRUE);
112 | }
113 |
114 | return $firstCount + $secondCount;
115 | }
116 | }
--------------------------------------------------------------------------------