CRM_Utils_File::absoluteDirectory() - Make `$basePath` required
authorTim Otten <totten@civicrm.org>
Wed, 10 Jul 2019 06:32:10 +0000 (23:32 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 10 Jul 2019 06:32:10 +0000 (23:32 -0700)
Previous versions interpreted `$basePath=NULL` to mean "default to `self::baseFilePath()`".
However, no code in the known `universe` relies on this interpretation, and
the `baseFilePath()` function is problematic/deprecated.

We could almost deprecate the entire function `absoluteDirectory()`;
however, it's fine to keep around: the semantics are reasonable if one
provides `$basePath`, and there is one active use-case in which
`Civi\Core\Paths` calls `absoluteDirectory()` with a suitable `$basePath`.

CRM/Utils/File.php

index b338a52a705763b2c4fa148d35a5b6551058d128..7eb974c6fde805352fbe9d6329fca8ce3a76191d 100644 (file)
@@ -666,12 +666,12 @@ HTACCESS;
 
   /**
    * @param $directory
-   * @param string|NULL $basePath
+   * @param string $basePath
    *   The base path when evaluating relative paths. Should include trailing slash.
    *
    * @return string
    */
-  public static function absoluteDirectory($directory, $basePath = NULL) {
+  public static function absoluteDirectory($directory, $basePath) {
     // check if directory is already absolute, if so return immediately
     // Note: Windows PHP accepts any mix of "/" or "\", so "C:\htdocs" or "C:/htdocs" would be a valid absolute path
     if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && preg_match(';^[a-zA-Z]:[/\\\\];', $directory)) {
@@ -683,8 +683,12 @@ HTACCESS;
       return $directory;
     }
 
-    // make everything absolute from the baseFilePath
-    $basePath = ($basePath === NULL) ? self::baseFilePath() : $basePath;
+    if ($basePath === NULL) {
+      // Previous versions interpreted `NULL` to mean "default to `self::baseFilePath()`".
+      // However, no code in the known `universe` relies on this interpretation, and
+      // the `baseFilePath()` function is problematic/deprecated.
+      throw new \RuntimeException("absoluteDirectory() requires specifying a basePath");
+    }
 
     // ensure that $basePath has a trailing slash
     $basePath = self::addTrailingSlash($basePath);