xref: /webtrees/app/Factories/FilesystemFactory.php (revision b45eba7a8f95143507ff14772c1e2a243489ae21)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://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\Adapter\Local;
28use League\Flysystem\Cached\CachedAdapter;
29use League\Flysystem\Cached\Storage\Memory;
30use League\Flysystem\Filesystem;
31use League\Flysystem\FilesystemInterface;
32
33use function realpath;
34
35/**
36 * Make a filesyste,.
37 */
38class FilesystemFactory implements FilesystemFactoryInterface
39{
40    private const ROOT_DIR = __DIR__ . '/../../..';
41
42    /**
43     * Create a filesystem for the user's data folder.
44     *
45     * @return FilesystemInterface
46     */
47    public function data(): FilesystemInterface
48    {
49        $data_dir = Site::getPreference('INDEX_DIRECTORY', Webtrees::DATA_DIR);
50
51        return new Filesystem(new CachedAdapter(new Local($data_dir), new Memory()));
52    }
53
54    /**
55     * Describe a filesystem for the user's data folder.
56     *
57     * @return string
58     */
59    public function dataName(): string
60    {
61        return Site::getPreference('INDEX_DIRECTORY', Webtrees::DATA_DIR);
62    }
63
64    /**
65     * Create a filesystem for a tree's media folder.
66     *
67     * @param Tree $tree
68     *
69     * @return FilesystemInterface
70     */
71    public function media(Tree $tree): FilesystemInterface
72    {
73        $media_dir = $tree->getPreference('MEDIA_DIRECTORY', 'media/');
74        $adapter   = new ChrootAdapter($this->data(), $media_dir);
75
76        return new Filesystem($adapter);
77    }
78
79    /**
80     * Create a filesystem for the application's root folder.
81     *
82     * @return FilesystemInterface
83     */
84    public function root(): FilesystemInterface
85    {
86        return new Filesystem(new CachedAdapter(new Local(self::ROOT_DIR), new Memory()));
87    }
88
89    /**
90     * Describe a filesystem for the application's root folder.
91     *
92     * @return string
93     */
94    public function rootName(): string
95    {
96        return realpath(self::ROOT_DIR) . '/';
97    }
98}
99