Windows 8: .phar files, how do you want to open

2019-03-26 00:19发布

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?

2条回答
等我变得足够好
2楼-- · 2019-03-26 00:47

You have to set php.exe as your default application for phar files.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-26 00:52

.phar stands for PHP Archive

Usually .phar's take some arguments, so they are intended to be run from command prompt. Linux/bsd/Osx shell or windows command prompt.

Linux .phar use case scenarios assume .phars 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:

  1. Put all your .phar files to one dir like C:\php\phars
  2. Add C:\php\phars to system environment variables (rightclick my computer->properties->advanced system settings->Environment variables)
  3. Start the elevated command prompt (find command prompt in start menu then right click run as administrator)
  4. Type the following commands, replacing the path C:\phpdev\php\php542\php.exe with full path to your PHP executable
    ftype PharArchive=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 (keybard 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:

  1. Created HKCR.phar → HKCR\PHARFile
  2. Created HKCR\PHARFile\shell\open\command = 'php.exe "%1" %*' [REG_EXPAND_SZ]
  3. 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%

查看更多
登录 后发表回答