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 $extension = basename(current(glob("{$this->cwd}/modules/*.so")));
70 if ($this->type
=== "zend_extension") {
71 $pattern = preg_quote((new ExecCmd($this->php_config
))->run(["--extension-dir"])->getOutput() . "/$extension", "/");
73 $pattern = preg_quote($extension, "/");
76 foreach ($this->inis
as $file) {
77 $temp = new Tempfile("phpini");
78 foreach (file($file) as $line) {
79 if (preg_match("/^\s*{$this->type}\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
82 fwrite($temp->getStream(), $line);
86 /* not found; append to last processed file, which is the main by default */
87 fprintf($temp->getStream(), $this->type
. "=%s\n", $extension);
90 $path = $temp->getPathname();
94 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
95 $cmd = new ExecCmd("chown", $verbose);
96 if (isset($this->sudo
)) {
97 $cmd->setSu($this->sudo
);
99 $cmd->run([$ugid, $path]);
101 // permission transfer
102 $perm = decoct($stat["mode"] & 0777);
103 $cmd = new ExecCmd("chmod", $verbose);
104 if (isset($this->sudo
)) {
105 $cmd->setSu($this->sudo
);
107 $cmd->run([$perm, $path]);
110 $cmd = new ExecCmd("mv", $verbose);
111 if (isset($this->sudo
)) {
112 $cmd->setSu($this->sudo
);
114 $cmd->run([$path, $file]);