Moosh\Command\Generic\Plugin\PluginInstall::execute PHP Method

execute() public method

public execute ( )
    public function execute()
    {
        global $CFG;
        require_once $CFG->libdir . '/adminlib.php';
        // various admin-only functions
        require_once $CFG->libdir . '/upgradelib.php';
        // general upgrade/install related functions
        require_once $CFG->libdir . '/environmentlib.php';
        require_once $CFG->dirroot . '/course/lib.php';
        $this->init();
        $pluginname = $this->arguments[0];
        $pluginversion = $this->arguments[1];
        $downloadurl = $this->get_plugin_url($pluginname, $pluginversion);
        $split = explode('_', $pluginname, 2);
        $type = $split[0];
        $component = $split[1];
        $tempdir = home_dir() . '/.moosh/moodleplugins/';
        $downloadedfile = $tempdir . $component . ".zip";
        if (!file_exists($tempdir)) {
            mkdir($tempdir);
        }
        if (!fopen($downloadedfile, 'w')) {
            echo "Failed to save plugin - check permissions on {$tempdir}.\n";
            return;
        }
        try {
            file_put_contents($downloadedfile, file_get_contents($downloadurl));
        } catch (Exception $e) {
            die("Failed to download plugin from {$downloadurl}. " . $e . "\n");
        }
        $installpath = $this->get_install_path($type);
        $targetpath = $installpath . DIRECTORY_SEPARATOR . $component;
        if (file_exists($targetpath)) {
            if ($this->expandedOptions['delete']) {
                echo "Removing previously installed {$pluginname} from {$targetpath}\n";
                run_external_command("rm -rf {$targetpath}");
            } else {
                die("Something already exists at {$targetpath} - please remove it and try again, or run with the -d option.\n");
            }
        }
        run_external_command("unzip {$downloadedfile} -d {$installpath}");
        run_external_command("rm {$downloadedfile}");
        echo "Installing\n";
        echo "\tname:    {$pluginname}\n";
        echo "\tversion: {$pluginversion}\n";
        upgrade_noncore(true);
        echo "Done\n";
    }