Issue #1856766 by chx, Berdir, heyrocker, kim.pepper: Convert file_public_path() to the new settings system (make it not configurable via UI or YAML).

8.0.x
Alex Pott 2013-08-22 02:11:07 +01:00
parent 5778af9115
commit 5460c03e89
19 changed files with 85 additions and 59 deletions

View File

@ -7,6 +7,7 @@
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage;
use Drupal\Core\StreamWrapper\PublicStream;
/**
* Stream wrapper bit flags that are the basis for composite types.
@ -385,7 +386,7 @@ function file_stream_wrapper_get_instance_by_uri($uri) {
* @param $scheme
* If the stream was "public://target", "public" would be the scheme.
*
* @return
* @return \Drupal\Core\StreamWrapper\StreamWrapperInterface
* Returns a new stream wrapper object appropriate for the given $scheme.
* For example, for the public scheme a stream wrapper object
* (Drupal\Core\StreamWrapper\PublicStream).
@ -1614,7 +1615,7 @@ function file_directory_temp() {
if (empty($temporary_directory)) {
// If no directory has been found default to 'files/tmp'.
$temporary_directory = variable_get('file_public_path', conf_path() . '/files') . '/tmp';
$temporary_directory = PublicStream::basePath() . '/tmp';
// Windows accepts paths with either slash (/) or backslash (\), but will
// not accept a path which contains both a slash and a backslash. Since

View File

@ -6,6 +6,7 @@
*/
namespace Drupal\Component\PhpStorage;
use Drupal\Core\StreamWrapper\PublicStream;
/**
* Creates a php storage object
@ -49,8 +50,7 @@ class PhpStorageFactory {
$configuration['bin'] = $name;
}
if (!isset($configuration['directory'])) {
$path = isset($conf['file_public_path']) ? $conf['file_public_path'] : conf_path() . '/files';
$configuration['directory'] = DRUPAL_ROOT . "/$path/php";
$configuration['directory'] = DRUPAL_ROOT . '/' . PublicStream::basePath() . '/php';
}
return new $class($configuration);
}

View File

@ -16,20 +16,36 @@ namespace Drupal\Core\StreamWrapper;
class PublicStream extends LocalStream {
/**
* Implements Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath()
* {@inheritdoc}
*/
public function getDirectoryPath() {
return variable_get('file_public_path', conf_path() . '/files');
return static::basePath();
}
/**
* Implements Drupal\Core\StreamWrapper\StreamWrapperInterface::getExternalUrl().
*
* @return string
* Returns the HTML URI of a public file.
* {@inheritdoc}
*/
function getExternalUrl() {
public function getExternalUrl() {
$path = str_replace('\\', '/', $this->getTarget());
return $GLOBALS['base_url'] . '/' . self::getDirectoryPath() . '/' . drupal_encode_path($path);
}
/**
* Returns the base path for public://.
*
* @return string
* The base path for public:// typically sites/default/files.
*/
public static function basePath() {
$base_path = settings()->get('file_public_path', conf_path() . '/files');
if ($test_prefix = drupal_valid_test_ua()) {
// Append the testing suffix unless already given.
// @see Drupal\simpletest\WebTestBase::setUp()
if (strpos($base_path, '/simpletest/' . substr($test_prefix, 10)) === FALSE) {
return $base_path . '/simpletest/' . substr($test_prefix, 10);
}
}
return $base_path;
}
}

View File

