1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; |
4
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Api\SearchAndReplaceAPI; |
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; |
7
|
|
|
|
8
|
|
|
class FixTemplateIncludeStatements extends Task |
9
|
|
|
{ |
10
|
|
|
protected $taskStep = 's30'; |
11
|
|
|
|
12
|
|
|
protected $debug = false; |
13
|
|
|
|
14
|
|
|
private $extensionArray = [ |
15
|
|
|
'ss', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
private $findArray = [ |
19
|
|
|
'<% include ', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
public function getTitle() |
23
|
|
|
{ |
24
|
|
|
return 'Add Namespacing to include statements in templates.'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getDescription() |
28
|
|
|
{ |
29
|
|
|
return ' |
30
|
|
|
Go through all templates and fix the include statements by adding namespacing e.g. % include Order % becomes % include Sunnysideup\Ecommerce\Includes\Order %.'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setExtensionArray($a) |
34
|
|
|
{ |
35
|
|
|
$this->extensionArray = $a; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setFindArray($a) |
41
|
|
|
{ |
42
|
|
|
$this->findArray = $a; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function runActualTask($params = []): ?string |
48
|
|
|
{ |
49
|
|
|
$codeDirs = $this->mu()->findNameSpaceAndCodeDirs(); |
|
|
|
|
50
|
|
|
$caseSensitive = false; |
51
|
|
|
foreach ($codeDirs as $baseNameSpace => $codeDir) { |
52
|
|
|
$baseDir = dirname($codeDir); |
53
|
|
|
//Start search machine from the module location. replace API |
54
|
|
|
$textSearchMachine = new SearchAndReplaceAPI($baseDir); |
55
|
|
|
$textSearchMachine->setIsReplacingEnabled(true); |
56
|
|
|
// $textSearchMachine->setFileNameMustContain('<% include'); |
57
|
|
|
$this->mu()->colourPrint("Checking ${baseDir}"); |
|
|
|
|
58
|
|
|
$baseDir = $this->mu()->checkIfPathExistsAndCleanItUp($baseDir); |
|
|
|
|
59
|
|
|
if (! file_exists($baseDir)) { |
60
|
|
|
$this->mu()->colourPrint("SKIPPING ${baseDir} as it does not exist."); |
61
|
|
|
} else { |
62
|
|
|
$textSearchMachine->setSearchPath($baseDir); |
63
|
|
|
$textSearchMachine->setExtensions($this->extensionArray); //setting extensions to search files within |
64
|
|
|
$this->mu()->colourPrint( |
65
|
|
|
"++++++++++++++++++++++++++++++++++++\n" . |
66
|
|
|
"CHECKING\n" . |
67
|
|
|
"IN ${baseDir}\n" . |
68
|
|
|
'FOR ' . implode(',', $this->extensionArray) . " FILES\n" . |
69
|
|
|
'BASE ' . $baseDir . "\n" . |
70
|
|
|
"++++++++++++++++++++++++++++++++++++\n" |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
foreach ($this->findArray as $finalFind) { |
74
|
|
|
$replacementType = 'BASIC'; |
75
|
|
|
$finalReplace = '<% include ' . $baseNameSpace . '\Includes'; |
76
|
|
|
$this->mu()->colourPrint( |
77
|
|
|
' --- FIND: ' . $finalFind . "\n" . |
78
|
|
|
' --- REPLACE: ' . $finalReplace . "\n" |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType); |
82
|
|
|
$textSearchMachine->setReplacementKey($finalReplace); |
83
|
|
|
// $textSearchMachine->setComment($comment); |
84
|
|
|
$textSearchMachine->startSearchAndReplace(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//SHOW TOTALS |
88
|
|
|
$replacements = $textSearchMachine->showFormattedSearchTotals(); |
89
|
|
|
if (! $replacements) { |
90
|
|
|
//flush output anyway! |
91
|
|
|
$this->mu()->colourPrint('No replacements for ' . implode(',', $this->extensionArray)); |
92
|
|
|
} |
93
|
|
|
$this->mu()->colourPrint($textSearchMachine->getOutput()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function hasCommitAndPush() |
100
|
|
|
{ |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|