A PHP REPL right in your editor! Tinker away without sacrificing your blessed keybinds!
See it in action
demo.mov
As long as you're in a PHP project, run :PhpTinker to open two buffers split on the screen.
If you don't see two buffers in split screen, it's likely because you're not in a PHP project that could be found by the TweakPHP client.
If you see the text "Tinker away!" on both screens, you're good to go! You can edit the left buffer, then run :PhpTinkerRun and you'll see the evaluation in the right buffer.
- PHP
- Currently, versions 8.2, 8.3, and 8.4 are supported
phpmust be in your path so we can run the client- If you're using a version other than those listed above, feel free to submit a pull request adding that version of the client phar.
- grep & sed
- We use these to detect your PHP version. php-tinker runs
php -vand then pipes it to grep and sed to pull out the version (e.g.8.4).
- We use these to detect your PHP version. php-tinker runs
- Neovim 0.11+
- I think I tested this on 0.10 before I upgraded, but currently I'm only testing this on 0.11
In your favorite package manager, add tylerdak/php-tinker.nvim. I use lazy, here's my config:
{
"tylerdak/php-tinker.nvim",
opts = {
keymaps = {
run_tinker = "<CR>"
}
}
}I suggest starting with the above if you're using lazy. Once you've added the plugin to your manager, you could technically be up and running on your next restart. However, I suggest adding a keymap like I do above.
The run_tinker keymap defines what keymap in normal mode will trigger the :PhpTinkerRun command.
For consistency with something like CodeCompanion,
<CR>feels like a nice default to try first. When developing this I was using<Leader>rp(for RunPhp, I guess) but that was pretty goofy and not very intuitive.
`Invalid Path` when running `:PhpTinker`
This means your project isn't being loaded properly by the TweakPHP client.
Any Laravel, WordPress, or Symfony project should be picked up automatically, but at a minimum your project directory should have a vendor/autoload.php file so the client's ComposerLoader can find your project.
If you really want to avoid Composer for some reason, you can even get away with a nearly blank php file at vendor/autoload.php like this:
<?phpNo other files necessary. Just a quick mkdir vendor && echo "<?php" > vendor/autoload.php should do the trick.
Are semicolons required or not?
Semicolons are required for every PHP statement you would normally need one on except for the last line. So when you write your first line in the buffer, you could leave it off. However, once you start adding more code you'd need to go back and add a semicolon to that first statement you wrote.
The last line can have a semicolon too if that's what you really want, though.
If I have time, I might tackle some of these:
- Add more configuration where applicable
- e.g. hooks for the startup process, so you can replace the default
"Tinker away!"text with something useful like dependency imports
- e.g. hooks for the startup process, so you can replace the default
- Add a help entry
- Automatic client download for your relevant PHP version
- Feels somewhat silly and wasteful to have several client versions in the repo
- It'd be cool to build the client using the user's installed PHP if it doesn't exist
- Savable tinker sessions
- LSP/cmp support would be really poggers but I have no clue how to do that yet
Feel free to submit PRs or ideas for getting the above done, though!
