I'm writing a middleware in Laravel 10 to grab the git version of my app. I'm developing on Windows 11 using VSCode. The code I'm using is as follows:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Process;
use Symfony\Component\HttpFoundation\Response;
class AppVersion
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$label = "V";
$version = "0.1.0";
// Get version string from git
$hash = Process::run('git log --pretty="%h" -n1 HEAD')->output();
$date = Process::run('git log -n1 --pretty=%ci HEAD')->output();
$version_string = "test";
config(['app.version' => $version_string]);
return $next($request);
}
}
However, in the ->errorOutput()
I get the message:
'git' is not recognized as an internal or external command, operable program or batch file.
The Process::run
is working with basic commands like DIR
and git is installed on my system and callable in a terminal, i.e. the PATH is set to the git executable. Thus, I was suspecting that Process::run is not using my system PATH variable to find the command. To check this, I did run
dd(Process::run("path")->output());
and received the output:
"PATH=(null)"
According my understanding, the system path should be used to find commands, but it is not. What am I missing here? Do I have to manually add %PATH% somewhere in Laravel?
I found a workaround. Since the PATH is not included in the search path, I added the full path to git. It's not the nicest solution and I would be happy to learn if there is a better way to do this.
Here is how I do it: