donnikitos / vite-plugin-php

Vite's speed and tooling to preprocess PHP-files!
https://www.npmjs.com/package/vite-plugin-php
MIT License
40 stars 0 forks source link

Fix relative php imports #28

Open donnikitos opened 2 months ago

donnikitos commented 2 months ago

i can set the include_path - but DIR will always be the location of router.php in node_modules/vite-plugin-php/dist so you can never have an include like ../db.php to include a file one folder above or hack together a solution that uses DIR from the file you want to include from.

5:54:08 AM [vite] page reload .php-tmp/main.php
index.mjs:6950
5:54:08 AM [vite] page reload .php-tmp/php/register.php
index.mjs:6950
5:54:08 AM [vite] page reload .php-tmp/php/include/db.php
index.mjs:6950
5:54:08 AM [vite] page reload .php-tmp/php/include/login.php
index.mjs:6950
5:54:08 AM [vite] page reload .php-tmp/php/include/functions/databasefunctions.php <-- trying to include('../db.php') here fails but will always work on any local / remote php installation / apache server.
index.mjs:6950
5:54:08 AM [vite] page reload .php-tmp/php/include/functions/logfunctions.php <-- working around this by using ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . __DIR__) will not work as __DIR__ is not ending in /include/functions but the dist-folder
index.mjs:6950
5:54:08 AM [vite] page reload .php-tmp/php/include/functions/wordfunctions.php

(made up names, don't laugh about my filenaming!!)

for now i just set include path from the ROOT that is in /dist and that's fine, but as you might guess, most coders coming from php expect their codebase to work somewhat similar to a simple host local / remote on an apache-server and i think making sure they get an easy same working environment will massively boost adoption rate of your plugin (which is great!).

Originally posted by @krei-se in https://github.com/donnikitos/vite-plugin-php/issues/27#issuecomment-2328001779

krei-se commented 2 months ago

I made some progress on this issue:

$_SERVER['DOCUMENT_ROOT'] <-- this will always be the root of the vite-project, so it's kinda safe bet to use it for figuring out where the handled script lives in.