Added initial support for running Grav library from outside the webroot [#3297]

This commit is contained in:
Matias Griese
2021-03-30 16:58:19 +03:00
parent e229ab191f
commit 1b9fd6276e
8 changed files with 33 additions and 23 deletions

View File

@@ -1,6 +1,8 @@
# v1.7.10
## mm/dd/2021
1. [](#new)
* Added initial support for running Grav library from outside the webroot [#3297](https://github.com/getgrav/grav/issues/3297)
1. [](#improved)
* Improved password handling when saving a user
1. [](#bugfix)

View File

@@ -27,10 +27,17 @@ if (!defined('GRAV_ROOT')) {
$path = rtrim(str_replace(DIRECTORY_SEPARATOR, DS, getenv('GRAV_ROOT') ?: getcwd()), DS);
define('GRAV_ROOT', $path);
}
if (!defined('GRAV_WEBROOT')) {
define('GRAV_WEBROOT', GRAV_ROOT);
}
if (!defined('GRAV_USER_PATH')) {
$path = rtrim(getenv('GRAV_USER_PATH') ?: 'user', DS);
define('GRAV_USER_PATH', $path);
}
if (!defined('GRAV_SYSTEM_PATH')) {
$path = rtrim(getenv('GRAV_SYSTEM_PATH') ?: 'system', DS);
define('GRAV_SYSTEM_PATH', $path);
}
if (!defined('GRAV_CACHE_PATH')) {
$path = rtrim(getenv('GRAV_CACHE_PATH') ?: 'cache', DS);
define('GRAV_CACHE_PATH', $path);
@@ -52,21 +59,21 @@ unset($path);
define('USER_PATH', GRAV_USER_PATH . DS);
define('CACHE_PATH', GRAV_CACHE_PATH . DS);
define('ROOT_DIR', GRAV_ROOT . DS);
define('USER_DIR', ROOT_DIR . USER_PATH);
define('CACHE_DIR', ROOT_DIR . CACHE_PATH);
define('USER_DIR', (!str_starts_with(USER_PATH, '/') ? GRAV_WEBROOT . '/' : '') . USER_PATH);
define('CACHE_DIR', (!str_starts_with(CACHE_PATH, '/') ? ROOT_DIR : '') . CACHE_PATH);
// DEPRECATED: Do not use!
define('ASSETS_DIR', ROOT_DIR . 'assets/');
define('IMAGES_DIR', ROOT_DIR . 'images/');
define('ASSETS_DIR', GRAV_WEBROOT . '/assets/');
define('IMAGES_DIR', GRAV_WEBROOT . '/images/');
define('ACCOUNTS_DIR', USER_DIR .'accounts/');
define('PAGES_DIR', USER_DIR .'pages/');
define('DATA_DIR', USER_DIR .'data/');
define('SYSTEM_DIR', ROOT_DIR .'system/');
define('LIB_DIR', SYSTEM_DIR .'src/');
define('PLUGINS_DIR', USER_DIR .'plugins/');
define('THEMES_DIR', USER_DIR .'themes/');
define('SYSTEM_DIR', (!str_starts_with(GRAV_SYSTEM_PATH, '/') ? ROOT_DIR : '') . GRAV_SYSTEM_PATH);
define('LIB_DIR', SYSTEM_DIR .'src/');
define('VENDOR_DIR', ROOT_DIR .'vendor/');
define('LOG_DIR', ROOT_DIR . GRAV_LOG_PATH . DS);
define('LOG_DIR', (!str_starts_with(GRAV_LOG_PATH, '/') ? ROOT_DIR : '') . GRAV_LOG_PATH . DS);
// END DEPRECATED
// Some extensions

View File

@@ -125,7 +125,7 @@ abstract class BaseAsset extends PropertyObject
if ($locator->isStream($asset)) {
$path = $locator->findResource($asset, true);
} else {
$path = GRAV_ROOT . $asset;
$path = GRAV_WEBROOT . $asset;
}
// If local file is missing return
@@ -172,10 +172,10 @@ abstract class BaseAsset extends PropertyObject
return $this;
}
/**
* Receive asset location and return the SRI integrity hash
*
*
* @param $input
*
* @return string
@@ -188,7 +188,7 @@ abstract class BaseAsset extends PropertyObject
if ( !empty($assetsConfig['enable_asset_sri']) && $assetsConfig['enable_asset_sri'] )
{
$dataToHash = file_get_contents( GRAV_ROOT . $input);
$dataToHash = file_get_contents( GRAV_WEBROOT . $input);
$hash = hash('sha256', $dataToHash, true);
$hash_base64 = base64_encode($hash);
@@ -209,7 +209,7 @@ abstract class BaseAsset extends PropertyObject
*/
// protected function getLastModificationTime($asset)
// {
// $file = GRAV_ROOT . $asset;
// $file = GRAV_WEBROOT . $asset;
// if (Grav::instance()['locator']->isStream($asset)) {
// $file = $this->buildLocalLink($asset, true);
// }
@@ -228,7 +228,7 @@ abstract class BaseAsset extends PropertyObject
protected function buildLocalLink($asset)
{
if ($asset) {
return $this->base_url . ltrim(Utils::replaceFirstOccurrence(GRAV_ROOT, '', $asset), '/');
return $this->base_url . ltrim(Utils::replaceFirstOccurrence(GRAV_WEBROOT, '', $asset), '/');
}
return false;
}

View File

@@ -164,6 +164,7 @@ class Setup extends Data
public function __construct($container)
{
// Configure main streams.
$this->streams['system']['prefixes'][''] = [GRAV_SYSTEM_PATH];
$this->streams['user']['prefixes'][''] = [GRAV_USER_PATH];
$this->streams['cache']['prefixes'][''] = [GRAV_CACHE_PATH];
$this->streams['log']['prefixes'][''] = [GRAV_LOG_PATH];
@@ -197,16 +198,16 @@ class Setup extends Data
if (null !== $setupFile) {
// Make sure that the custom setup file exists. Terminates the script if not.
if (!str_starts_with($setupFile, '/')) {
$setupFile = GRAV_ROOT . '/' . $setupFile;
$setupFile = GRAV_WEBROOT . '/' . $setupFile;
}
if (!is_file($setupFile)) {
echo 'GRAV_SETUP_PATH is defined but does not point to existing setup file.';
exit(1);
}
} else {
$setupFile = GRAV_ROOT . '/setup.php';
$setupFile = GRAV_WEBROOT . '/setup.php';
if (!is_file($setupFile)) {
$setupFile = GRAV_ROOT . '/' . GRAV_USER_PATH . '/setup.php';
$setupFile = GRAV_WEBROOT . '/' . GRAV_USER_PATH . '/setup.php';
}
if (!is_file($setupFile)) {
$setupFile = null;
@@ -234,7 +235,7 @@ class Setup extends Data
$envPath .= '/';
} else {
// Use default location. Start with Grav 1.7 default.
$envPath = GRAV_ROOT. '/' . GRAV_USER_PATH . '/env';
$envPath = GRAV_WEBROOT. '/' . GRAV_USER_PATH . '/env';
if (is_dir($envPath)) {
$envPath = 'user://env/';
} else {
@@ -257,7 +258,7 @@ class Setup extends Data
*/
public function init()
{
$locator = new UniformResourceLocator(GRAV_ROOT);
$locator = new UniformResourceLocator(GRAV_WEBROOT);
$files = [];
$guard = 5;

View File

@@ -178,7 +178,7 @@ class InitializeProcessor extends ProcessorBase
$grav['plugins']->setup();
if (defined('GRAV_SCHEMA') && $config->get('versions') === null) {
$filename = GRAV_ROOT . '/user/config/versions.yaml';
$filename = USER_DIR . 'config/versions.yaml';
if (!is_file($filename)) {
$versions = [
'core' => [

View File

@@ -30,7 +30,7 @@ class StreamsServiceProvider implements ServiceProviderInterface
public function register(Container $container)
{
$container['locator'] = function (Container $container) {
$locator = new UniformResourceLocator(GRAV_ROOT);
$locator = new UniformResourceLocator(GRAV_WEBROOT);
/** @var Setup $setup */
$setup = $container['setup'];

View File

@@ -232,7 +232,7 @@ ERR;
$this->location = dirname($location, 4);
$versions = Versions::instance(GRAV_ROOT . '/user/config/versions.yaml');
$versions = Versions::instance(USER_DIR . 'config/versions.yaml');
$this->updater = new VersionUpdater('core/grav', __DIR__ . '/updates', $this->getVersion(), $versions);
$this->updater->preflight();
@@ -280,7 +280,7 @@ ERR;
{
// Finalize can be run without installing Grav first.
if (!$this->updater) {
$versions = Versions::instance(GRAV_ROOT . '/user/config/versions.yaml');
$versions = Versions::instance(USER_DIR . 'config/versions.yaml');
$this->updater = new VersionUpdater('core/grav', __DIR__ . '/updates', GRAV_VERSION, $versions);
$this->updater->install();
}

View File

@@ -36,7 +36,7 @@ final class Versions
*/
public static function instance(string $filename = null): self
{
$filename = $filename ?? GRAV_ROOT . '/user/config/versions.yaml';
$filename = $filename ?? USER_DIR . 'config/versions.yaml';
if (!isset(self::$instance[$filename])) {
self::$instance[$filename] = new self($filename);