Passed
Branch master (2f7647)
by Nicolaas
04:56 queued 03:20
created

AddPSR4Autoloading::hasCommitAndPush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
6
7
/**
8
 * Fixes the folder name cases in to make them PSR4 compatible
9
 * e.g.
10
 * yourmodule/src/model becomes yourmodule/src/Model
11
 */
12
class AddPSR4Autoloading extends Task
13
{
14
    protected $taskStep = 's50';
15
16
    public function getTitle()
17
    {
18
        return 'Add PSR-4 Autoloading to the composer file.';
19
    }
20
21
    public function getDescription()
22
    {
23
        return '
24
            Goes through all the folders in the code or src dir and adds them to the composer.json file as autoloader.
25
            This must run after the folder names have been changed to CamelCase (see: UpperCaseFolderNamesForPSR4).
26
        ';
27
    }
28
29
    public function runActualTask($params = [])
30
    {
31
        //project composer.json
32
        // - app/src/...
33
        // - app2/src/...
34
        // DO FOR BOTH
35
        //module composor.json
36
        //  ONLY FOR module
37
        $listOfAutoLoads = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $listOfAutoLoads is dead and can be removed.
Loading history...
38
        $baseCommands = '
39
            if(! isset($data["autoload"])) {
40
                $data["autoload"] = [];
41
            }
42
            if(! isset($data["autoload"]["psr-4"])) {
43
                $data["autoload"]["psr-4"] = [];
44
            }
45
        ';
46
        $addPage = '';
47
        if ($this->mu()->getIsModuleUpgrade()) {
0 ignored issues
show
Bug introduced by
The method getIsModuleUpgrade() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
48
            $addPage = '
49
            if(! isset($data["autoload"]["files"])) {
50
                $data["autoload"]["files"] = [
51
                    "app/src/Page.php",
52
                    "app/src/PageController.php"
53
                ];
54
            }';
55
        }
56
57
        $codeDirs = $this->mu()->findNameSpaceAndCodeDirs();
58
        $webRootLocation = $this->mu()->getWebRootDirLocation();
0 ignored issues
show
Bug introduced by
The method getWebRootDirLocation() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $webRootLocation = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
Loading history...
59
        $command = $baseCommands . $addPage;
60
        $comment = 'Adding autoload Page and Page controller details in ' . $webRootLocation . '/composer.json';
61
        $this->updateJSONViaCommandLine(
62
            $webRootLocation,
63
            $command,
64
            $comment
65
        );
66
        foreach ($codeDirs as $baseNameSpace => $codeDir) {
67
            $location = trim(str_replace($webRootLocation, '', $codeDir), '/') . '/';
68
            //update webroot composer file
69
            //location:
70
            $command = $baseCommands . '
71
            $data["autoload"]["psr-4"]["' . $this->doubleSlash($baseNameSpace) . '"] = "' . $location . '";';
72
            $comment = 'Adding autoload psr-4 details in ' . $webRootLocation . '/composer.json: ' . $baseNameSpace . ' => ' . $location;
73
            $this->updateJSONViaCommandLine(
74
                $webRootLocation,
75
                $command,
76
                $comment
77
            );
78
            if ($this->mu()->getIsModuleUpgrade()) {
79
                $moduleLocation = dirname($codeDir);
80
                $location = trim(basename($codeDir), '/') . '/';
81
                $command = $baseCommands . '
82
                $data["autoload"]["psr-4"]["' . $this->doubleSlash($baseNameSpace) . '"] = "' . ltrim($location, '/') . '";';
83
                $comment = 'Adding autoload psr-4 details in ' . $moduleLocation . '/composer.json: ' . $baseNameSpace . ' => ' . $location;
84
                $this->updateJSONViaCommandLine(
85
                    $moduleLocation,
86
                    $command,
87
                    $comment
88
                );
89
            }
90
        }
91
        $this->mu()->execMe(
92
            $this->mu()->getWebRootDirLocation(),
93
            'composer dumpautoload',
94
            'run composer dumpautoload',
95
            false
96
        );
97
    }
98
99
    protected function hasCommitAndPush()
100
    {
101
        return true;
102
    }
103
104
    protected function doubleSlash($str)
105
    {
106
        return str_replace('\\', '\\\\', $str);
107
    }
108
}
109