xref: /webtrees/app/Factories/FilesystemFactory.php (revision 9458f20a3c5e8e676f65488153a8d8a76b1773fb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Flysystem\Adapter\ChrootAdapter;
23use Fisharebest\Webtrees\Contracts\FilesystemFactoryInterface;
24use Fisharebest\Webtrees\Site;
25use Fisharebest\Webtrees\Tree;
26use League\Flysystem\Filesystem;
27use League\Flysystem\FilesystemOperator;
28use League\Flysystem\Local\LocalFilesystemAdapter;
29
30use function realpath;
31
32/**
33 * Make a filesystem.
34 */
35class FilesystemFactory implements FilesystemFactoryInterface
36{
37    private const ROOT_DIR = __DIR__ . '/../..';
38
39    /**
40     * Create a filesystem for the user's data folder.
41     *
42     * @param string $path_prefix
43     *
44     * @return FilesystemOperator
45     */
46    public function data(string $path_prefix = ''): FilesystemOperator
47    {
48        $folder     = Site::getPreference('INDEX_DIRECTORY');
49        $adapter    = new LocalFilesystemAdapter($folder);
50        $filesystem = new Filesystem($adapter);
51
52        if ($path_prefix === '') {
53            return $filesystem;
54        }
55
56        return new Filesystem(new ChrootAdapter($filesystem, $path_prefix));
57    }
58
59    /**
60     * Describe a filesystem for the user's data folder.
61     *
62     * @return string
63     */
64    public function dataName(): string
65    {
66        return Site::getPreference('INDEX_DIRECTORY');
67    }
68
69    /**
70     * Create a filesystem for a tree's media folder.
71     *
72     * @param Tree $tree
73     *
74     * @return FilesystemOperator
75     *
76     * @deprecated - Will be removed in webtrees 2.2.  Use mediaFilesystem() directly.
77     */
78    public function media(Tree $tree): FilesystemOperator
79    {
80        return $tree->mediaFilesystem();
81    }
82
83    /**
84     * Create a filesystem for the application's root folder.
85     *
86     * @param string $path_prefix
87     *
88     * @return FilesystemOperator
89     */
90    public function root(string $path_prefix = ''): FilesystemOperator
91    {
92        $folder     = self::ROOT_DIR;
93        $adapter    = new LocalFilesystemAdapter($folder);
94        $filesystem = new Filesystem($adapter);
95
96        if ($path_prefix === '') {
97            return $filesystem;
98        }
99
100        return new Filesystem(new ChrootAdapter($filesystem, $path_prefix));
101    }
102
103    /**
104     * Describe a filesystem for the application's root folder.
105     *
106     * @return string
107     */
108    public function rootName(): string
109    {
110        return realpath(self::ROOT_DIR) . '/';
111    }
112}
113