@ -7,6 +7,7 @@
namespace Drupal\filter\Tests;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\WebTestBase;
/**
@ -75,7 +76,7 @@ class FilterHtmlImageSecureTest extends WebTestBase {
function testImageSource() {
global $base_url;
$public_files_path = variable_get('file_public_path', conf_path() . '/files');
$public_files_path = PublicStream::basePath();
$http_base_url = preg_replace('/^https?/', 'http', $base_url);
$https_base_url = preg_replace('/^https?/', 'https', $base_url);

View File

@ -226,25 +226,6 @@ function image_permission() {
);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function image_form_system_file_system_settings_alter(&$form, &$form_state) {
$form['#submit'][] = 'image_system_file_system_settings_submit';
}
/**
* Form submission handler for system_file_system_settings().
*
* Adds a menu rebuild after the public file path has been changed, so that the
* menu router item depending on that file path will be regenerated.
*/
function image_system_file_system_settings_submit($form, &$form_state) {
if ($form['file_public_path']['#default_value'] !== $form_state['values']['file_public_path']) {
Drupal::state()->set('menu_rebuild_needed', TRUE);
}
}
/**
* Implements hook_file_download().
*

View File

@ -7,6 +7,7 @@
namespace Drupal\locale\Tests;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\WebTestBase;
use Drupal\Component\Utility\String;
@ -172,7 +173,7 @@ EOF;
\Drupal::state()->set('locale.test_projects_alter', TRUE);
// Setup the environment.
$public_path = variable_get('file_public_path', conf_path() . '/files');
$public_path = PublicStream::basePath();
$this->setTranslationsDirectory($public_path . '/local');
$config->set('translation.default_filename', '%project-%version.%language._po')->save();

View File

@ -4,6 +4,7 @@
* @file
* Simulate a custom module with a local po file.
*/
use Drupal\Core\StreamWrapper\PublicStream;
/**
* Implements hook_system_info_alter().
@ -42,7 +43,7 @@ function locale_test_locale_translation_projects_alter(&$projects) {
// Instead of the default ftp.drupal.org we use the file system of the test
// instance to simulate a remote file location.
$url = url(NULL, array('absolute' => TRUE));
$remote_url = $url . variable_get('file_public_path', conf_path() . '/files') . '/remote/';
$remote_url = $url . PublicStream::basePath() . '/remote/';
// Completely replace the project data with a set of test projects.
$base_url = url();

View File

@ -17,6 +17,7 @@ use Drupal\Core\Database\ConnectionNotDefinedException;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Language\Language;
use Drupal\Core\StreamWrapper\PublicStream;
use ReflectionMethod;
use ReflectionObject;
@ -708,7 +709,7 @@ abstract class TestBase {
if ($simpletest_config->get('verbose')) {
// Initialize verbose debugging.
$this->verbose = TRUE;
$this->verboseDirectory = variable_get('file_public_path', conf_path() . '/files') . '/simpletest/verbose';
$this->verboseDirectory = PublicStream::basePath() . '/simpletest/verbose';
$this->verboseDirectoryUrl = file_create_url($this->verboseDirectory);
if (file_prepare_directory($this->verboseDirectory, FILE_CREATE_DIRECTORY) && !file_exists($this->verboseDirectory . '/.htaccess')) {
file_put_contents($this->verboseDirectory . '/.htaccess', "<IfModule mod_expires.c>\nExpiresActive Off\n</IfModule>\n");
@ -888,7 +889,9 @@ abstract class TestBase {
$this->originalTheme = isset($GLOBALS['theme']) ? $GLOBALS['theme'] : NULL;
// Save further contextual information.
$this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
// Use the original files directory to avoid nesting it within an existing
// simpletest directory if a test is executed within a test.
$this->originalFileDirectory = settings()->get('file_public_path', conf_path() . '/files');
$this->originalProfile = drupal_get_profile();
$this->originalUser = isset($user) ? clone $user : NULL;

View File

@ -56,7 +56,7 @@ abstract class UnitTestBase extends TestBase {
$conf = array();
drupal_static_reset();
$conf['file_public_path'] = $this->public_files_directory;
$this->settingsSet('file_public_path', $this->public_files_directory);
// Change the database prefix.
// All static variables need to be reset before the database prefix is

View File

@ -16,6 +16,7 @@ use Drupal\Core\Database\ConnectionNotDefinedException;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\UserSession;
use Drupal\Core\StreamWrapper\PublicStream;
use PDO;
use stdClass;
use DOMDocument;
@ -416,7 +417,7 @@ abstract class WebTestBase extends TestBase {
$original = drupal_get_path('module', 'simpletest') . '/files';
$files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
foreach ($files as $file) {
file_unmanaged_copy($file->uri, variable_get('file_public_path', conf_path() . '/files'));
file_unmanaged_copy($file->uri, PublicStream::basePath());
}
$this->generatedTestFiles = TRUE;
@ -772,7 +773,7 @@ abstract class WebTestBase extends TestBase {
NestedArray::setValue($GLOBALS['conf'], array_merge(array($config_base), explode('.', $name)), $value);
}
}
$GLOBALS['conf']['file_public_path'] = $this->public_files_directory;
$this->settingsSet('file_public_path', $this->public_files_directory);
// Execute the non-interactive installer.
require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
$this->settingsSet('cache', array('default' => 'cache.backend.memory'));
@ -820,7 +821,6 @@ abstract class WebTestBase extends TestBase {
}
$config->save();
}
variable_set('file_public_path', $this->public_files_directory);
// Use the test mail class instead of the default mail handler class.
\Drupal::config('system.mail')->set('interface.default', 'Drupal\Core\Mail\VariableLog')->save();

View File

@ -7,6 +7,7 @@
namespace Drupal\system\Form;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\system\SystemConfigFormBase;
/**
@ -27,12 +28,11 @@ class FileSystemForm extends SystemConfigFormBase {
public function buildForm(array $form, array &$form_state) {
$config = $this->configFactory->get('system.file');
$form['file_public_path'] = array(
'#type' => 'textfield',
'#type' => 'item',
'#title' => t('Public file system path'),
'#default_value' => variable_get('file_public_path', conf_path() . '/files'),
'#maxlength' => 255,
'#description' => t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web.'),
'#after_build' => array('system_check_directory'),
'#default_value' => PublicStream::basePath(),
'#markup' => PublicStream::basePath(),
'#description' => t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web. This must be changed in settings.php'),
);
$form['file_private_path'] = array(
@ -78,7 +78,6 @@ class FileSystemForm extends SystemConfigFormBase {
$config = $this->configFactory->get('system.file')
->set('path.private', $form_state['values']['file_private_path'])
->set('path.temporary', $form_state['values']['file_temporary_path']);
variable_set('file_public_path', $form_state['values']['file_public_path']);
if (isset($form_state['values']['file_default_scheme'])) {
$config->set('default_scheme', $form_state['values']['file_default_scheme']);

View File

@ -7,6 +7,7 @@
namespace Drupal\system\Form;
use Drupal\Core\StreamWrapper\PublicStream;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Cache\Cache;
@ -240,7 +241,7 @@ class ThemeSettingsForm extends SystemConfigFormBase {
// Prepare local file path for description.
if ($original_path && isset($friendly_path)) {
$local_file = strtr($original_path, array('public:/' => variable_get('file_public_path', conf_path() . '/files')));
$local_file = strtr($original_path, array('public:/' => PublicStream::basePath()));
}
elseif ($theme_name) {
$local_file = drupal_get_path('theme', $theme_name) . '/' . $default;

View File

@ -37,7 +37,6 @@ class ConfigTest extends FileTestBase {
// upon form submission.
$file_path = $this->public_files_directory;
$fields = array(
'file_public_path' => $file_path . '/file_config_page_test/public',
'file_private_path' => $file_path . '/file_config_page_test/private',
'file_temporary_path' => $file_path . '/file_config_page_test/temporary',
'file_default_scheme' => 'private',

View File

@ -7,6 +7,7 @@
namespace Drupal\system\Tests\File;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\WebTestBase;
/**
@ -82,7 +83,7 @@ class StreamWrapperTest extends WebTestBase {
// Test file_build_uri() and
// Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath().
$this->assertEqual(file_build_uri('foo/bar.txt'), 'public://foo/bar.txt', 'Expected scheme was added.');
$this->assertEqual(file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath(), variable_get('file_public_path'), 'Expected default directory path was returned.');
$this->assertEqual(file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath(), PublicStream::basePath(), 'Expected default directory path was returned.');
$this->assertEqual(file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath(), $config->get('path.temporary'), 'Expected temporary directory path was returned.');
$config->set('default_scheme', 'private')->save();
$this->assertEqual(file_build_uri('foo/bar.txt'), 'private://foo/bar.txt', 'Got a valid URI from foo/bar.txt.');

View File

@ -8,6 +8,7 @@
namespace Drupal\system\Tests\FileTransfer;
use Drupal\Core\FileTransfer\FileTransferException;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\WebTestBase;
/**
@ -91,7 +92,7 @@ class FileTransferTest extends WebTestBase {
$gotit = TRUE;
try {
$this->testConnection->copyDirectory($source, DRUPAL_ROOT . '/'. variable_get('file_public_path', conf_path() . '/files'));
$this->testConnection->copyDirectory($source, DRUPAL_ROOT . '/' . PublicStream::basePath());
}
catch (FileTransferException $e) {
$gotit = FALSE;

View File

@ -7,6 +7,7 @@
namespace Drupal\system\Tests\System;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\WebTestBase;
/**
@ -45,7 +46,7 @@ class ThemeTest extends WebTestBase {
function testThemeSettings() {
// Specify a filesystem path to be used for the logo.
$file = current($this->drupalGetTestFiles('image'));
$file_relative = strtr($file->uri, array('public:/' => variable_get('file_public_path', conf_path() . '/files')));
$file_relative = strtr($file->uri, array('public:/' => PublicStream::basePath()));
$default_theme_path = 'core/themes/stark';
$supported_paths = array(
@ -97,7 +98,7 @@ class ThemeTest extends WebTestBase {
if (file_uri_scheme($input) == 'public') {
$implicit_public_file = file_uri_target($input);
$explicit_file = $input;
$local_file = strtr($input, array('public:/' => variable_get('file_public_path', conf_path() . '/files')));
$local_file = strtr($input, array('public:/' => PublicStream::basePath()));
}
// Adjust for fully qualified stream wrapper URI elsewhere.
elseif (file_uri_scheme($input) !== FALSE) {
@ -107,7 +108,7 @@ class ThemeTest extends WebTestBase {
elseif ($input == file_uri_target($file->uri)) {
$implicit_public_file = $input;
$explicit_file = 'public://' . $input;
$local_file = variable_get('file_public_path', conf_path() . '/files') . '/' . $input;
$local_file = PublicStream::basePath() . '/' . $input;
}
$this->assertEqual((string) $elements[0], $implicit_public_file);
$this->assertEqual((string) $elements[1], $explicit_file);
@ -134,9 +135,9 @@ class ThemeTest extends WebTestBase {
// Relative path within the public filesystem to non-existing file.
'whatever.png',
// Relative path to non-existing file in public filesystem.
variable_get('file_public_path', conf_path() . '/files') . '/whatever.png',
PublicStream::basePath() . '/whatever.png',
// Semi-absolute path to non-existing file in public filesystem.
'/' . variable_get('file_public_path', conf_path() . '/files') . '/whatever.png',
'/' . PublicStream::basePath() . '/whatever.png',
// Relative path to arbitrary non-existing file.
'core/misc/whatever.png',
// Semi-absolute path to arbitrary non-existing file.

View File

@ -9,6 +9,7 @@ use Drupal\Component\Utility\Crypt;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Database\Database;
use Drupal\Core\Language\Language;
use Drupal\Core\StreamWrapper\PublicStream;
/**
* Implements hook_requirements().
@ -302,7 +303,7 @@ function system_requirements($phase) {
if ($phase != 'install') {
$filesystem_config = Drupal::config('system.file');
$directories = array(
variable_get('file_public_path', conf_path() . '/files'),
PublicStream::basePath(),
// By default no private files directory is configured. For private files
// to be secure the admin needs to provide a path outside the webroot.
$filesystem_config->get('path.private'),
@ -315,8 +316,8 @@ function system_requirements($phase) {
if ($phase == 'install') {
global $conf;
$directories = array();
if (!empty($conf['file_public_path'])) {
$directories[] = $conf['file_public_path'];
if ($file_public_path = settings()->get('file_public_path')) {
$directories[] = $file_public_path;
}
else {
// If we are installing Drupal, the settings.php file might not exist yet
@ -2241,6 +2242,15 @@ function system_update_8059() {
));
}
/**
* Move the file_public_path variable to settings.
*/
function system_upgrade_8060() {
if ($path = update_variable_get('file_public_path')) {
drupal_rewrite_settings(array('settings' => array('file_public_path' => $path)));
}
}
/**
* @} End of "defgroup updates-7.x-to-8.x".
* The next series of updates should start at 9000.

View File

@ -3,6 +3,7 @@
* @file
* This script runs Drupal tests from command line.
*/
use Drupal\Core\StreamWrapper\PublicStream;
const SIMPLETEST_SCRIPT_COLOR_PASS = 32; // Green.
const SIMPLETEST_SCRIPT_COLOR_FAIL = 31; // Red.
@ -399,7 +400,7 @@ function simpletest_script_execute_batch($test_classes) {
echo 'FATAL ' . $child['class'] . ': test runner returned a non-zero error code (' . $status['exitcode'] . ').' . "\n";
if ($args['die-on-fail']) {
list($db_prefix, ) = simpletest_last_test_get($child['test_id']);
$public_files = variable_get('file_public_path', conf_path() . '/files');
$public_files = PublicStream::basePath();
$test_directory = $public_files . '/simpletest/' . substr($db_prefix, 10);
echo 'Simpletest database and files kept and test exited immediately on fail so should be reproducible if you change settings.php to use the database prefix '. $db_prefix . ' and config directories in '. $test_directory . "\n";
$args['keep-results'] = TRUE;
@ -568,7 +569,7 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) {
// Check whether a test file directory was setup already.
// @see prepareEnvironment()
$public_files = variable_get('file_public_path', conf_path() . '/files');
$public_files = PublicStream::basePath();
$test_directory = $public_files . '/simpletest/' . substr($db_prefix, 10);
if (is_dir($test_directory)) {
// Output the error_log.

View File

@ -454,6 +454,15 @@ $settings['update_free_access'] = FALSE;
*/
# $settings['mixed_mode_sessions'] = TRUE;
/**
* Public file path:
*
* A local file system path where public files will be stored. This directory
* must exist and be writable by Drupal. This directory must be relative to
* the Drupal installation directory and be accessible over the web.
*/
# $settings['file_public_path'] = 'sites/default/files';
/**
* Session write interval:
*