forked from damiankloip/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_executables
More file actions
210 lines (154 loc) · 5.83 KB
/
php_executables
File metadata and controls
210 lines (154 loc) · 5.83 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env bash
# Wrapper for php and drush executables to dynamically switch between
# versions of php based on project configuration.
#
# Your PHP project should either have a .platform.app.yaml or
# .php-version file in the root.
#
# The project configuration file should have a line somewhere in the
# file like one of the following:
#
# type: php:5.5
# type: php:5.6
# type: php:7.1
# type: 5.5
# type: 5.6
# type: 7.1
# type: hhvm
# Define some default values for Ubuntu 14.04 which has hhvm installed
# through Ubuntu's default repositories and php 5.5, 5.6, 7.1 with:
# https://launchpad.net/~ondrej/+archive/ubuntu/php
# If these defaults need to be overriden, don't edit this file.
# Instead create a file at: ~/.php_settings and copy/paste the settings
# between the ## START SETTINGS ## and ## END SETTINGS ## lines or just
# the lines you wish to override. There is a condition to check the
# major release version of Ubuntu as 16.04 is typically just for PHP7.
declare -A php_settings_exec
declare -A php_settings_options
## START SETTINGS - can be overriden in ~/.php_settings ##
if [ "$(lsb_release -r -s | cut -d'.' -f1)" -ge "16" ]; then
php_settings_exec[7.1]='/usr/bin/php'
php_settings_options[7.1]='-c /etc/php/7.1/cli/php.ini'
else
php_settings_exec[5.5]='/usr/bin/php5'
php_settings_options[5.5]='-c /etc/php5/cli/php.ini'
php_settings_exec[5.6]='/usr/bin/php5.6'
php_settings_options[5.6]='-c /etc/php/5.6/cli/php.ini'
php_settings_exec[7.1]='/usr/bin/php7.1'
php_settings_options[7.1]='-c /etc/php/7.1/cli/php.ini'
php_settings_exec[hhvm]='/usr/bin/hhvm'
php_settings_options[hhvm]='-c /etc/hhvm/php.ini'
fi
## END SETTINGS ##
# Allow overriding of PHP settings exectuables and options.
if [ -f "$HOME/.php_settings" ]; then
source "$HOME/.php_settings"
fi
php_executables () {
# Define fallback PHP version is no config file is found.
if [ "$(lsb_release -r -s | cut -d'.' -f1)" -ge "16" ]; then
php_version_default="7.1"
else
php_version_default="5.6"
fi
# Define PHP version files.
declare -A check_php_version_files
check_php_version_files[platformsh]=.platform.app.yaml
check_php_version_files[phpversion]=.php-version
show_debug=true
php_version_pwd="`pwd`"
php_version=""
dir_parts=$(echo $php_version_pwd | tr "/" "\n")
for check_php_version_file in "${check_php_version_files[@]}"; do
check_dir_composite=""
for part in $dir_parts; do
check_dir_composite=$check_dir_composite/$part
if [ -f "$check_dir_composite/$check_php_version_file" ]; then
php_definition_file="$check_dir_composite/$check_php_version_file"
php_version=`grep -E "type: (.*)" "$php_definition_file" | cut -d: -f2- | sed "s/'//g" | sed "s/php://g" | tr -d "[[:space:]]"`
break
fi
done
done
if [ -z $php_version ]; then
# If php version isn't found then fallback to default.
php_version=$php_version_default
if [ "$show_debug" == true ]; then
echo -e "Using default PHP version: "$php_version_default"\n"
fi
else
if [ "$show_debug" == true ]; then
echo -e "Using PHP version "$php_version" as defined in "$php_definition_file
fi
fi
# Define the php executable and runtime options (notably the php.ini
# file location) to pass to the php executable from the php settings
# arrays.
php_exec=${php_settings_exec[$php_version]}
php_options=${php_settings_options[$php_version]}
if [ "$show_debug" == true ]; then
echo -e "PHP executable and options: "$php_exec" "$php_options
fi
# Capture the first argument as the executable to run.
php_command=$1
# Remove the first argument ready for passing to executable.
shift
# Execute php command.
if [ "$php_command" = "php" ]; then
echo -e "$php_exec $php_options $@" | bash -i
fi
# Execute drush or drupal console command.
if [ "$php_command" = "drush" ] || [ "$php_command" = "drupal" ]; then
# cd into child directory (working Drupal install) if found.
if [ -d web ]; then
cd web
cd_child_dir=true
elif [ -d www ]; then
cd www
cd_child_dir=true
elif [ -d docroot ]; then
cd docroot
cd_child_dir=true
else
cd_child_dir=false
fi
if [ "$php_command" = "drush" ]; then
# Define the drush executable - assumed to be in the $PATH.
drush_exec="`which drush`"
# See: https://www.drupal.org/node/1302418
DRUSH_PHP="$php_exec" "$php_exec" "$php_options" "$drush_exec" "$@"
elif [ "$php_command" = "drupal" ]; then
# Discover the most appropriate drupal executable.
parentdir=`dirname $PWD`
if [ -f $parentdir"/vendor/bin/drupal" ]; then
# In a document root where the parent folder contains a vendor
# directory containing the drupal console executable installed
# with composer.
drupal_exec=$parentdir"/vendor/bin/drupal"
elif [ -f $PWD"/vendor/bin/drupal" ]; then
# In a document root where the current folder contains a vendor
# directory containing the drupal console executable installed
# with composer.
drupal_exec=$PWD"/vendor/bin/drupal"
elif [ -f "`which drupal`" ]; then
# Otherwise assumed to be in the $PATH.
drupal_exec="`which drupal`"
else
echo -e 'Discover failed for drupal console executable discovery'
fi
if [ "$show_debug" == true ]; then
echo -e "Using drupal console executable at: "$drupal_exec
fi
# Execute command.
echo -e "$php_exec $php_options $drupal_exec $@" | bash -i
fi
# cd back to parent directory if drush is run from within a child directory.
if [ $cd_child_dir == true ]; then
cd ..
fi
fi
}
# Add php aliases which use the php_executables function.
alias php="php_executables php"
alias drush="php_executables drush"
alias drupal="php_executables drupal"