ModuleUninstallCommand.php 3.6 KB
Newer Older
S
develop  
server 已提交
1 2 3 4 5
<?php

namespace ModStart\Command;

use Illuminate\Console\Command;
ModStart's avatar
develop  
ModStart 已提交
6 7
use Illuminate\Support\Facades\Event;
use ModStart\Core\Events\ModuleUninstalledEvent;
S
develop  
server 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
use ModStart\Core\Exception\BizException;
use ModStart\Core\Input\Response;
use ModStart\Core\Util\FileUtil;
use ModStart\ModStart;
use ModStart\Module\ModuleManager;

class ModuleUninstallCommand extends Command
{
    protected $signature = 'modstart:module-uninstall {module}';

    public function handle()
    {
        $module = $this->argument('module');
ModStart's avatar
develop  
ModStart 已提交
21
        $isExists = ModuleManager::isExists($module);
S
develop  
server 已提交
22
        $installeds = ModuleManager::listAllInstalledModules();
ModStart's avatar
develop  
ModStart 已提交
23 24 25 26 27 28 29 30 31 32 33
        if ($isExists) {
            BizException::throwsIf(L('Module not installed'), !isset($installeds[$module]));
            foreach ($installeds as $one => $_) {
                $basic = ModuleManager::getModuleBasic($one);
                BizException::throwsIf('Module[' . $one . '] config empty', !$basic);
                if (in_array($module, $basic['require'])) {
                    return Response::generateError(L('Module %s depend on %s, uninstall fail', $one, $module));
                }
            }
            if (method_exists(ModuleManager::class, 'callHook')) {
                ModuleManager::callHook($module, 'hookBeforeUninstall');
S
develop  
server 已提交
34 35 36
            }
        }
        unset($installeds[$module]);
ModStart's avatar
develop  
ModStart 已提交
37
        $this->unPublishAsset($module);
S
develop  
server 已提交
38 39 40 41 42
        $this->unPublishRoot($module);

        ModuleManager::saveUserInstalledModules($installeds);

        ModStart::clearCache();
ModStart's avatar
develop  
ModStart 已提交
43 44 45 46

        $event = new ModuleUninstalledEvent();
        $event->name = $module;
        Event::fire($event);
S
develop  
server 已提交
47 48
    }

ModStart's avatar
develop  
ModStart 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    private function unPublishAsset($module)
    {
        $fs = $this->laravel['files'];
        $from = ModuleManager::path($module, 'Asset') . '/';
        if (!file_exists($from)) {
            return;
        }
        $to = public_path("vendor/$module/");
        if (!file_exists($to)) {
            return;
        }
        $fs->deleteDirectory($to);
        $this->info("Module Asset UnPublish : $to");
    }

S
develop  
server 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    private function unPublishRoot($module)
    {
        $root = ModuleManager::path($module, 'ROOT');
        if (!file_exists($root)) {
            return;
        }
        $files = FileUtil::listAllFiles($root);
        $files = array_filter($files, function ($file) {
            return $file['isFile'];
        });
        $publishFiles = 0;
        foreach ($files as $file) {
            $relativePath = $file['filename'];
            $relativePathBackup = $relativePath . '._delete_.' . $module;
            $currentFile = base_path($relativePath);
            $currentFileBackup = $currentFile . '._delete_.' . $module;
S
develop  
server 已提交
80 81 82
            if (!file_exists($currentFile)) {
                continue;
            }
S
develop  
server 已提交
83 84 85 86 87 88 89
            if (
                (!file_exists($currentFileBackup) && md5_file($currentFile) == md5_file($file['pathname']))
                ||
                (file_exists($currentFileBackup))
            ) {
                unlink($currentFile);
                if (file_exists($currentFileBackup)) {
S
develop  
server 已提交
90 91 92 93 94 95 96 97
                    $content = trim(file_get_contents($currentFileBackup));
                    if ('__MODSTART_EMPTY_FILE__' == $content) {
                        unlink($currentFileBackup);
                        $this->info("Module Root Publish : $relativePath");
                    } else {
                        rename($currentFileBackup, $currentFile);
                        $this->info("Module Root Publish : $relativePath <- $relativePathBackup");
                    }
S
develop  
server 已提交
98 99 100 101 102 103 104 105
                }
                $publishFiles++;
            }
        }
        $this->info("Module Root UnPublish : $publishFiles item(s)");
    }

}