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 if (!file_exists($file)) {
84 throw new Exception(sprintf("INI file '%s' does not exist", $file));
86 $temp = new Tempfile("phpini");
87 foreach (file($file) as $line) {
88 if (preg_match("/^\s*{$this->type}\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
91 fwrite($temp->getStream(), $line);
95 /* not found; append to last processed file, which is the main by default */
97 printf("Activating in %s ...\n", $file);
99 fprintf($temp->getStream(), $this->type
. "=%s\n", $extension);
100 $temp->closeStream();
102 $path = $temp->getPathname();
106 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
107 $cmd = new ExecCmd("chown", $verbose);
108 if (isset($this->sudo
)) {
109 $cmd->setSu($this->sudo
);
111 $cmd->run([$ugid, $path]);
113 // permission transfer
114 $perm = decoct($stat["mode"] & 0777);
115 $cmd = new ExecCmd("chmod", $verbose);
116 if (isset($this->sudo
)) {
117 $cmd->setSu($this->sudo
);
119 $cmd->run([$perm, $path]);
122 $cmd = new ExecCmd("mv", $verbose);
123 if (isset($this->sudo
)) {
124 $cmd->setSu($this->sudo
);
126 $cmd->run([$path, $file]);
129 printf("Replaced %s ...\n", $file);