In a GUI-driven operating system like Windows, where you don't hang around the command line that much, it is sometimes useful to run a PHP file from its context menu in Windows Explorer.
That's easy to set up. But a reasonably smart handler should perhaps not hand the file over to PHP blindly. PHPUnit test cases need a different treatment, so the handler could try to detect them and pass them on to PHPUnit instead.
Here's a simple script, php-cli.bat, that does just that. Test files are identified through convention, not introspection: If the filename ends in "...test.php", PHPUnit will get to handle it. All other files are passed on to PHP. Both PHP and PHPUnit are expected to be in your PATH.
This is what the script does:
@REM PHP CLI runner, incorporating a PHPUnit test runner.
@REM o Expects a filepath as first argument.
@REM o An ordinary file will be passed on to PHP. If the file is a test, it
@REM will be run through PHPUnit.
@REM o Test files are identified by their basename ending in 'Test'.
@REM o PHP and PHPUnit must be found in the PATH.
@echo off
set separator=________________________________________________________________________________
@REM Get the filename as basename without extension
set filename=%~n1
set filepath=%~f1
set scriptdir=%~dp1
cd /d %scriptdir%
@REM check if the filename ends in "Test"
set suffix=%filename:~-4,4%
if "%suffix%" == "Test" GOTO :IS_TESTFILE
@REM not a testfile, run through PHP
echo Running %filepath% ...
echo %separator%
echo.
php %*
echo.
@REM keep the window open after PHP has run
cmd /K
GOTO :END
:IS_TESTFILE
@REM this is a testfile, use PHPUnit
echo Running tests in %filepath% ...
echo %separator%
echo.
START /B phpunit %*
:END
In case you wonder about the %~n1
oddities, these are actually %1
placeholders with modifiers, whereas set suffix=%filename:~-4,4%
uses a directive for string manipulation.
Setting it up
There are several ways to add the batch script to the context menu of .php files.
In the "Open with ..." flyout menu (Windows 7)
Just put the batch file wherever you like it to be "installed". Next, right-click on any PHP file, select "Open with ..." > "Choose default program" > "Browse" and pick the script. Before you hit OK, don't forget to uncheck "Always use the selected program to open this kind of file" unless that's really what you want.
As soon as this is done, php-cli.bat will become a permanent option in the "Open with" list for PHP files.
At the top level of the context menu (Windows 7)
You'll have to edit the Registry for that. (Disclaimer: Don't do that unless you know what you are doing, are aware of the risks involved and take adequate precautions.)
Assuming you have registered .php files before as a PHPFile type (or some app has done it for you),
- open HKEY_CLASSES_ROOT\PHPFile\shell\
- Add a subkey named "Run with PHP".
- Set the default value to the text you want to appear in the context menu, e.g. "Run with PHP"
- Inside the new key, add a subkey named "Command"
- Set the default value to
"C:\path\to\php-cli.bat" "%1" %*
(including the quotes), e.g. "C:\Program Files (x86)\php-extras\php-cli.bat" "%1" %*
And you are done. If you are interested, a generic description of registering a context menu handler is available at MSDN in a basic and more elaborate version.
Other Windows flavours
In Windows Vista, the procedure should be roughly the same (I have not tried it, though). If you are still using Windows XP, adding an entry to the context menu works a little differently.
Download php-cli.bat
"cooool thx for sharing!!!!!", haha. Yeah this is a nice script. I don't have any need for phpunit, but it's nice to use for debugging php cron scripts(especially long ones). The reason I find this much more useful than the browser, is because in the browser it buffers the stdout until the page is done, then flushes it all to the page. If you run it from the command line you get every echo as it happens. You can use flush(), but if you plan on adding header data down the road in the script, it'll break that. Anyways thanks, this helps.
Good point. Glad you find it useful.
Comments are disabled.
Comments have primarily been disabled because of a flood of comment spam. Turning them off has also been an easy way to comply with EU privacy and data protection regulations. User nicknames have been replaced by anonymous placeholders. All data relating to the original commenters has been deleted.