-
Notifications
You must be signed in to change notification settings - Fork 120
/
newphp
executable file
·84 lines (75 loc) · 2.52 KB
/
newphp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/local/php70/bin/php
<?php
$prefix = "/usr/local";
$map = [
'bin/phar'=>'bin/phar.phar',
'bin/phar.phar'=>'bin/phar.phar',
'bin/php'=>'bin/php',
'bin/php-cgi'=>'bin/php-cgi',
'bin/php-config'=>'bin/php-config',
'bin/phpize'=>'bin/phpize',
'include/php'=>'include/php',
'lib/php/build'=>'lib/php/build',
'php'=>'php',
'sbin/php-fpm'=>'sbin/php-fpm',
'bin/phpdbg'=>'bin/phpdbg',
'bin/pear'=>'bin/pear',
'bin/peardev'=>'bin/peardev',
'bin/pecl'=>'bin/pecl'
];
if($argc<2) usage();
$debug = $zts = false;
if($argc>2) {
for ($i = 2; $i < $argc; $i++) {
if ($argv[$i] == 'debug') {
$debug = true;
} else if ($argv[$i] == 'zts') {
$zts = true;
} else if ($argv[$i] == 'debugzts') {
$debug = true;
$zts = true;
}
}
}
$version = preg_replace('/[\.\-]+/', '', $argv[1]);
if($version=='7') switch_php('php70', $debug, $zts);
else if($version=='5') switch_php('php56', $debug, $zts);
else if(file_exists("$prefix/$version/bin/php")) switch_php($version, $debug, $zts);
else switch_php('php'.$version, $debug, $zts);
function usage() {
$cmd = basename(__FILE__);
echo "Usage: $cmd 71|70|56|55|54|53 [debug|zts|debugzts]\n";
echo "To switch to PHP 5.5-debug: $cmd 55 debug\n\n";
exit;
}
function switch_php($version, $debug=false, $zts = false) {
global $map, $prefix;
$path = "$prefix/$version";
if($debug) $path .= '-debug';
if($zts) $path .= '-zts';
if(!is_file("$path/bin/php")) {
echo "$path/bin/php doesn't exist\n\n";
usage();
return false;
}
$real_version = trim(shell_exec("$path/bin/php -v | head -1"))
. (($debug && $zts) ? ' (with debug and zts)' : ($debug ? ' (with debug)' : ($zts ? ' (with zts)' : '')));
echo "Activating $real_version and restarting php-fpm\n";
foreach($map as $link=>$target) {
// echo "Creating $prefix/$link => $path/$target\n";
symlink_it("$path/$target", "$prefix/$link");
}
if(substr($version,0,4)=='php7') {
shell_exec("sudo a2dismod php5 && sudo a2disconf php5");
shell_exec("sudo a2enmod php7 && sudo a2enconf php7");
} else {
shell_exec("sudo a2dismod php7 && sudo a2disconf php7");
shell_exec("sudo a2enmod php5 && sudo a2enconf php5");
}
shell_exec("sudo service php-fpm restart");
}
function symlink_it($target, $link) {
// Skip files that have been hard-linked in place
if(file_exists($link) && !is_link($link)) return false;
system("sudo ln -f -s $target $link");
}