1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-06 00:40:06 +01:00

organize better

This commit is contained in:
Taylor Otwell
2016-05-10 08:29:24 -05:00
parent d4164ad764
commit 97f7ae2342
37 changed files with 13 additions and 13 deletions

View File

@@ -0,0 +1,155 @@
<?php
class BasicValetDriver 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 true;
}
/**
* 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)
{
if (file_exists($staticFilePath = $sitePath.'/public'.$uri)) {
return $staticFilePath;
} elseif ($this->isActualFile($staticFilePath = $sitePath.$uri)) {
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)
{
$dynamicCandidates = [
$this->asActualFile($sitePath, $uri),
$this->asPhpIndexFileInDirectory($sitePath, $uri),
$this->asHtmlIndexFileInDirectory($sitePath, $uri),
];
foreach ($dynamicCandidates as $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate);
return $candidate;
}
}
$fixedCandidates = [
$this->asRootPhpIndexFile($sitePath),
$this->asPublicPhpIndexFile($sitePath),
$this->asPublicHtmlIndexFile($sitePath),
];
foreach ($fixedCandidates as $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = '/index.php';
return $candidate;
}
}
}
/**
* Determine if the path is a file and not a directory.
*
* @param string $path
* @return bool
*/
protected function isActualFile($path)
{
return file_exists($path) && ! is_dir($path);
}
/**
* Concatenate the site path and URI as a single file name.
*
* @param string $sitePath
* @param string $uri
* @return string
*/
protected function asActualFile($sitePath, $uri)
{
return $sitePath.$uri;
}
/**
* Format the site path and URI with a trailing "index.php".
*
* @param string $sitePath
* @param string $uri
* @return string
*/
protected function asPhpIndexFileInDirectory($sitePath, $uri)
{
return $sitePath.rtrim($uri, '/').'/index.php';
}
/**
* Format the site path and URI with a trailing "index.html".
*
* @param string $sitePath
* @param string $uri
* @return string
*/
protected function asHtmlIndexFileInDirectory($sitePath, $uri)
{
return $sitePath.rtrim($uri, '/').'/index.html';
}
/**
* Format the incoming site path as root "index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asRootPhpIndexFile($sitePath)
{
return $sitePath.'/index.php';
}
/**
* Format the incoming site path as a "public/index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asPublicPhpIndexFile($sitePath)
{
return $sitePath.'/public/index.php';
}
/**
* Format the incoming site path as a "public/index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asPublicHtmlIndexFile($sitePath)
{
return $sitePath.'/public/index.html';
}
}

View File

@@ -0,0 +1,49 @@
<?php
class CakeValetDriver 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.'/bin/cake');
}
/**
* 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)
{
if (file_exists($staticFilePath = $sitePath.'/webroot/'.$uri)) {
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)
{
$_SERVER['SCRIPT_FILENAME'] = $sitePath.'/webroot/index.php';
return $sitePath.'/webroot/index.php';
}
}

View File

@@ -0,0 +1,51 @@
<?php
class ContaoValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serves($sitePath, $siteName, $uri)
{
return is_dir($sitePath.'/vendor/contao') && file_exists($sitePath.'/web/app.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)
{
if (file_exists($staticFilePath = $sitePath.'/web'.$uri)) {
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)
{
if ($uri == '/install.php') {
return $sitePath.'/web/install.php';
}
return $sitePath.'/web/app.php';
}
}

View File

@@ -0,0 +1,51 @@
<?php
class CraftValetDriver 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 is_dir($sitePath.'/craft');
}
/**
* 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)
{
if (file_exists($staticFilePath = $sitePath.'/public'.$uri)) {
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)
{
$_SERVER['SCRIPT_FILENAME'] = $sitePath.'/public/index.php';
$_SERVER['SCRIPT_NAME'] = '/index.php';
return $sitePath.'/craft/app/index.php';
}
}

View File

@@ -0,0 +1,28 @@
<?php
class JigsawValetDriver extends BasicValetDriver
{
/**
* Mutate the incoming URI.
*
* @param string $uri
* @return string
*/
public function mutateUri($uri)
{
return rtrim('/build_local'.$uri, '/');
}
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serves($sitePath, $siteName, $uri)
{
return is_dir($sitePath.'/build_local');
}
}

View File

