ModuleInstaller::uninstall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Composer\Installers;
6
7
use Exception;
8
use Composer\Package\PackageInterface;
9
use Illuminate\Foundation\Application;
10
use Composer\Repository\InstalledRepositoryInterface;
11
12
class ModuleInstaller extends LibraryInstaller
13
{
14
    /**
15
     * Decides if the installer supports the given type
16
     *
17
     * @param  string $packageType
18
     *
19
     * @return bool
20
     */
21
    public function supports($packageType)
22
    {
23
        return $packageType === 'cortex-module';
24
    }
25
26
    /**
27
     * Returns the installation path of a package
28
     *
29
     * @param  PackageInterface $package
30
     *
31
     * @return string
32
     */
33
    public function getInstallPath(PackageInterface $package)
34
    {
35
        return $this->getPath('app').$package->getPrettyName();
36
    }
37
38
    /**
39
     * Installs specific package.
40
     *
41
     * @param InstalledRepositoryInterface $repo    repository in which to check
42
     * @param PackageInterface             $package package instance
43
     *
44
     * @throws \Exception
45
     *
46
     * @return void
47
     */
48
    public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
49
    {
50
        $this->loadModule($package, true);
51
52
        parent::install($repo, $package);
53
    }
54
55
    /**
56
     * Uninstalls specific package.
57
     *
58
     * @param InstalledRepositoryInterface $repo    repository in which to check
59
     * @param PackageInterface             $package package instance
60
     *
61
     * @throws \Exception
62
     *
63
     * @return void
64
     */
65
    public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
66
    {
67
        $this->loadModule($package, false);
68
69
        parent::uninstall($repo, $package);
70
    }
71
72
    /**
73
     * Autoload installed module.
74
     *
75
     * @param \Composer\Package\PackageInterface $package
76
     * @param bool                               $status
77
     *
78
     * @throws \Exception
79
     *
80
     * @return void
81
     */
82
    protected function loadModule(PackageInterface $package, bool $status): void
83
    {
84
        $laravel = new Application(getcwd());
85
        $modulesManifestPath = $laravel->bootstrapPath('cache'.DIRECTORY_SEPARATOR.'modules.php');
86
        $modulesManifest = file_exists($modulesManifestPath) ? require $modulesManifestPath : [];
87
88
        if ($status && ! in_array($package->getPrettyName(), array_keys($modulesManifest))) {
89
            $modulesManifest = array_merge($modulesManifest, [$package->getPrettyName() => ['active' => false, 'autoload' => true]]);
90
        } else if (! $status) {
91
            unset($modulesManifest[$package->getPrettyName()]);
92
        }
93
94
        if (! is_writable($dirname = dirname($modulesManifestPath))) {
95
            throw new Exception("The {$dirname} directory must be present and writable.");
96
        }
97
98
        $this->replaceFile(
99
            $modulesManifestPath, '<?php return '.var_export($modulesManifest, true).';'
100
        );
101
    }
102
103
    /**
104
     * Write the contents of a file, replacing it atomically if it already exists.
105
     *
106
     * @param  string  $path
107
     * @param  string  $content
108
     *
109
     * @return void
110
     */
111
    protected function replaceFile($path, $content)
112
    {
113
        // If the path already exists and is a symlink, get the real path...
114
        clearstatcache(true, $path);
115
116
        $path = realpath($path) ?: $path;
117
118
        $tempPath = tempnam(dirname($path), basename($path));
119
120
        // Fix permissions of tempPath because `tempnam()` creates it with permissions set to 0600...
121
        chmod($tempPath, 0777 - umask());
122
123
        file_put_contents($tempPath, $content);
124
125
        rename($tempPath, $path);
126
    }
127
}
128