How to get PHP executable

@picklebrownie The PHP path is determined dynamically depending on the OS and system architecture, which is why it’s not available on the site object.

To display it in a component, you could use an IPC call. For example:

main.ts

import * as LocalMain from '@getflywheel/local/main';

export default function (context: LocalMain.AddonMainContext): void {
	const { electron } = context;
	const { ipcMain } = electron;
	const { siteData, lightningServices } = LocalMain.getServiceContainer().cradle;

	ipcMain.on('get-php-bin-path', (event, siteId) => {
		const site = siteData.getSite(siteId);
		const services = lightningServices.getSiteServices(site);
		const phpService = services.find((service) => service.serviceName === 'php');
		const phpBinPath = phpService?.bin;

		event.sender.send('php-bin-path', phpBinPath);
	});
}

renderer.jsx

import Boilerplate from './Boilerplate';
import fs from 'fs-extra';
import path from 'path';

const packageJSON = fs.readJsonSync(path.join(__dirname, '../package.json'));
const addonID = packageJSON.slug;

export default function (context) {
	const { React, hooks } = context;

	hooks.addFilter('siteInfoToolsItem', (menu) => [
		...menu,
		{
			menuItem: 'Counter',
			path: `/${addonID}`,
			render: (props) => <Boilerplate {...props} />,
		},
	]);
}

Boilerplate.jsx

import React, { useState, useEffect } from "react";
import { ipcRenderer } from "electron";

const Boilerplate = (props) => {
	const [siteId] = useState(props.site?.id);
	const [phpBinPath, setPhpBinPath] = useState("");

	useEffect(() => {
		ipcRenderer.send("get-php-bin-path", siteId);
		ipcRenderer.on("php-bin-path", (event, phpBinPath) => {
			setPhpBinPath(phpBinPath);
		});

		return () => {
			ipcRenderer.removeAllListeners("php-bin-path");
		};
	}, []);

	return (
		<div style={{ flex: "1", overflowY: "auto", margin: "10px" }}>
			<p>PHP Bin Path: {JSON.stringify(phpBinPath)}</p>
		</div>
	);
};

export default Boilerplate;

Resulting in this:

If you just want the php path you can use phpBinPath.php instead of the full phpBinPath object (which gives php-fpm and php-cgi paths too).

3 Likes