@@ -0,0 +1,28 @@
<?php
class KatanaValetDriver extends BasicValetDriver
{
/**
* Mutate the incoming URI.
*
* @param string $uri
* @return string
*/
public function mutateUri($uri)
{
return rtrim('/public'.$uri, '/');
}
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serves($sitePath, $siteName, $uri)
{
return file_exists($sitePath.'/katana');
}
}

View File

@@ -0,0 +1,60 @@
<?php
class KirbyValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serves($sitePath, $siteName, $uri)
{
return is_dir($sitePath.'/kirby');
}
/**
* 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)
{
if (file_exists($sitePath.$uri) && ! is_dir($sitePath.$uri)) {
return $sitePath.$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)
{
// Needed to force Kirby to use *.dev to generate its URLs...
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
if (preg_match('/^\/panel/', $uri)) {
$_SERVER['SCRIPT_NAME'] = '/panel/index.php';
return $sitePath.'/panel/index.php';
}
if (file_exists($indexPath = $sitePath.'/index.php')) {
$_SERVER['SCRIPT_NAME'] = '/index.php';
return $indexPath;
}
}
}

View File

@@ -0,0 +1,59 @@
<?php
class LaravelValetDriver 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.'/public/index.php') &&
file_exists($sitePath.'/artisan');
}
/**
* 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)
{
if (file_exists($staticFilePath = $sitePath.'/public'.$uri)) {
return $staticFilePath;
}
$storageUri = $uri;
if (strpos($uri, '/storage/') === 0) {
$storageUri = substr($uri, 8);
}
if (file_exists($storagePath = $sitePath.'/storage/app/public'.$storageUri) &&
! is_dir($storagePath)) {
return $storagePath;
}
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)
{
return $sitePath.'/public/index.php';
}
}

View File

@@ -0,0 +1,28 @@
<?php
class SculpinValetDriver extends BasicValetDriver
{
/**
* 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 is_dir($sitePath.'/.sculpin');
}
/**
* Mutate the incoming URI.
*
* @param string $uri
* @return string
*/
public function mutateUri($uri)
{
return rtrim('/output_dev'.$uri, '/');
}
}

View File

