|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; |
|
4
|
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Api\SearchAndReplaceAPI; |
|
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Replaces a bunch of code snippets in preparation of the upgrade. |
|
10
|
|
|
* Controversial replacements will be replaced with a comment |
|
11
|
|
|
* next to it so you can review replacements easily. |
|
12
|
|
|
*/ |
|
13
|
|
|
class AddTableName extends Task |
|
14
|
|
|
{ |
|
15
|
|
|
protected $taskStep = 's10'; |
|
16
|
|
|
|
|
17
|
|
|
protected $debug = false; |
|
18
|
|
|
|
|
19
|
|
|
private $ignoreFolderArray = [ |
|
20
|
|
|
'extensions', |
|
21
|
|
|
'Extensions', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
private $extensionArray = [ |
|
25
|
|
|
'php', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
private $findArray = [ |
|
29
|
|
|
'private static $db', |
|
30
|
|
|
'private static $has_one' . |
|
31
|
|
|
'private static $belongs_to =', |
|
32
|
|
|
'private static $has_many =', |
|
33
|
|
|
'private static $many_many =', |
|
34
|
|
|
'private static $belongs_many_many =', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
public function getTitle() |
|
38
|
|
|
{ |
|
39
|
|
|
return 'Add the table name '; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getDescription() |
|
43
|
|
|
{ |
|
44
|
|
|
return ' |
|
45
|
|
|
Finds $db and $has_one and adds the private static $table_name for the class...'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function setExtensionArray($a) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->extensionArray = $a; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setFindArray($a) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->findArray = $a; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function runActualTask($params = []): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { |
|
|
|
|
|
|
65
|
|
|
$moduleDir = $this->mu()->findMyCodeDir($moduleDir); |
|
|
|
|
|
|
66
|
|
|
//Start search machine from the module location. replace API |
|
67
|
|
|
$textSearchMachine = new SearchAndReplaceAPI($moduleDir); |
|
68
|
|
|
$textSearchMachine->setIsReplacingEnabled(true); |
|
69
|
|
|
$textSearchMachine->setFileReplacementMaxCount(1); |
|
70
|
|
|
$textSearchMachine->setIgnoreFileIfFound(['private static $table_name']); |
|
71
|
|
|
$textSearchMachine->addToIgnoreFolderArray($this->ignoreFolderArray); |
|
72
|
|
|
$this->mu()->colourPrint("Checking ${moduleDir}"); |
|
|
|
|
|
|
73
|
|
|
$moduleDir = $this->mu()->checkIfPathExistsAndCleanItUp($moduleDir); |
|
|
|
|
|
|
74
|
|
|
if (! file_exists($moduleDir)) { |
|
75
|
|
|
$this->mu()->colourPrint("SKIPPING ${moduleDir} as it does not exist."); |
|
76
|
|
|
} else { |
|
77
|
|
|
$textSearchMachine->setSearchPath($moduleDir); |
|
78
|
|
|
$textSearchMachine->setExtensions($this->extensionArray); //setting extensions to search files within |
|
79
|
|
|
$this->mu()->colourPrint( |
|
80
|
|
|
"++++++++++++++++++++++++++++++++++++\n" . |
|
81
|
|
|
"CHECKING\n" . |
|
82
|
|
|
"IN ${moduleDir}\n" . |
|
83
|
|
|
'FOR ' . implode(',', $this->extensionArray) . " FILES\n" . |
|
84
|
|
|
'BASE ' . $moduleDir . "\n" . |
|
85
|
|
|
"++++++++++++++++++++++++++++++++++++\n" |
|
86
|
|
|
); |
|
87
|
|
|
foreach ($this->findArray as $finalFind) { |
|
88
|
|
|
$caseSensitive = true; |
|
89
|
|
|
$replacementType = 'COMPLEX'; |
|
90
|
|
|
$comment = 'Check that is class indeed extends DataObject and that it is not a data-extension!'; |
|
91
|
|
|
$finalReplace = ' |
|
92
|
|
|
private static $table_name = \'[SEARCH_REPLACE_CLASS_NAME_GOES_HERE]\'; |
|
93
|
|
|
|
|
94
|
|
|
' . $finalFind; |
|
95
|
|
|
$this->mu()->colourPrint( |
|
96
|
|
|
' --- FIND: ' . $finalFind . "\n" . |
|
97
|
|
|
' --- REPLACE: ' . $finalReplace . "\n" |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType); |
|
101
|
|
|
$textSearchMachine->setReplacementKey($finalReplace); |
|
102
|
|
|
$textSearchMachine->setComment($comment); |
|
103
|
|
|
$textSearchMachine->startSearchAndReplace(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
//SHOW TOTALS |
|
107
|
|
|
$replacements = $textSearchMachine->showFormattedSearchTotals(); |
|
108
|
|
|
if (! $replacements) { |
|
109
|
|
|
//flush output anyway! |
|
110
|
|
|
$this->mu()->colourPrint('No replacements for ' . implode(',', $this->extensionArray)); |
|
111
|
|
|
} |
|
112
|
|
|
$this->mu()->colourPrint($textSearchMachine->getOutput()); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function hasCommitAndPush() |
|
119
|
|
|
{ |
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|