I'm trying to run composer on windows with wamp. I installed composer using the cmd prompt, and now I'm trying to run "composer update" for an SDK. However, when I type in "composer.phar update," windows asks what app I want to use to run this program. I want the command prompt to deal with it! How do I just run it through cmd, without this "what app" window coming up?
4 Answers
You have to set php.exe as your default application for phar files.
-
Just right click the file > properties > change > choose php.exe . Jan 10, 2019 at 16:11
.phar
stands for PHP Archive
Usually .phar
s take some arguments, so they are intended to be run from command prompt. Linux/BSD/OS X shell or Windows command prompt.
Linux .phar
use case scenarios assume .phar
s are copied to some /bin and renamed to be without .phar extension, so you can use a php archive as if you would use any other linux command. So I recommend following way of doing the same thing with Windows:
- Put all your
.phar
files to one directory likeC:\php\phars
- Add
C:\php\phars
to system environment variables (right-click myComputer -> Properties -> Advanced System Settings -> Environment variables
) - Start the elevated command prompt (find command prompt in start menu then right-click and select
Run as Administrator
) - Type the following commands, replacing the path
C:\phpdev\php\php542\php.exe
with full path to your PHP executable:
ftype PHARFile=C:\phpdev\php\php542\php.exe "%1" %*
assoc .phar=PHARFile
set PATHEXT=%PATHEXT%;.PHAR
Next time your should be able just to run Windows console (keyboard Win+R and type cmd.exe
) and type any of your .phar
's like apigen.phar
followed by any command and it will work
C:\Users\acosonic>apigen.phar help
Usage:
...
Arguments:
command The command to execute
command_name The command name (default: "help")
Options:
--xml To output help as XML
--format To output help in other formats (default: "txt")
--raw To output raw command help
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--version (-V) Display this application version.
Help:
The help command displays help for a given command:
php C:\phpdev\phars\apigen.phar help list
You can also output the help in other formats by using the --format option:
php C:\phpdev\phars\apigen.phar help --format=xml list
To display the list of available commands, please use the list command.
C:\Users\acosonic>
So this way lets you run .phar
archives in a directory where you need to work, for example generating documentation in C:\myproject\controller
without specifying full path to .phar
as if you would if it's run without adding it to Windows path.
To explain what commands in step 4 did:
- Created mapping HKCR.phar → HKCR\PHARFile
- Created HKCR\PHARFile\shell\open\command = 'php.exe "%1" %*' [REG_EXPAND_SZ]
- Extended HKCU\Environment\PATHEXT = '%PATHEXT%;.PHAR' [REG_EXPAND_SZ]
*.phar
gets treated like binary/script, and *.phar
execution works as long as a *.phar
file is located anywhere in %PATH%
.
-
Johni's method seemed to work, I don't know if both were needed to access the help for the phar. Jan 10, 2019 at 16:15
One can wrap php
*.phar
with *.bat
, then the filename will be the name of the CLI command:
@ECHO OFF
php "C:/Program Files/PHAR/phpDocumentor.phar" %*
One can also use such a wrap to pass along default arguments; eg. wp.bat
:
@ECHO OFF
php "C:/Program Files/PHAR/wp-cli.phar" --path="D:/SDK/wordpress" %*
Where pattern %*
will capture and forward CLI arguments, as it is supposed to.
Alike this one can run phar
alike any other CLI command, in a terminal window.
-
Many wordpress wp-cli.phar examples online use the linux short form command line
wp <command>
. If you use @Martin Z's example ofwp.bat
you can effectively enable the short form on windows, to make it easier to follow linux based examples. Wherever you choose to place your phars, like c:/phar, it must be on the path as suggested by @Alexsandar's answer.– DWBJan 18 at 1:49 -
It doesn't matter if one wraps it in a batch script or with the registry, both appears valid... but a script can be checked in (it does more than just run a single command). Else you'd need a
*.reg
Regedit config, which registers*.phar
file-type accordingly.C:/Program Files/PHAR
could indeed be onPATH
, whilePATH
isn't too portable either. Jan 18 at 12:25
Keep it simple. Instead of changing .phar's default program, which is not always easy in Windows, try just typing "php" in front of your command.
php composer.phar update