3 namespace pharext\Task
;
13 class Activate
implements Task
41 * @param string $cwd working directory
42 * @param array $inis custom INI or list of loaded/scanned INI files
43 * @param string $type extension or zend_extension
44 * @param string $prefix install prefix, e.g. /usr/local
45 * @param string $common_name PHP programs common name, e.g. php5
46 * @param string $sudo sudo command
47 * @throws \pharext\Exception
49 public function __construct($cwd, array $inis, $type = "extension", $prefix = null, $common_name = "php", $sudo = null) {
53 if (!$this->inis
= $inis) {
54 throw new Exception("No PHP INIs given");
56 $cmd = $common_name . "-config";
58 $cmd = $prefix . "/bin/" . $cmd;
60 $this->php_config
= $cmd;
64 * @param bool $verbose
65 * @return boolean false, if extension was already activated
67 public function run($verbose = false) {
68 if ($verbose !== false) {
69 printf("Running INI activation ...\n");
71 $extension = basename(current(glob("{$this->cwd}/modules/*.so")));
73 if ($this->type
=== "zend_extension") {
74 $pattern = preg_quote((new ExecCmd($this->php_config
))->run(["--extension-dir"])->getOutput() . "/$extension", "/");
76 $pattern = preg_quote($extension, "/");
79 foreach ($this->inis
as $file) {
81 printf("Checking %s ...\n", $file);
83 $temp = new Tempfile("phpini");
84 foreach (file($file) as $line) {
85 if (preg_match("/^\s*{$this->type}\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
88 fwrite($temp->getStream(), $line);
92 /* not found; append to last processed file, which is the main by default */
94 printf("Activating in %s ...\n", $file);
96 fprintf($temp->getStream(), $this->type
. "=%s\n", $extension);
99 $path = $temp->getPathname();
103 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
104 $cmd = new ExecCmd("chown", $verbose);
105 if (isset($this->sudo
)) {
106 $cmd->setSu($this->sudo
);
108 $cmd->run([$ugid, $path]);
110 // permission transfer
111 $perm = decoct($stat["mode"] & 0777);
112 $cmd = new ExecCmd("chmod", $verbose);
113 if (isset($this->sudo
)) {
114 $cmd->setSu($this->sudo
);
116 $cmd->run([$perm, $path]);
119 $cmd = new ExecCmd("mv", $verbose);
120 if (isset($this->sudo
)) {
121 $cmd->setSu($this->sudo
);
123 $cmd->run([$path, $file]);
126 printf("Replaced %s ...\n", $file);