Skip to content

Instantly share code, notes, and snippets.

@RomainMazB
Created September 10, 2020 07:13
Show Gist options
  • Save RomainMazB/b09346f2d4d7a173afa27373bce5b0cf to your computer and use it in GitHub Desktop.
Save RomainMazB/b09346f2d4d7a173afa27373bce5b0cf to your computer and use it in GitHub Desktop.
A Valet PrestaShop Driver, tested on 1.6.2 but it should work on newer version. Just place it in your ~/.valet/Drivers/ directory and let the magic happen :)
<?php
class PrestaShopValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return bool
*/
public function serves($sitePath, $siteName, $uri)
{
return file_exists($sitePath . '/classes/PrestaShopAutoload.php');
}
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return string|false
*/
public function isStaticFile($sitePath, $siteName, $uri)
{
// Basic static file
if (is_file($staticFilePath = "{$sitePath}/{$uri}")) {
return $staticFilePath;
}
// RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]
if (preg_match('/c\/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/c/{$matches[1]}{$matches[2]}{$matches[3]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]
if (preg_match('/c\/([a-zA-Z_-]+)(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/c/{$matches[1]}{$matches[2]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
if (preg_match('/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[2]}/{$matches[3]}/{$matches[4]}/{$matches[5]}/{$matches[6]}/{$matches[7]}/{$matches[8]}/{$matches[1]}{$matches[2]}{$matches[3]}{$matches[4]}{$matches[5]}{$matches[6]}{$matches[7]}{$matches[8]}{$matches[9]}{$matches[10]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
if (preg_match('/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[2]}/{$matches[3]}/{$matches[4]}/{$matches[5]}/{$matches[6]}/{$matches[7]}/{$matches[1]}{$matches[2]}{$matches[3]}{$matches[4]}{$matches[5]}{$matches[6]}{$matches[7]}{$matches[8]}{$matches[9]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
if (preg_match('/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[2]}/{$matches[3]}/{$matches[4]}/{$matches[5]}/{$matches[6]}/{$matches[1]}{$matches[2]}{$matches[3]}{$matches[4]}{$matches[5]}{$matches[6]}{$matches[7]}{$matches[8]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
if (preg_match('/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[2]}/{$matches[3]}/{$matches[4]}/{$matches[5]}/{$matches[1]}{$matches[2]}{$matches[3]}{$matches[4]}{$matches[5]}{$matches[6]}{$matches[7]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
if (preg_match('/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[2]}/{$matches[3]}/{$matches[4]}/{$matches[1]}{$matches[2]}{$matches[3]}{$matches[4]}{$matches[5]}{$matches[6]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
if (preg_match('/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[2]}/{$matches[3]}/{$matches[1]}{$matches[2]}{$matches[3]}{$matches[4]}{$matches[5]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
if (preg_match('/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[2]}/{$matches[1]}{$matches[2]}{$matches[3]}{$matches[4]}.jpg")) {
return $staticFilePath;
}
}
// RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
if (preg_match('/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?\/.+\.jpg/i', $uri, $matches)) {
if (is_file($staticFilePath = "{$sitePath}/img/p/{$matches[1]}/{$matches[1]}{$matches[2]}{$matches[3]}.jpg")) {
return $staticFilePath;
}
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return string
*/
public function frontControllerPath($sitePath, $siteName, $uri)
{
//Legacy URls
$parts = explode('/',$uri);
if(isset($parts[1]) && $parts[1] !='' && file_exists($adminIndex = $sitePath . '/'. $parts[1] .'/index.php')){
if(isset($parts[2]) && $parts[2] === 'filemanager') {
$_SERVER['SCRIPT_FILENAME'] = $sitePath . $uri;
return $sitePath . $uri;
}
$_SERVER['SCRIPT_FILENAME'] = $adminIndex;
$_SERVER['SCRIPT_NAME'] = '/'. $parts[1] .'/index.php';
if(isset($_GET['controller']) || isset($_GET['tab'])){
return $adminIndex;
}
return $adminIndex;
}
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SCRIPT_FILENAME'] = $sitePath . '/index.php';
return $sitePath . '/index.php';
}
}
@RomainMazB
Copy link
Author

I've found some PrestaShop Valet Driver out there but all had small issues: image links were broken or filemanager wasn't working.

I've forked one of them and fix the issues (because the repo didn't merge a PR request in three years).

This Valet Driver have been tested by myself on with PrestaShop 1.6.2 (the only version I needed to run) but it should work on new version since the files paths are the same in newer version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment