Skip to content

Instantly share code, notes, and snippets.

@Frulko
Created June 29, 2023 14:40
Show Gist options
  • Save Frulko/53dea82576a72e2d11816239dfebd2b2 to your computer and use it in GitHub Desktop.
Save Frulko/53dea82576a72e2d11816239dfebd2b2 to your computer and use it in GitHub Desktop.
Valet proxy for PHP and Composer commands inside isolated sites

Valet proxy commands

Instruction

  • Copy the file valetproxy.php inside your computer
  • rename as valetproxy
  • chmod u+x valetproxy
  • mv valetproxy /usr/local/bin/ (like composer installation process)
  • edit your ~/.zshrc, ~/.bash_profile
  • add these following lines
alias php='valetproxy --detect && $(valetproxy) php'
alias composer='valetproxy --detect && $(valetproxy) composer'
  • start a new terminal window
  • voilà
#!/usr/bin/env php
<?php
use Illuminate\Container\Container;
use function Valet\warning;
use Valet\PhpFpm;
use Valet\Configuration;
$composer_autoload = getenv('HOME').'/.composer/vendor/autoload.php';
if ( file_exists( $composer_autoload ) ) {
require_once $composer_autoload;
}
// Use the Valet namespace
define('DETECT_MODE', count($argv) > 1 && $argv[1] === '--detect');
/**
* Create the application.
*/
$container = Container::setInstance(new Container);
$current_folder = getcwd();
$executable = "";
$links = Site::links();
$found = current(array_filter($links->all(), function($link) {
global $current_folder;
return strpos($current_folder, $link['path']) !== false;
}));
$configuration = $container->get(Configuration::class);
if (empty($configuration->read()['tld']) || empty($configuration->read()['loopback'])) {
$configuration->writeBaseConfiguration();
}
if (!empty($found)) {
$url = preg_replace("(^https?://)", "", $found['url']);
$phpfpm = $container->get(PhpFpm::class);
$sites = $phpfpm->isolatedDirectories();
$found_isolated = current(array_filter($sites->all(), function($site) {
global $url;
return $site['url'] === $url;
}));
if (!empty($found_isolated)) {
$executable = "valet";
if (DETECT_MODE) {
warning("[Valet] You are in isolated environnement : " . $found_isolated['version']);
}
}
}
if (DETECT_MODE) {
exit();
}
exit($executable . PHP_EOL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment