Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cyco130/php-tag
PHP-in-JS: a silly experiment
https://github.com/cyco130/php-tag
Last synced: 3 months ago
JSON representation
PHP-in-JS: a silly experiment
- Host: GitHub
- URL: https://github.com/cyco130/php-tag
- Owner: cyco130
- License: mit
- Created: 2022-07-31T11:09:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-13T09:59:54.000Z (about 2 years ago)
- Last Synced: 2024-09-07T23:11:47.048Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 435 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# php-tag
Write your PHP backend code right inside your frontend JavaScript code using tagged template literals:
```js
import { php } from "php-tag";async function run() {
// The return value is serialized to JSON and sent back
const hello = await php`return "Hello world!";`;
console.assert(hello === "Hello world!");// Interpolated values are serialized to JSON and sent to the backend
const sum = await php`return ${12} + ${30};`;
console.assert(sum === 42);
}
```## Anticipated reactions
- **Oh the horror! Why? Just why?**
Because I can :D
- **This is a terrible idea!**
But colocation is good, isn't it? Like Next.js's `getServerSideProps` but for PHP backends. Is it _really_ that different? With some IDE support it could be pretty useful, methinks.
Still not buying it? OK, neither am I.
- **Isn't it unsafe?**
In production, it's no less safe than any other API endpoint. Just remember to treat interpolated values like any other user input and validate them.
## Actual reactions
![Reactions](https://github.com/cyco130/php-tag/raw/main/pics/reactions.png)
Interestingly the PHP community was more receptive thant the JavaScript community.
## How does it work?
`php-tag` comes in the form of a Vite plugin (`php-tag/vite-plugin`). If you want to use with other tools, the underlying Babel transform (`php-tag/babel-plugin`) is also available.
For the production build, the Babel transform extracts each piece of PHP code into its own PHP file with a unique name. The client sends this name along with the interpolated arguments as a POST request with a JSON body to the `/php-tag` endpoint. The backend is supposed to provide a dispatcher at that endpoint that loads the requested PHP file and executes it with the arguments sent.
During development, the client sends the code itself instead of a file name and the backend just `eval`s it.
## Getting started
```sh
# Clone the example
npx degit cyco130/php-tag/examples/basic php-tag-example
# Install dependencies
npm install
# Run the example in development mode
npm run dev# Build the example
npm run build
# Preview the example
npm run preview
```## Future ideas
- [ ] IDE support
- [ ] `python-tag`, `go-tag`, `java-tag` etc.
- [ ] Customization options
- [ ] More integration examples (Laravel, Symfony, etc.)## What about `node-tag`?
[It already exists](https://rakkasjs.org/guide/use-server-side-query) under the name `useServersideQuery` in a much more useful and type-safe form in Rakkas, a React metaframework with some cutting-edge features.
## License
[MIT](./LICENSE)