Created
June 4, 2018 16:11
-
-
Save trk/c215d9d8ab3ce03d147c7b54913c4180 to your computer and use it in GitHub Desktop.
ConfigurableValetDriver (currently in use with ProcessWire)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class ConfigurableValetDriver | |
* | |
* You can change paths and files arrays via creating a config file under your site path ".valet/index.php" | |
* | |
* This files must return an array like example. | |
* | |
* return ["paths" => ["foo", "bar"], "files" => ["bar.php", "foo.php"]]; | |
*/ | |
class ConfigurableValetDriver extends BasicValetDriver { | |
// Possible Paths | |
private $paths = ["public", ""]; | |
// Possible Files | |
private $files = ["install.php", "index.php"]; | |
// Real path | |
private $path = ""; | |
// Full-path | |
private $full_path = ""; | |
/** | |
* Determine if the driver serves the request. | |
* | |
* @param string $sitePath | |
* @param string $siteName | |
* @param string $uri | |
* @return bool | |
*/ | |
public function serves($sitePath, $siteName, $uri) { | |
$configsPath = $sitePath . DIRECTORY_SEPARATOR . ".valet" . DIRECTORY_SEPARATOR . "index.php"; | |
if(file_exists($configsPath)) { | |
$configs = include($configsPath); | |
if(is_array($configs)) { | |
if(array_key_exists("paths", $configs) && is_array($configs["paths"])) $this->paths = $configs["paths"]; | |
if(array_key_exists("files", $configs) && is_array($configs["files"])) $this->paths = $configs["files"]; | |
} | |
} | |
$this->getPossiblePath($sitePath, $uri); | |
if(is_dir($this->full_path)) return true; | |
return false; | |
} | |
public function isStaticFile($sitePath, $siteName, $uri) { | |
if(is_file($this->full_path . $uri)) { | |
return $this->full_path . $uri; | |
} | |
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) { | |
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; | |
$_SERVER['SCRIPT_NAME'] = $this->path . '/index.php'; | |
$_GET['it'] = $uri; | |
if ($uri === '') { | |
$uri = '/'; | |
} | |
foreach ($this->files as $file) { | |
$path = $this->full_path . $file; | |
if(file_exists($path)) { | |
$_SERVER['SCRIPT_FILENAME'] = $path; | |
// echo "<pre>" . print_r($_SERVER, true) . "</pre>"; | |
return parent::frontControllerPath( | |
$this->full_path, $siteName, $uri | |
); | |
} | |
} | |
} | |
/** | |
* Get possible site path | |
* | |
* @param $sitePath | |
* @param $uri | |
*/ | |
private function getPossiblePath($sitePath, $uri) { | |
$segments = explode("/", $uri); | |
if(count($segments) > 1) { | |
$uri = ""; | |
foreach ($segments as $segment) { | |
if(!$segment) continue; | |
$data[] = $segment; | |
if(in_array($segment, $this->paths)) $uri = $segment; | |
} | |
if($uri) { | |
$this->path = DIRECTORY_SEPARATOR . $uri; | |
$this->full_path = $sitePath . DIRECTORY_SEPARATOR . $uri; | |
} | |
} | |
if(!$this->full_path){ | |
foreach ($this->paths as $path) { | |
if(is_dir($sitePath . DIRECTORY_SEPARATOR . $path)) { | |
$this->full_path = $sitePath . DIRECTORY_SEPARATOR . $path; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment