pharext\Task\Activate::run PHP Method

run() public method

public run ( boolean $verbose = false ) : boolean
$verbose boolean
return boolean false, if extension was already activated
    public function run($verbose = false)
    {
        if ($verbose !== false) {
            printf("Running INI activation ...\n");
        }
        $extension = basename(current(glob("{$this->cwd}/modules/*.so")));
        if ($this->type === "zend_extension") {
            $pattern = preg_quote((new ExecCmd($this->php_config))->run(["--extension-dir"])->getOutput() . "/{$extension}", "/");
        } else {
            $pattern = preg_quote($extension, "/");
        }
        foreach ($this->inis as $file) {
            if ($verbose) {
                printf("Checking %s ...\n", $file);
            }
            if (!file_exists($file)) {
                throw new Exception(sprintf("INI file '%s' does not exist", $file));
            }
            $temp = new Tempfile("phpini");
            foreach (file($file) as $line) {
                if (preg_match("/^\\s*{$this->type}\\s*=\\s*[\"']?{$pattern}[\"']?\\s*(;.*)?\$/", $line)) {
                    return false;
                }
                fwrite($temp->getStream(), $line);
            }
        }
        /* not found; append to last processed file, which is the main by default */
        if ($verbose) {
            printf("Activating in %s ...\n", $file);
        }
        fprintf($temp->getStream(), $this->type . "=%s\n", $extension);
        $temp->closeStream();
        $path = $temp->getPathname();
        $stat = stat($file);
        // owner transfer
        $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
        $cmd = new ExecCmd("chown", $verbose);
        if (isset($this->sudo)) {
            $cmd->setSu($this->sudo);
        }
        $cmd->run([$ugid, $path]);
        // permission transfer
        $perm = decoct($stat["mode"] & 0777);
        $cmd = new ExecCmd("chmod", $verbose);
        if (isset($this->sudo)) {
            $cmd->setSu($this->sudo);
        }
        $cmd->run([$perm, $path]);
        // rename
        $cmd = new ExecCmd("mv", $verbose);
        if (isset($this->sudo)) {
            $cmd->setSu($this->sudo);
        }
        $cmd->run([$path, $file]);
        if ($verbose) {
            printf("Replaced %s ...\n", $file);
        }
        return true;
    }

Usage Example

示例#1
0
 private function activate($temp)
 {
     if ($this->args->ini) {
         $files = [$this->args->ini];
     } else {
         $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
         $files[] = php_ini_loaded_file();
     }
     $sudo = isset($this->args->sudo) ? $this->args->sudo : null;
     $type = $this->metadata("type") ?: "extension";
     $activate = new Task\Activate($temp, $files, $type, $this->args->prefix, $this->args["common-name"], $sudo);
     if (!$activate->run($this->verbosity())) {
         $this->info("Extension already activated ...\n");
     }
 }