@@ -0,0 +1,68 @@
<?php
class StatamicV1ValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serves($sitePath, $siteName, $uri)
{
return file_exists($sitePath.'/_app/core/statamic.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)
{
if (strpos($uri, '/_add-ons') === 0 || strpos($uri, '/_app') === 0 || strpos($uri, '/_content') === 0 ||
strpos($uri, '/_cache') === 0 || strpos($uri, '/_config') === 0 || strpos($uri, '/_logs') === 0 ||
$uri === '/admin'
) {
return false;
}
if (file_exists($staticFilePath = $sitePath.$uri)) {
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)
{
if (strpos($uri, '/admin.php') === 0) {
$_SERVER['SCRIPT_NAME'] = '/admin.php';
return $sitePath.'/admin.php';
}
if ($uri === '/admin') {
$_SERVER['SCRIPT_NAME'] = '/admin/index.php';
return $sitePath.'/admin/index.php';
}
$_SERVER['SCRIPT_NAME'] = '/index.php';
return $sitePath.'/index.php';
}
}

View File

@@ -0,0 +1,77 @@
<?php
class StatamicValetDriver 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 is_dir($sitePath.'/statamic');
}
/**
* 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)
{
if (strpos($uri, '/site') === 0 && strpos($uri, '/site/themes') !== 0) {
return false;
} elseif (strpos($uri, '/local') === 0 || strpos($uri, '/statamic') === 0) {
return false;
} elseif (file_exists($staticFilePath = $sitePath.$uri)) {
return $staticFilePath;
} elseif (file_exists($staticFilePath = $sitePath.'/public'.$uri)) {
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)
{
if (file_exists($staticPath = $sitePath.'/static'.$uri.'/index.html')) {
return $staticPath;
}
$_SERVER['SCRIPT_NAME'] = '/index.php';
if (strpos($_SERVER['REQUEST_URI'], '/index.php') === 0) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 10);
}
if ($uri === '') {
$uri = '/';
}
if ($uri === '/installer.php') {
return $sitePath.'/installer.php';
}
if (file_exists($indexPath = $sitePath.'/index.php')) {
return $indexPath;
}
if (file_exists($indexPath = $sitePath.'/public/index.php')) {
return $indexPath;
}
}
}

View File

@@ -0,0 +1,52 @@
<?php
class SymfonyValetDriver 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.'/web/app_dev.php') || file_exists($sitePath.'/web/app.php')) &&
(file_exists($sitePath.'/bin/symfony_requirements'));
}
/**
* 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)
{
if (file_exists($staticFilePath = $sitePath.'/web/'.$uri)) {
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)
{
if (file_exists($frontControllerPath = $sitePath.'/web/app_dev.php')) {
return $frontControllerPath;
} elseif (file_exists($frontControllerPath = $sitePath.'/web/app.php')) {
return $frontControllerPath;
}
}
}

129
cli/drivers/ValetDriver.php Normal file
View File

@@ -0,0 +1,129 @@
<?php
abstract class ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
abstract public function serves($sitePath, $siteName, $uri);
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
*/
abstract public function isStaticFile($sitePath, $siteName, $uri);
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
abstract public function frontControllerPath($sitePath, $siteName, $uri);
/**
* Find a driver that can serve the incoming request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return ValetDriver|null
*/
public static function assign($sitePath, $siteName, $uri)
{
$drivers = static::driversIn(VALET_HOME_PATH.'/Drivers');
$drivers[] = 'LaravelValetDriver';
$drivers[] = 'WordPressValetDriver';
$drivers[] = 'SymfonyValetDriver';
$drivers[] = 'CraftValetDriver';
$drivers[] = 'StatamicValetDriver';
$drivers[] = 'StatamicV1ValetDriver';
$drivers[] = 'CakeValetDriver';
$drivers[] = 'SculpinValetDriver';
$drivers[] = 'JigsawValetDriver';
$drivers[] = 'KirbyValetDriver';
$drivers[] = 'ContaoValetDriver';
$drivers[] = 'KatanaValetDriver';
$drivers[] = 'BasicValetDriver';
foreach ($drivers as $driver) {
$driver = new $driver;
if ($driver->serves($sitePath, $siteName, $driver->mutateUri($uri))) {
return $driver;
}
}
}
/**
* Get all of the driver classes in a given path.
*
* @param string $path
* @return array
*/
public static function driversIn($path)
{
if (! is_dir($path)) {
return [];
}
$drivers = [];
foreach (scandir($path) as $file) {
if ($file !== 'ValetDriver.php' && strpos($file, 'ValetDriver') !== false) {
require_once $path.'/'.$file;
$drivers[] = basename($file, '.php');
}
}
return $drivers;
}
/**
* Mutate the incoming URI.
*
* @param string $uri
* @return string
*/
public function mutateUri($uri)
{
return $uri;
}
/**
* Serve the static file at the given path.
*
* @param string $staticFilePath
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
{
$extension = pathinfo($staticFilePath)['extension'];
$mimes = require(__DIR__.'/../mimes.php');
$mime = isset($mimes[$extension]) ? $mimes[$extension] : 'application/octet-stream';
header('Content-Type: '. $mime);
readfile($staticFilePath);
}
}

View File

@@ -0,0 +1,32 @@
<?php
class WordPressValetDriver extends BasicValetDriver
{
/**
* 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 is_dir($sitePath.'/wp-admin');
}
/**
* 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['PHP_SELF'] = $uri;
return parent::frontControllerPath($sitePath, $siteName, $uri);
}
}

23
cli/drivers/require.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
/**
* Basic drivers...
*/
require_once __DIR__.'/ValetDriver.php';
require_once __DIR__.'/BasicValetDriver.php';
/**
* Specific drivers...
*/
require_once __DIR__.'/CraftValetDriver.php';
require_once __DIR__.'/JigsawValetDriver.php';
require_once __DIR__.'/KirbyValetDriver.php';
require_once __DIR__.'/LaravelValetDriver.php';
require_once __DIR__.'/SculpinValetDriver.php';
require_once __DIR__.'/StatamicValetDriver.php';
require_once __DIR__.'/StatamicV1ValetDriver.php';
require_once __DIR__.'/SymfonyValetDriver.php';
require_once __DIR__.'/WordPressValetDriver.php';
require_once __DIR__.'/ContaoValetDriver.php';
require_once __DIR__.'/KatanaValetDriver.php';
require_once __DIR__.'/CakeValetDriver.php';