diff --git a/core/includes/common.inc b/core/includes/common.inc index 225028a..c5e1d9d 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -871,14 +871,15 @@ function drupal_set_time_limit($time_limit) { * This function should only be used when including a file containing PHP code; * the 'module://', 'profile://' and 'theme://' stream wrappers should be used * for other use cases. - * @param $type + * + * @param string $type * The type of the item; one of 'core', 'profile', 'module', 'theme', or * 'theme_engine'. - * @param $name + * @param string $name * The name of the item for which the path is requested. Ignored for * $type 'core'. * - * @return + * @return string * The path to the requested item or an empty string if the item is not found. */ function drupal_get_path($type, $name) { diff --git a/core/lib/Drupal/Core/StreamWrapper/LocalStream.php b/core/lib/Drupal/Core/StreamWrapper/LocalStream.php index fbec988..01c3186 100644 --- a/core/lib/Drupal/Core/StreamWrapper/LocalStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/LocalStream.php @@ -125,11 +125,11 @@ protected function getLocalPath($uri = NULL) { if (!isset($uri)) { $uri = $this->uri; } - $path = $this->getDirectoryPath() . '/' . $this->getTarget($uri); + $path = $this->getDirectoryPath() . DIRECTORY_SEPARATOR . $this->getTarget($uri); $realpath = realpath($path); if (!$realpath) { // This file does not yet exist. - $realpath = realpath(dirname($path)) . '/' . drupal_basename($path); + $realpath = realpath(dirname($path)) . DIRECTORY_SEPARATOR . drupal_basename($path); } $directory = realpath($this->getDirectoryPath()); if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) { @@ -426,7 +426,7 @@ public function mkdir($uri, $mode, $options) { if ($recursive) { // $this->getLocalPath() fails if $uri has multiple levels of directories // that do not yet exist. - $localpath = $this->getDirectoryPath() . '/' . $this->getTarget($uri); + $localpath = $this->getDirectoryPath() . DIRECTORY_SEPARATOR . $this->getTarget($uri); } else { $localpath = $this->getLocalPath($uri); diff --git a/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php b/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php index a3df6e3..4e2f420 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ModuleStream.php @@ -17,7 +17,7 @@ class ModuleStream extends SystemStream { */ protected function getOwnerName() { $name = parent::getOwnerName(); - if (\Drupal::moduleHandler()->moduleExists($name)) { + if ($this->moduleHandler()->moduleExists($name)) { return $name; } else { @@ -27,12 +27,16 @@ protected function getOwnerName() { } /** - * Gets the module's directory path. - * - * @return string - * String specifying the path. + * {@inheritdoc} */ protected function getDirectoryPath() { return drupal_get_path('module', $this->getOwnerName()); } + + /** + * Wraps the ModuleHandler service. + */ + protected function moduleHandler() { + return \Drupal::moduleHandler(); + } } diff --git a/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php b/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php index eff73ed..e36ede2 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ProfileStream.php @@ -8,7 +8,7 @@ namespace Drupal\Core\StreamWrapper; /** - * Defines the read-only profile:// stream wrapper for profiles. + * Defines the read-only profile:// stream wrapper for profile files. */ class ProfileStream extends SystemStream { @@ -31,10 +31,7 @@ protected function getOwnerName() { } /** - * Gets the profile's directory path. - * - * @return string - * String specifying the path. + * {@inheritdoc} */ protected function getDirectoryPath() { return drupal_get_path('profile', $this->getOwnerName()); diff --git a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php index c337400..728bb67 100644 --- a/core/lib/Drupal/Core/StreamWrapper/SystemStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/SystemStream.php @@ -7,18 +7,21 @@ namespace Drupal\Core\StreamWrapper; -use \Drupal\Component\Utility\UrlHelper; - /** * Defines a base stream wrapper implementation. * - * This class provides a read-only Drupal stream wrapper base class for system - * files such as modules, themes and profiles. + * SystemStream is a read-only Drupal stream wrapper base class for system files + * located in modules, themes and profiles. */ abstract class SystemStream extends LocalReadOnlyStream { /** - * Get the module, theme, or profile name of the current URI. + * Gets the module, theme, or profile name of the current URI. + * + * This will return the name of the module, theme or profile e.g. + * @code SystemStream::getOwnerName('module://foo') @endcode and @code + * SystemStream::getOwnerName('module://foo/')@endcode will both return @code + * 'foo'@endcode * * @return string * The extension name. @@ -45,18 +48,7 @@ protected function getTarget($uri = NULL) { } /** - * Returns a web accessible URL for the resource. - * - * This function should return a URL that can be embedded in a web page - * and accessed from a browser. For example, the external URL of - * "youtube://xIpLd0WQKCY" might be - * "http://www.youtube.com/watch?v=xIpLd0WQKCY". - * - * @param string $uri - * An optional URI. - * - * @return string - * Returns a string containing a web accessible URL for the resource. + * {@inheritdoc} * * @throws \InvalidArgumentException */ @@ -65,7 +57,7 @@ public function getExternalUrl() { if (empty($dir)) { throw new \InvalidArgumentException(sprintf('Extension directory for %s does not exist.', $this->uri)); } - return \Drupal::request()->getUriForPath(base_path() . $dir . $this->getTarget()); + return $this->request()->getUriForPath(base_path() . $dir . $this->getTarget()); } /** @@ -79,11 +71,18 @@ public function dirname($uri = NULL) { $target = $this->getTarget($uri); $dirname = dirname($target); - if ($dirname == '.') { + if ($dirname === '.' || $dirname === '\\') { $dirname = ''; } return rtrim($scheme . '://' . $this->getOwnerName() . $dirname, '/'); } + /** + * Wraps the drupal request. + */ + protected function request() { + return \Drupal::request(); + } + } diff --git a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php index c6c4c48..31ff89e 100644 --- a/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php +++ b/core/lib/Drupal/Core/StreamWrapper/ThemeStream.php @@ -25,7 +25,7 @@ protected function getOwnerName() { $name = $this->themeHandler()->getDefault(); break; case 'admin': - $name = \Drupal::config('system.theme')->get('admin'); + $name = $this->config('system.theme')->get('admin'); break; } // Return name only for installed themes. @@ -39,10 +39,7 @@ protected function getOwnerName() { } /** - * Gets the theme's directory path. - * - * @return string - * String specifying the path. + * {@inheritdoc} */ protected function getDirectoryPath() { return drupal_get_path('theme', $this->getOwnerName()); @@ -63,4 +60,11 @@ protected function getActiveTheme() { protected function themeHandler() { return \Drupal::service('theme_handler'); } + + /** + * Wraps the drupal configuration service. + */ + protected function config($config_name) { + return \Drupal::config($config_name); + } } diff --git a/core/modules/system/src/Tests/File/SystemStreamTest.php b/core/modules/system/src/Tests/File/SystemStreamTest.php index b43b836..02b75ab 100644 --- a/core/modules/system/src/Tests/File/SystemStreamTest.php +++ b/core/modules/system/src/Tests/File/SystemStreamTest.php @@ -356,6 +356,11 @@ private function runTestOnInstanceMethod($instance, $method, array $data) { } } else { + if ($method == 'realpath') { + // realpath() returns strings with the OS-specific DIRECTORY_SEPARATOR, + // make sure we are testing for that. + $info[0] = str_replace('/', DIRECTORY_SEPARATOR, $info[0]); + } $this->assertEqual($instance->$method(), $info[0], $info[1]); } }