Skip to content

Commit da86148

Browse files
Merge branch 'release/v1.2.0'
2 parents 37ab65a + 4d28b34 commit da86148

File tree

3 files changed

+107
-27
lines changed

3 files changed

+107
-27
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
## [1.2.0] - 2019-05-19
10+
11+
### Added
12+
- Ability to remove from the environment user path multiple PHP version paths.
13+
- Ability to remove from the environment user path any PHP version path(s) that are not in the list of available PHP versions.
14+
15+
### Changed
16+
- Updated readme.md
17+
918
## [1.1.1] - 2018-12-23
1019

1120
### Fixed

README.md

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
# WampServer PHP CLI Version Changer
2+
3+
[![GitHub version](https://img.shields.io/github/tag/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?label=WampServer-PHP-CLI-Version-Changer&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/releases)
4+
![Maintained](https://img.shields.io/static/v1.svg?label=maintened&message=yes&color=informational&logo=github)
5+
[![Stars](https://img.shields.io/github/stars/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=brightgreen&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/stargazers)
6+
7+
[![GitHub License](https://img.shields.io/github/license/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=informational&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/blob/master/LICENSE-MIT)
8+
[![GitHub last commit](https://img.shields.io/github/last-commit/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?logo=github)](https://github.com/midnight-coding/grunt-cache-killer/commits/master)
9+
[![GitHub open issues](https://img.shields.io/github/issues-raw/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=brightgreen&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/issues?q=is%3Aopen+is%3Aissue)
10+
[![GitHub closed issues](https://img.shields.io/github/issues-closed-raw/midnight-coding/WampServer-PHP-CLI-Version-Changer.svg?color=brightgreen&logo=github)](https://github.com/midnight-coding/WampServer-PHP-CLI-Version-Changer/issues?q=is%3Aissue+is%3Aclosed)
11+
212
WampServer PHP CLI Version Changer is a Microsoft Windows batch script that allows you to easily change between installed WampServer PHP CLI versions using the users environmental path.
313

14+
## Table Of Contents
15+
16+
* [Minimum Requirements](#minimum-requirements)
17+
* [Installation](#installation)
18+
* [Configuration](#configuration)
19+
* [How To Use](#how-to-use)
20+
* [Text Based User Interface (TUI)](#text-based-user-interface--TUI-)
21+
* [Command Line Interface (CLI)](#command-line-interface--CLI-)
22+
* [FAQ's](#faq-s)
23+
424
## Minimum Requirements
25+
526
The following are required for the CLI Changer script to function correctly.
627
- Microsoft Windows 7 and up.
728
- WampServer v3.0.0 and up.
829

930
## Installation
31+
1032
No installation is required.
1133

12-
At just over 7kB the CLI Changer script is small enough to be saved anywhere in your file system.
34+
At just over 10kB the CLI Changer script is small enough to be saved anywhere in your file system.
1335

1436
**Tip:** Once you have save the CLI Changer script, create a desktop shortcut to it for quick and easy access.
1537

1638
## Configuration
39+
1740
No configuration is necessary if your installed WampServer in its default directory.
1841

1942
The default installation directories are:
@@ -24,7 +47,7 @@ If you installed WampServer in a custom directory, then follow the steps below:
2447

2548
1. Open the CLI Changer script in your preferred text editor.
2649
2. Append your custom install path to the `$customInstallPath` variable.
27-
3. Save the file and close your editor.
50+
3. Save the file and close your text editor.
2851

2952
Example: A customised WampServer install path.
3053
```
@@ -39,12 +62,14 @@ set $customInstallPath=D:\WampServer 64-Bit
3962
**IMPORTANT:** Do not add quotation marks around your custom installation path, even if the path contains spaces.
4063

4164
## How To Use
65+
4266
There are two ways you can use the CLI Changer script.
4367

4468
* Text based user interface (TUI).
4569
* Command line interface (CLI).
4670

4771
### Text Based User Interface (TUI)
72+
4873
Upon running the CLI Changer script you will be presented with a numbered list of installed PHP versions that you can select from.
4974

5075
The exact list of installed PHP CLI versions is dependent on what you currently have in your WampServer PHP addon folder.
@@ -84,6 +109,7 @@ To exit the CLI Changer script without making any changes just press the ENTER k
84109
Should you make an invalid selection or should the selection be the same as the currently selected version then you will receive feedback indicating so.
85110

86111
### Command Line Interface (CLI)
112+
87113
To update the PHP CLI version number directly from the command line, you can pass in the desired PHP version via the scripts first argument. This will bypass the selection screen and go straight to setting the desired version number.
88114

89115
From a CMD prompt:
@@ -108,7 +134,21 @@ Following execution, an exit code will be given:
108134
**Tip:** Calling scripts via the command line is common during (automated) development, testing and deployment. EG: Incorporate it into your build files...
109135

110136
## FAQ's
137+
138+
### How does the environment 'user' and 'system' path variables work?
139+
140+
Environment 'path' variables allow the user (and system) to call an executable without the need to use the executables absolute (full) path. Windows parses the path variables from left to right, with the 'user' path being appended to the 'system' path. (IE: path = system.path + user.path)
141+
142+
When the user (or the user's script) calls the `php.exe` executable, the path pointing to the executable will be used. If an environment path contains two or more paths to a PHP executable, then only the first one found is executed. The other php executables will never be called, ever.
143+
144+
Based on this information and pursuant to the successful selection of a PHP version number, this script scans and then removes any and all reference to any php executable path(s) found within the environment 'user' path prior to appending the selected PHP version path.
145+
146+
The PHP CLI version number returned by typing `php -v` at the command prompt should be the same as that selected by you when using this script. If it is not, there is a strong chance that there is reference to a PHP executable within the environment 'system' path. To correct this situation, you must remove this reference from the environment 'system' path manually.
147+
148+
Both the environment 'user' and 'system' paths can be accessed by clicking 'Start' -> 'Control Panel' -> 'System' -> 'Advanced system settings' -> 'Environment Variables...'
149+
111150
### How do I remove the error at the bottom of the WampServer right-click menu?
151+
112152
As of WampServer v3.1.2 the below error message may be displayed.
113153

114154
```
@@ -137,11 +177,17 @@ Press ENTER to continue...
137177

138178
This error can be suppressed by right-clicking the WampServer icon in the taskbar notification area and selecting: _Wamp Settings -> Do not verify PATH_
139179

140-
**Question 1:** Why would you want to suppress this error? Because currently, WampServer does not have the ability to change the CLI version of PHP should your script(s) require a specific version.
180+
#### Why would you want to suppress this error?
181+
182+
Because currently, WampServer does not have the ability to change the CLI version of PHP should your script(s) require a specific version.
183+
184+
#### But isn't that what right-clicking the WampServer icon in the taskbar notification area and selecting: _Tools -> Change PHP CLI version_ does?
185+
186+
No, it doesn't. This selection currently changes the CLI version that the _WampServer's scripts use_, not what your scripts use when called from the command line.
141187

142-
**Question 2:** But isn't that what right-clicking the WampServer icon in the taskbar notification area and selecting: _Tools -> Change PHP CLI version_ does? No, it doesn't. This selection currently changes the CLI version that the _WampServer's scripts use_, not what your scripts use when called from the command line.
188+
#### So can I safely use this script?
143189

144-
**Question 3:** So can I safely use this script? Currently, yes (but this may change if WampServer decides in the future to begin using either or both of your systems environmental path variables).
190+
Currently, yes (but this may change if WampServer decides in the future to begin using either or both of your systems environmental path variables).
145191

146192
### How do I add more PHP versions?
147193
To add more PHP versions to your WampServer v3 installation visit [SourceForge](https://sourceforge.net/projects/wampserver/files/WampServer%203/WampServer%203.0.0/Addons/Php/).

cli_changer.bat

+47-22
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ rem +------------------------------------------------+
2020
rem ---------------------
2121
rem Default Variables
2222
rem ---------------------
23-
set $scriptVersion=1.1.1
23+
set $scriptVersion=1.2.0
2424

2525
set $defaultInstallPath[0]=C:\wamp
2626
set $defaultInstallPath[1]=C:\wamp64
@@ -77,17 +77,17 @@ rem -------------------
7777
rem PHP Folder Path
7878
rem -------------------
7979

80-
rem Set the $pathToPhpFolders path.
80+
rem Set the path to the PHP folders.
8181
if %$installPath:~-1% neq \ (
8282
set $pathToPhpFolders=%$installPath%\%$pathToPhpFolders%
8383
) else (
8484
set $pathToPhpFolders=%$installPath%%$pathToPhpFolders%
8585
)
8686

87-
rem Check the $pathToPhpFolders path exists.
87+
rem Check the path to the PHP folders exists.
8888
if not exist "%$pathToPhpFolders%" goto invalidPathToPhpFoldersGiven
8989

90-
rem Iterate through folders in the the $pathToPhpFolders path adding them to the availablePhpVersionsArray.
90+
rem Iterate through the folders in the the $pathToPhpFolders variable adding them to the $availablePhpVersionsArray.
9191
set counter=0
9292

9393
for /F "delims=" %%a in ('dir %$pathToPhpFolders% /AD /B') do (
@@ -104,11 +104,11 @@ rem Users Environmental Path
104104
rem ----------------------------
105105

106106
rem Get the users environmental path string.
107-
for /f "usebackq tokens=2,*" %%a in (`reg.exe query HKCU\Environment /v PATH`) do (
107+
for /F "usebackq tokens=2,*" %%a in (`reg.exe query HKCU\Environment /v PATH`) do (
108108
set $usersEnvironmentalPathString=%%b
109109
)
110110

111-
rem Explode the users environmental path string into an array.
111+
rem Using recursion, explode the users environmental path string into an array.
112112
set counter=0
113113

114114
:explode
@@ -128,16 +128,18 @@ rem ----------------------------
128128
rem Match PHP Folder Version
129129
rem ----------------------------
130130

131-
rem Only the first PHP path is used by the computer if more than one PHP path be detected in the users
132-
rem environmental path. Therefore, there is no need to detect multiple PHP paths, only the first one.
131+
rem If there is more than one PHP path in the users environmental path, the operating system
132+
rem will only use the first one. Therefore, we only need to match the first one.
133133
set $currentPhpVersionId=0
134134
set $currentUserEnvPathId=0
135135

136-
rem Loop through the $usersEnvironmentalPathArray.
136+
rem Iterate through the users environmental path array.
137137
for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do (
138-
rem Loop through the $availablePhpVersionsArray.
138+
139+
rem Iterate through the available PHP versions array.
139140
for /L %%b in (1,1,%$lastAvailablePhpVersionsArrayId%) do (
140-
rem Check if the users environmental path string matches the path to the available PHP version string.
141+
142+
rem Check if the users environmental path string matches the (combined) full path of the available PHP version string.
141143
if "!$usersEnvironmentalPathArray[%%a]!"=="%$pathToPhpFolders%\!$availablePhpVersionsArray[%%b]!" (
142144
rem Force the 'for' command parameters into type 'integer'.
143145
set /A $currentPhpVersionId=currentPhpVersionId+%%b
@@ -160,8 +162,10 @@ if "%~1" neq "" (
160162
set $cliMode=1
161163
set $newSelectionId=0
162164

163-
rem Find the $newSelectionId by matching the name to the id.
165+
rem Iterate through the available PHP versions array.
164166
for /L %%a in (1,1,%$lastAvailablePhpVersionsArrayId%) do (
167+
168+
rem If a matching installed PHP folder name is found, set the new selection id.
165169
if "%1"=="!$availablePhpVersionsArray[%%a]!" (
166170
set $newSelectionId=%%a
167171
)
@@ -177,7 +181,7 @@ rem Hack(s)
177181
rem -----------
178182

179183
rem Hack to define a backspace so the 'set /p' command can be offset from the windows edge.
180-
for /f %%a in ('"prompt $H &echo on &for %%b in (1) do rem"') do set backspace=%%a
184+
for /F %%a in ('"prompt $H &echo on &for %%b in (1) do rem"') do set backspace=%%a
181185

182186

183187
rem ------------------------
@@ -193,8 +197,10 @@ echo Available PHP CLI Versions
193197
echo --------------------------
194198
echo:
195199

196-
rem List all installed PHP folder names.
200+
rem Iterate though the available PHP versions array.
197201
for /L %%a in (1,1,%$lastAvailablePhpVersionsArrayId%) do (
202+
203+
rem Check if the listed version matches the current version.
198204
if %%a equ %$currentPhpVersionId% (
199205
echo %%a - !$availablePhpVersionsArray[%%a]! - Current
200206
) else (
@@ -214,7 +220,7 @@ rem --------------------
214220
:checkUserInput
215221

216222
rem Check if the new selection comprises of digits.
217-
echo %$newSelectionId%| findstr /r "^[1-9][0-9]*$">nul
223+
echo %$newSelectionId%| findstr /R "^[1-9][0-9]*$" >nul
218224
if %errorlevel% neq 0 goto invalidSelectionGiven
219225

220226
rem Check if the new selection is a valid selection.
@@ -228,20 +234,39 @@ rem ---------------------------------
228234
rem Update Users Environment Path
229235
rem ---------------------------------
230236

231-
rem Rebuild the $usersEnvironmentalPathString excluding any previously set PHP folder path.
237+
rem Rebuild the users environmental path string excluding any and all previously
238+
rem set PHP folder paths no matter where they are located within the string.
232239
set "$usersEnvironmentalPathString="
233240

241+
rem Iterate through the users environmental path array.
234242
for /L %%a in (1,1,%$lastUsersEnvironmentalPathArrayId%) do (
235-
if !$currentUserEnvPathId! neq %%a (
236-
set $result=!$result!!$usersEnvironmentalPathArray[%%a]!;
243+
244+
rem Remove any trailing slash.
245+
if !$usersEnvironmentalPathArray[%%a]:~-1! equ \ (
246+
set $path=!$usersEnvironmentalPathArray[%%a]:~0,-1!
247+
) else (
248+
set $path=!$usersEnvironmentalPathArray[%%a]!
249+
)
250+
251+
rem Get the last segment of the path.
252+
for %%b in (!$path!) do (
253+
set $segment=%%~nxb
254+
)
255+
256+
rem Check the segment for a matching regex expression. IE: Any PHP folder.
257+
echo !$segment! | findstr /R /C:"^php[1-9][0-9]*\.[0-9][0-9]*\.*[0-9]*[0-9]*" >nul
258+
259+
rem If a match is not found, append the path to the users environmental path string.
260+
if !errorlevel! neq 0 (
261+
set $usersEnvironmentalPathString=!$usersEnvironmentalPathString!!$usersEnvironmentalPathArray[%%a]!;
237262
)
238263
)
239264

240-
rem Add the selected PHP folder path to the end of the $usersEnvironmentalPathString.
241-
set $result=%$result%%$pathToPhpFolders%\!$availablePhpVersionsArray[%$newSelectionId%]!
265+
rem Add the selected PHP folder path to the end of the users environmental path string.
266+
set $usersEnvironmentalPathString=%$usersEnvironmentalPathString%%$pathToPhpFolders%\!$availablePhpVersionsArray[%$newSelectionId%]!
242267

243-
rem Set the $usersEnvironmentalPathString.
244-
setx path "%$result%" >nul
268+
rem Set the users environmental path string.
269+
setx path "%$usersEnvironmentalPathString%" >nul
245270

246271

247272
rem ------------------------------

0 commit comments

Comments
 (0)