xref: /webtrees/app/Factories/FilesystemFactory.php (revision afa67798854828b1edc33dd077960ec2b18e6140)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 Fisharebest\Webtrees\Webtrees;
27use League\Flysystem\Filesystem;
28use League\Flysystem\FilesystemOperator;
29use League\Flysystem\Local\LocalFilesystemAdapter;
30
31use function realpath;
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     * @return FilesystemOperator
44     */
45    public function data(): FilesystemOperator
46    {
47        $data_dir = Site::getPreference('INDEX_DIRECTORY', Webtrees::DATA_DIR);
48
49        return new Filesystem(new LocalFilesystemAdapter($data_dir));
50    }
51
52    /**
53     * Describe a filesystem for the user's data folder.
54     *
55     * @return string
56     */
57    public function dataName(): string
58    {
59        return Site::getPreference('INDEX_DIRECTORY', Webtrees::DATA_DIR);
60    }
61
62    /**
63     * Create a filesystem for a tree's media folder.
64     *
65     * @param Tree $tree
66     *
67     * @return FilesystemOperator
68     */
69    public function media(Tree $tree): FilesystemOperator
70    {
71        $media_dir = $tree->getPreference('MEDIA_DIRECTORY', 'media/');
72        $adapter   = new ChrootAdapter($this->data(), $media_dir);
73
74        return new Filesystem($adapter);
75    }
76
77    /**
78     * Create a filesystem for the application's root folder.
79     *
80     * @return FilesystemOperator
81     */
82    public function root(): FilesystemOperator
83    {
84        return new Filesystem(new LocalFilesystemAdapter(self::ROOT_DIR));
85    }
86
87    /**
88     * Describe a filesystem for the application's root folder.
89     *
90     * @return string
91     */
92    public function rootName(): string
93    {
94        return realpath(self::ROOT_DIR) . '/';
95    }
96}
97