updated core to 1.7.15
This commit is contained in:
@@ -1,44 +1,96 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav.Common.Config
|
||||
* @package Grav\Common\Config
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Common\Config;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Grav\Common\File\CompiledYamlFile;
|
||||
use Grav\Common\Data\Data;
|
||||
use Grav\Common\Utils;
|
||||
use InvalidArgumentException;
|
||||
use Pimple\Container;
|
||||
use RocketTheme\Toolbox\File\YamlFile;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
use RuntimeException;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Class Setup
|
||||
* @package Grav\Common\Config
|
||||
*/
|
||||
class Setup extends Data
|
||||
{
|
||||
/**
|
||||
* @var array Environment aliases normalized to lower case.
|
||||
*/
|
||||
public static $environments = [
|
||||
'' => 'unknown',
|
||||
'127.0.0.1' => 'localhost',
|
||||
'::1' => 'localhost'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string|null Current environment normalized to lower case.
|
||||
*/
|
||||
public static $environment;
|
||||
|
||||
/** @var array */
|
||||
protected $streams = [
|
||||
'system' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'prefixes' => [
|
||||
'' => ['system'],
|
||||
]
|
||||
],
|
||||
'user' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => ['user'],
|
||||
'' => [] // Set in constructor
|
||||
]
|
||||
],
|
||||
'cache' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => [], // Set in constructor
|
||||
'images' => ['images']
|
||||
]
|
||||
],
|
||||
'log' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => [] // Set in constructor
|
||||
]
|
||||
],
|
||||
'tmp' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => [] // Set in constructor
|
||||
]
|
||||
],
|
||||
'backup' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => [] // Set in constructor
|
||||
]
|
||||
],
|
||||
'environment' => [
|
||||
'type' => 'ReadOnlyStream'
|
||||
// If not defined, environment will be set up in the constructor.
|
||||
],
|
||||
'asset' => [
|
||||
'system' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'prefixes' => [
|
||||
'' => ['system'],
|
||||
]
|
||||
],
|
||||
'asset' => [
|
||||
'type' => 'Stream',
|
||||
'prefixes' => [
|
||||
'' => ['assets'],
|
||||
]
|
||||
@@ -46,13 +98,13 @@ class Setup extends Data
|
||||
'blueprints' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'prefixes' => [
|
||||
'' => ['environment://blueprints', 'user://blueprints', 'system/blueprints'],
|
||||
'' => ['environment://blueprints', 'user://blueprints', 'system://blueprints'],
|
||||
]
|
||||
],
|
||||
'config' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'prefixes' => [
|
||||
'' => ['environment://config', 'user://config', 'system/config'],
|
||||
'' => ['environment://config', 'user://config', 'system://config'],
|
||||
]
|
||||
],
|
||||
'plugins' => [
|
||||
@@ -76,40 +128,11 @@ class Setup extends Data
|
||||
'languages' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'prefixes' => [
|
||||
'' => ['environment://languages', 'user://languages', 'system/languages'],
|
||||
]
|
||||
],
|
||||
'cache' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => ['cache'],
|
||||
'images' => ['images']
|
||||
]
|
||||
],
|
||||
'log' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => ['logs']
|
||||
]
|
||||
],
|
||||
'backup' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => ['backup']
|
||||
]
|
||||
],
|
||||
'tmp' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => ['tmp']
|
||||
'' => ['environment://languages', 'user://languages', 'system://languages'],
|
||||
]
|
||||
],
|
||||
'image' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'type' => 'Stream',
|
||||
'prefixes' => [
|
||||
'' => ['user://images', 'system://images']
|
||||
]
|
||||
@@ -120,6 +143,13 @@ class Setup extends Data
|
||||
'' => ['user://pages']
|
||||
]
|
||||
],
|
||||
'user-data' => [
|
||||
'type' => 'Stream',
|
||||
'force' => true,
|
||||
'prefixes' => [
|
||||
'' => ['user://data']
|
||||
]
|
||||
],
|
||||
'account' => [
|
||||
'type' => 'ReadOnlyStream',
|
||||
'prefixes' => [
|
||||
@@ -133,13 +163,58 @@ class Setup extends Data
|
||||
*/
|
||||
public function __construct($container)
|
||||
{
|
||||
$environment = null !== static::$environment ? static::$environment : ($container['uri']->environment() ?: 'localhost');
|
||||
// Configure main streams.
|
||||
$abs = str_starts_with(GRAV_SYSTEM_PATH, '/');
|
||||
$this->streams['system']['prefixes'][''] = $abs ? ['system', GRAV_SYSTEM_PATH] : ['system'];
|
||||
$this->streams['user']['prefixes'][''] = [GRAV_USER_PATH];
|
||||
$this->streams['cache']['prefixes'][''] = [GRAV_CACHE_PATH];
|
||||
$this->streams['log']['prefixes'][''] = [GRAV_LOG_PATH];
|
||||
$this->streams['tmp']['prefixes'][''] = [GRAV_TMP_PATH];
|
||||
$this->streams['backup']['prefixes'][''] = [GRAV_BACKUP_PATH];
|
||||
|
||||
// If environment is not set, look for the environment variable and then the constant.
|
||||
$environment = static::$environment ??
|
||||
(defined('GRAV_ENVIRONMENT') ? GRAV_ENVIRONMENT : (getenv('GRAV_ENVIRONMENT') ?: null));
|
||||
|
||||
// If no environment is set, make sure we get one (CLI or hostname).
|
||||
if (null === $environment) {
|
||||
if (defined('GRAV_CLI')) {
|
||||
$environment = 'cli';
|
||||
} else {
|
||||
/** @var ServerRequestInterface $request */
|
||||
$request = $container['request'];
|
||||
$host = $request->getUri()->getHost();
|
||||
|
||||
$environment = Utils::substrToString($host, ':');
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve server aliases to the proper environment.
|
||||
static::$environment = static::$environments[$environment] ?? $environment;
|
||||
|
||||
// Pre-load setup.php which contains our initial configuration.
|
||||
// Configuration may contain dynamic parts, which is why we need to always load it.
|
||||
// If "GRAVE_SETUP_PATH" has been defined, use it, otherwise use defaults.
|
||||
$file = defined('GRAV_SETUP_PATH') ? GRAV_SETUP_PATH : GRAV_ROOT . '/setup.php';
|
||||
$setup = is_file($file) ? (array) include $file : [];
|
||||
// If GRAV_SETUP_PATH has been defined, use it, otherwise use defaults.
|
||||
$setupFile = defined('GRAV_SETUP_PATH') ? GRAV_SETUP_PATH : (getenv('GRAV_SETUP_PATH') ?: null);
|
||||
if (null !== $setupFile) {
|
||||
// Make sure that the custom setup file exists. Terminates the script if not.
|
||||
if (!str_starts_with($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_WEBROOT . '/setup.php';
|
||||
if (!is_file($setupFile)) {
|
||||
$setupFile = GRAV_WEBROOT . '/' . GRAV_USER_PATH . '/setup.php';
|
||||
}
|
||||
if (!is_file($setupFile)) {
|
||||
$setupFile = null;
|
||||
}
|
||||
}
|
||||
$setup = $setupFile ? (array) include $setupFile : [];
|
||||
|
||||
// Add default streams defined in beginning of the class.
|
||||
if (!isset($setup['streams']['schemes'])) {
|
||||
@@ -150,19 +225,41 @@ class Setup extends Data
|
||||
// Initialize class.
|
||||
parent::__construct($setup);
|
||||
|
||||
$this->def('environment', static::$environment);
|
||||
|
||||
// Figure out path for the current environment.
|
||||
$envPath = defined('GRAV_ENVIRONMENT_PATH') ? GRAV_ENVIRONMENT_PATH : (getenv('GRAV_ENVIRONMENT_PATH') ?: null);
|
||||
if (null === $envPath) {
|
||||
// Find common path for all environments and append current environment into it.
|
||||
$envPath = defined('GRAV_ENVIRONMENTS_PATH') ? GRAV_ENVIRONMENTS_PATH : (getenv('GRAV_ENVIRONMENTS_PATH') ?: null);
|
||||
if (null !== $envPath) {
|
||||
$envPath .= '/';
|
||||
} else {
|
||||
// Use default location. Start with Grav 1.7 default.
|
||||
$envPath = GRAV_WEBROOT. '/' . GRAV_USER_PATH . '/env';
|
||||
if (is_dir($envPath)) {
|
||||
$envPath = 'user://env/';
|
||||
} else {
|
||||
// Fallback to Grav 1.6 default.
|
||||
$envPath = 'user://';
|
||||
}
|
||||
}
|
||||
$envPath .= $this->get('environment');
|
||||
}
|
||||
|
||||
// Set up environment.
|
||||
$this->def('environment', $environment ?: 'cli');
|
||||
$this->def('streams.schemes.environment.prefixes', ['' => $environment ? ["user://{$this->environment}"] : []]);
|
||||
$this->def('environment', static::$environment);
|
||||
$this->def('streams.schemes.environment.prefixes', ['' => [$envPath]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
* @throws \RuntimeException
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws RuntimeException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$locator = new UniformResourceLocator(GRAV_ROOT);
|
||||
$locator = new UniformResourceLocator(GRAV_WEBROOT);
|
||||
$files = [];
|
||||
|
||||
$guard = 5;
|
||||
@@ -186,7 +283,7 @@ class Setup extends Data
|
||||
} while (--$guard);
|
||||
|
||||
if (!$guard) {
|
||||
throw new \RuntimeException('Setup: Configuration reload loop detected!');
|
||||
throw new RuntimeException('Setup: Configuration reload loop detected!');
|
||||
}
|
||||
|
||||
// Make sure we have valid setup.
|
||||
@@ -199,7 +296,8 @@ class Setup extends Data
|
||||
* Initialize resource locator by using the configuration.
|
||||
*
|
||||
* @param UniformResourceLocator $locator
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
* @throws BadMethodCallException
|
||||
*/
|
||||
public function initializeLocator(UniformResourceLocator $locator)
|
||||
{
|
||||
@@ -212,8 +310,8 @@ class Setup extends Data
|
||||
$locator->addPath($scheme, '', $config['paths']);
|
||||
}
|
||||
|
||||
$override = isset($config['override']) ? $config['override'] : false;
|
||||
$force = isset($config['force']) ? $config['force'] : false;
|
||||
$override = $config['override'] ?? false;
|
||||
$force = $config['force'] ?? false;
|
||||
|
||||
if (isset($config['prefixes'])) {
|
||||
foreach ((array)$config['prefixes'] as $prefix => $paths) {
|
||||
@@ -232,7 +330,7 @@ class Setup extends Data
|
||||
{
|
||||
$schemes = [];
|
||||
foreach ((array) $this->get('streams.schemes') as $scheme => $config) {
|
||||
$type = !empty($config['type']) ? $config['type'] : 'ReadOnlyStream';
|
||||
$type = $config['type'] ?? 'ReadOnlyStream';
|
||||
if ($type[0] !== '\\') {
|
||||
$type = '\\RocketTheme\\Toolbox\\StreamWrapper\\' . $type;
|
||||
}
|
||||
@@ -245,24 +343,51 @@ class Setup extends Data
|
||||
|
||||
/**
|
||||
* @param UniformResourceLocator $locator
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws \BadMethodCallException
|
||||
* @throws \RuntimeException
|
||||
* @return void
|
||||
* @throws InvalidArgumentException
|
||||
* @throws BadMethodCallException
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function check(UniformResourceLocator $locator)
|
||||
{
|
||||
$streams = isset($this->items['streams']['schemes']) ? $this->items['streams']['schemes'] : null;
|
||||
$streams = $this->items['streams']['schemes'] ?? null;
|
||||
if (!is_array($streams)) {
|
||||
throw new \InvalidArgumentException('Configuration is missing streams.schemes!');
|
||||
throw new InvalidArgumentException('Configuration is missing streams.schemes!');
|
||||
}
|
||||
$diff = array_keys(array_diff_key($this->streams, $streams));
|
||||
if ($diff) {
|
||||
throw new \InvalidArgumentException(
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Configuration is missing keys %s from streams.schemes!', implode(', ', $diff))
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// If environment is found, remove all missing override locations (B/C compatibility).
|
||||
if ($locator->findResource('environment://', true)) {
|
||||
$force = $this->get('streams.schemes.environment.force', false);
|
||||
if (!$force) {
|
||||
$prefixes = $this->get('streams.schemes.environment.prefixes.');
|
||||
$update = false;
|
||||
foreach ($prefixes as $i => $prefix) {
|
||||
if ($locator->isStream($prefix)) {
|
||||
if ($locator->findResource($prefix, true)) {
|
||||
break;
|
||||
}
|
||||
} elseif (file_exists($prefix)) {
|
||||
break;
|
||||
}
|
||||
|
||||
unset($prefixes[$i]);
|
||||
$update = true;
|
||||
}
|
||||
|
||||
if ($update) {
|
||||
$this->set('streams.schemes.environment.prefixes', ['' => array_values($prefixes)]);
|
||||
$this->initializeLocator($locator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$locator->findResource('environment://config', true)) {
|
||||
// If environment does not have its own directory, remove it from the lookup.
|
||||
$this->set('streams.schemes.environment.prefixes', ['config' => []]);
|
||||
@@ -271,13 +396,17 @@ class Setup extends Data
|
||||
|
||||
// Create security.yaml if it doesn't exist.
|
||||
$filename = $locator->findResource('config://security.yaml', true, true);
|
||||
$file = YamlFile::instance($filename);
|
||||
if (!$file->exists()) {
|
||||
$file->save(['salt' => Utils::generateRandomString(14)]);
|
||||
$file->free();
|
||||
$security_file = CompiledYamlFile::instance($filename);
|
||||
$security_content = (array)$security_file->content();
|
||||
|
||||
if (!isset($security_content['salt'])) {
|
||||
$security_content = array_merge($security_content, ['salt' => Utils::generateRandomString(14)]);
|
||||
$security_file->content($security_content);
|
||||
$security_file->save();
|
||||
$security_file->free();
|
||||
}
|
||||
} catch (\RuntimeException $e) {
|
||||
throw new \RuntimeException(sprintf('Grav failed to initialize: %s', $e->getMessage()), 500, $e);
|
||||
} catch (RuntimeException $e) {
|
||||
throw new RuntimeException(sprintf('Grav failed to initialize: %s', $e->getMessage()), 500, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user