1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; |
4
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\Composer; |
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes; |
7
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Fixes the folder name cases in to make them PSR4 compatible |
11
|
|
|
* e.g. |
12
|
|
|
* yourmodule/src/model becomes yourmodule/src/Model |
13
|
|
|
*/ |
14
|
|
|
class AddPSR4Autoloading extends Task |
15
|
|
|
{ |
16
|
|
|
protected $taskStep = 's50'; |
17
|
|
|
|
18
|
|
|
public function getTitle() |
19
|
|
|
{ |
20
|
|
|
return 'Add PSR-4 Autoloading to the composer file.'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getDescription() |
24
|
|
|
{ |
25
|
|
|
return ' |
26
|
|
|
Goes through all the folders in the code or src dir and adds them to the composer.json file as autoloader. |
27
|
|
|
This must run after the folder names have been changed to CamelCase (see: UpperCaseFolderNamesForPSR4). |
28
|
|
|
'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function runActualTask($params = []): ?string |
32
|
|
|
{ |
33
|
|
|
//project composer.json |
34
|
|
|
// - app/src/... |
35
|
|
|
// - app2/src/... |
36
|
|
|
// DO FOR BOTH |
37
|
|
|
//module composor.json |
38
|
|
|
// ONLY FOR module |
39
|
|
|
$baseCommands = ' |
40
|
|
|
if(! isset($data["autoload"])) { |
41
|
|
|
$data["autoload"] = []; |
42
|
|
|
} |
43
|
|
|
if(! isset($data["autoload"]["psr-4"])) { |
44
|
|
|
$data["autoload"]["psr-4"] = []; |
45
|
|
|
} |
46
|
|
|
'; |
47
|
|
|
$addPage = ''; |
48
|
|
|
if ($this->mu()->getIsModuleUpgrade()) { |
|
|
|
|
49
|
|
|
$addPage = ' |
50
|
|
|
if(! isset($data["autoload"]["files"])) { |
51
|
|
|
$data["autoload"]["files"] = [ |
52
|
|
|
"app/src/Page.php", |
53
|
|
|
"app/src/PageController.php" |
54
|
|
|
]; |
55
|
|
|
}'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$codeDirs = $this->mu()->findNameSpaceAndCodeDirs(); |
|
|
|
|
59
|
|
|
$webRootLocation = $this->mu()->getWebRootDirLocation(); |
|
|
|
|
60
|
|
|
$command = $baseCommands . $addPage; |
61
|
|
|
$comment = 'Adding autoload Page and Page controller details in ' . $webRootLocation . '/composer.json'; |
62
|
|
|
ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine( |
63
|
|
|
$webRootLocation, |
64
|
|
|
$command, |
65
|
|
|
$comment |
66
|
|
|
); |
67
|
|
|
foreach ($codeDirs as $baseNameSpace => $codeDir) { |
68
|
|
|
$location = trim(str_replace($webRootLocation, '', $codeDir), '/') . '/'; |
|
|
|
|
69
|
|
|
//update webroot composer file |
70
|
|
|
//location: |
71
|
|
|
$command = $baseCommands . ' |
72
|
|
|
$data["autoload"]["psr-4"]["' . $this->doubleSlash($baseNameSpace) . '"] = "' . $location . '";'; |
73
|
|
|
$comment = 'Adding autoload psr-4 details in ' . |
74
|
|
|
$webRootLocation . '/composer.json: ' . |
75
|
|
|
$baseNameSpace . ' => ' . $location; |
76
|
|
|
ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine( |
77
|
|
|
$webRootLocation, |
78
|
|
|
$command, |
79
|
|
|
$comment |
80
|
|
|
); |
81
|
|
|
if ($this->mu()->getIsModuleUpgrade()) { |
82
|
|
|
$moduleLocation = dirname($codeDir); |
83
|
|
|
$location = trim(basename($codeDir), '/') . '/'; |
84
|
|
|
$command = $baseCommands . ' |
85
|
|
|
$data["autoload"]["psr-4"]["' . |
86
|
|
|
$this->doubleSlash($baseNameSpace) . '"] = "' . |
87
|
|
|
ltrim($location, '/') . '";'; |
88
|
|
|
$comment = 'Adding autoload psr-4 details in ' . |
89
|
|
|
$moduleLocation . '/composer.json: ' . |
90
|
|
|
$baseNameSpace . ' => ' . $location; |
91
|
|
|
ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine( |
92
|
|
|
$moduleLocation, |
93
|
|
|
$command, |
94
|
|
|
$comment |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
Composer::inst($this->mu()) |
99
|
|
|
->DumpAutoload(); |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function hasCommitAndPush() |
104
|
|
|
{ |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function doubleSlash($str) |
109
|
|
|
{ |
110
|
|
|
return str_replace('\\', '\\\\', $str); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|