Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Utils / FsUtils.php
1 <?php
2
3 namespace Drush\Utils;
4
5 use Drush\Drush;
6 use Drush\Sql\SqlBase;
7 use Symfony\Component\Filesystem\Filesystem;
8 use Webmozart\PathUtil\Path;
9
10 class FsUtils
11 {
12
13     /**
14      * Decide where our backup directory should go
15      *
16      * @param string $subdir
17      *   The name of the desired subdirectory(s) under drush-backups.
18      *   Usually a database name.
19      *
20      * @return
21      *   A path to the backup directory.
22      * @throws \Exception
23      */
24     public static function getBackupDir($subdir = null)
25     {
26         // Try to use db name as subdir if none was provided.
27         if (empty($subdir)) {
28             $subdir = 'unknown';
29             if ($sql = SqlBase::create()) {
30                 $db_spec = $sql->getDbSpec();
31                 $subdir = $db_spec['database'];
32             }
33         }
34
35         // Save the date to be used in the backup directory's path name.
36         $date = gmdate('YmdHis', $_SERVER['REQUEST_TIME']);
37         return Path::join(
38             Drush::config()->home(),
39             'drush-backups',
40             $subdir,
41             $date
42         );
43     }
44
45     /**
46      * Prepare a backup directory.
47      *
48      * @param string $subdir
49      *   A string naming the subdirectory of the backup directory.
50      *
51      * @return string
52      *   Path to the specified backup directory.
53      * @throws \Exception
54      */
55     public static function prepareBackupDir($subdir = null)
56     {
57         $fs = new Filesystem();
58         $backup_dir = self::getBackupDir($subdir);
59         $fs->mkdir($backup_dir);
60         return $backup_dir;
61     }
62
63     /**
64      * Returns canonicalized absolute pathname.
65      *
66      * The difference between this and PHP's realpath() is that this will
67      * return the original path even if it doesn't exist.
68      *
69      * @param string $path
70      *   The path being checked.
71      *
72      * @return string
73      *   The canonicalized absolute pathname.
74      */
75     public static function realpath($path)
76     {
77         $realpath = realpath($path);
78         return $realpath ?: $path;
79     }
80 }