]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/BasePath.php
Merge pull request #8147 from annando/fetch-post
[friendica.git] / src / Util / BasePath.php
index fc9c3b5939b1706fa24b6ce032fb4d33f3637fb7..39931a84e4042d50c31ebdb4eec702d807d7b55a 100644 (file)
@@ -5,33 +5,58 @@ namespace Friendica\Util;
 class BasePath
 {
        /**
-        * @brief Returns the base filesystem path of the App
+        * @var string
+        */
+       private $baseDir;
+       /**
+        * @var array
+        */
+       private $server;
+
+       /**
+        * @param string|null $baseDir The default base path
+        * @param array       $server  server arguments
+        */
+       public function __construct(string $baseDir, array $server = [])
+       {
+               $this->baseDir = $baseDir;
+               $this->server = $server;
+       }
+
+       /**
+        * Returns the base filesystem path of the App
         *
         * It first checks for the internal variable, then for DOCUMENT_ROOT and
         * finally for PWD
         *
-        * @param string|null $basePath The default base path
-        * @param array       $server   server arguments
-        *
         * @return string
         *
         * @throws \Exception if directory isn't usable
         */
-       public static function create($basePath, array $server = [])
+       public function getPath()
        {
-               if (!$basePath && !empty($server['DOCUMENT_ROOT'])) {
-                       $basePath = $server['DOCUMENT_ROOT'];
+               $baseDir = $this->baseDir;
+               $server = $this->server;
+
+               if ((!$baseDir || !is_dir($baseDir)) && !empty($server['DOCUMENT_ROOT'])) {
+                       $baseDir = $server['DOCUMENT_ROOT'];
                }
 
-               if (!$basePath && !empty($server['PWD'])) {
-                       $basePath = $server['PWD'];
+               if ((!$baseDir || !is_dir($baseDir)) && !empty($server['PWD'])) {
+                       $baseDir = $server['PWD'];
+               }
+
+               $baseDir = self::getRealPath($baseDir);
+
+               if (!is_dir($baseDir)) {
+                       throw new \Exception(sprintf('\'%s\' is not a valid basepath', $baseDir));
                }
 
-               return self::getRealPath($basePath);
+               return $baseDir;
        }
 
        /**
-        * @brief Returns a normalized file path
+        * Returns a normalized file path
         *
         * This is a wrapper for the "realpath" function.
         * That function cannot detect the real path when some folders aren't readable.