Skip to content

Instantly share code, notes, and snippets.

@allanphilipbarku
Last active December 6, 2024 14:00
Show Gist options
  • Save allanphilipbarku/84073b39839b6a852e81f86729db89ed to your computer and use it in GitHub Desktop.
Save allanphilipbarku/84073b39839b6a852e81f86729db89ed to your computer and use it in GitHub Desktop.

Laravel LocalValetDriver with Custom Path Resolution for OJS Open Journal Systems (OJS) - Software

This PHP script acts as a custom Valet (or Herd) driver for Open Journal Systems (OJS), allowing you to set custom paths and resolutions for specific resources in your ojs application. It can be exceptionally useful if you're working on ojs projects on using larevel herd or valet as development setup.

Usage

To use the driver, save this as LocalValetDriver.php and place it in your project's root directory. Laravel Valet (or Herd) will automatically use the custom driver when serving your application.

<?php
use Valet\Drivers\LaravelValetDriver;
class LocalValetDriver extends LaravelValetDriver
{
/**
* Determine if the driver serves the request.
*/
public function serves(string $sitePath, string $siteName, string $uri): bool
{
return true;
}
/**
* Determine if the incoming request is for a static file.
*/
public function isStaticFile(string $sitePath, string $siteName, string $uri)/*: string|false */
{
$staticFilePath = $sitePath.$uri;
$patternStyle = "/Applications\/Herd\.app\/Contents\/Resources\/valet\/styles\//";
$replacementStyle = '/styles/';
$staticFilePath = preg_replace($patternStyle, $replacementStyle, $staticFilePath);
$patternJs = "/Applications\/Herd\.app\/Contents\/Resources\/valet\/js\//";
$replacementJs = '/js/';
$staticFilePath = preg_replace($patternJs, $replacementJs, $staticFilePath);
$patternPlugin = "/Applications\/Herd\.app\/Contents\/Resources\/valet\/plugins\//";
$replacementPlugin = '/plugins/';
$staticFilePath = preg_replace($patternPlugin, $replacementPlugin, $staticFilePath);
// Rewrite files from Applications/Herd.app/Contents/Resources/valet/lib/ to /lib/
$pattern = "/Applications\/Herd\.app\/Contents\/Resources\/valet\/lib\//";
$replacement = '/lib/';
$staticFilePath = preg_replace($pattern, $replacement, $staticFilePath);
// support index.html
if (is_dir($staticFilePath)) {
if (file_exists($f = str_replace('//', '/', $staticFilePath.'/index.html'))) {
return $f;
}
}
if (file_exists($staticFilePath) && ! is_dir($staticFilePath)) {
return $staticFilePath;
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*/
public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
{
$path = $sitePath.$uri;
// The requested resource is a directory and contains a child ‘index.php’ file.
if (file_exists($path.'/index.php')) {
return $path.'/index.php';
}
// For cases the URI is structured as 'index.php/index/install'
if (strpos($uri, 'index.php') !== false) {
return $sitePath.'/index.php';
}
// The requested resource is not a PHP file.
if (file_exists($path) && isset(pathinfo($path)['extension']) && pathinfo($path)['extension'] != 'php') {
return $path;
}
// The requested resource is a PHP file.
if (file_exists($path) && isset(pathinfo($path)['extension']) && pathinfo($path)['extension'] == 'php') {
return $path;
}
// 404
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment