xref: /webtrees/app/Factories/FilesystemFactory.php (revision 15c4f62c40de0c794536302daf7d5ec0548a1973)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Factories;
21
22use Fisharebest\Webtrees\Contracts\FilesystemFactoryInterface;
23use Fisharebest\Webtrees\Site;
24use League\Flysystem\Filesystem;
25use League\Flysystem\FilesystemOperator;
26use League\Flysystem\Local\LocalFilesystemAdapter;
27use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
28
29use function realpath;
30
31use const DIRECTORY_SEPARATOR;
32
33/**
34 * Make a filesystem.
35 */
36class FilesystemFactory implements FilesystemFactoryInterface
37{
38    private const ROOT_DIR = __DIR__ . '/../..';
39
40    /**
41     * Create a filesystem for the user's data folder.
42     *
43     * @param string $path_prefix
44     *
45     * @return FilesystemOperator
46     */
47    public function data(string $path_prefix = ''): FilesystemOperator
48    {
49        $adapter = new LocalFilesystemAdapter(Site::getPreference('INDEX_DIRECTORY'));
50
51        if ($path_prefix !== '') {
52            $adapter = new PathPrefixedAdapter($adapter, $path_prefix);
53        }
54
55        return new Filesystem($adapter);
56    }
57
58    /**
59     * Describe a filesystem for the user's data folder.
60     *
61     * @return string
62     */
63    public function dataName(): string
64    {
65        return realpath(Site::getPreference('INDEX_DIRECTORY')) . DIRECTORY_SEPARATOR;
66    }
67
68    /**
69     * Create a filesystem for the application's root folder.
70     *
71     * @param string $path_prefix
72     *
73     * @return FilesystemOperator
74     */
75    public function root(string $path_prefix = ''): FilesystemOperator
76    {
77        $adapter = new LocalFilesystemAdapter(self::ROOT_DIR);
78
79        if ($path_prefix !== '') {
80            $adapter = new PathPrefixedAdapter($adapter, $path_prefix);
81        }
82
83        return new Filesystem($adapter);
84    }
85
86    /**
87     * Describe a filesystem for the application's root folder.
88     *
89     * @return string
90     */
91    public function rootName(): string
92    {
93        return realpath(self::ROOT_DIR) . '/';
94    }
95}
96