xref: /webtrees/app/Factories/FilesystemFactory.php (revision 3f17da1ad8d9a8ffdf20212b66fa8455d49562ac)
169675509SGreg Roach<?php
269675509SGreg Roach
369675509SGreg Roach/**
469675509SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
669675509SGreg Roach * This program is free software: you can redistribute it and/or modify
769675509SGreg Roach * it under the terms of the GNU General Public License as published by
869675509SGreg Roach * the Free Software Foundation, either version 3 of the License, or
969675509SGreg Roach * (at your option) any later version.
1069675509SGreg Roach * This program is distributed in the hope that it will be useful,
1169675509SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1269675509SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1369675509SGreg Roach * GNU General Public License for more details.
1469675509SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1669675509SGreg Roach */
1769675509SGreg Roach
1869675509SGreg Roachdeclare(strict_types=1);
1969675509SGreg Roach
2069675509SGreg Roachnamespace Fisharebest\Webtrees\Factories;
2169675509SGreg Roach
2269675509SGreg Roachuse Fisharebest\Webtrees\Contracts\FilesystemFactoryInterface;
2369675509SGreg Roachuse Fisharebest\Webtrees\Site;
2469675509SGreg Roachuse League\Flysystem\Filesystem;
25f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
26f7cf8a15SGreg Roachuse League\Flysystem\Local\LocalFilesystemAdapter;
2732bf54bcSGreg Roachuse League\Flysystem\PathPrefixing\PathPrefixedAdapter;
2869675509SGreg Roach
2969675509SGreg Roachuse function realpath;
3069675509SGreg Roach
31*3f17da1aSGreg Roachuse const DIRECTORY_SEPARATOR;
32*3f17da1aSGreg Roach
3369675509SGreg Roach/**
34e1994f42SGreg Roach * Make a filesystem.
3569675509SGreg Roach */
3669675509SGreg Roachclass FilesystemFactory implements FilesystemFactoryInterface
3769675509SGreg Roach{
38babdf465SGreg Roach    private const ROOT_DIR = __DIR__ . '/../..';
3969675509SGreg Roach
4069675509SGreg Roach    /**
4169675509SGreg Roach     * Create a filesystem for the user's data folder.
4269675509SGreg Roach     *
439458f20aSGreg Roach     * @param string $path_prefix
449458f20aSGreg Roach     *
45f7cf8a15SGreg Roach     * @return FilesystemOperator
4669675509SGreg Roach     */
479458f20aSGreg Roach    public function data(string $path_prefix = ''): FilesystemOperator
4869675509SGreg Roach    {
49b15a83a7SGreg Roach        $adapter = new LocalFilesystemAdapter(Site::getPreference('INDEX_DIRECTORY'));
5069675509SGreg Roach
5132bf54bcSGreg Roach        if ($path_prefix !== '') {
5232bf54bcSGreg Roach            $adapter = new PathPrefixedAdapter($adapter, $path_prefix);
539458f20aSGreg Roach        }
549458f20aSGreg Roach
5532bf54bcSGreg Roach        return new Filesystem($adapter);
5669675509SGreg Roach    }
5769675509SGreg Roach
5869675509SGreg Roach    /**
5969675509SGreg Roach     * Describe a filesystem for the user's data folder.
6069675509SGreg Roach     *
6169675509SGreg Roach     * @return string
6269675509SGreg Roach     */
6369675509SGreg Roach    public function dataName(): string
6469675509SGreg Roach    {
65*3f17da1aSGreg Roach        return realpath(Site::getPreference('INDEX_DIRECTORY')) . DIRECTORY_SEPARATOR;
6669675509SGreg Roach    }
6769675509SGreg Roach
6869675509SGreg Roach    /**
6969675509SGreg Roach     * Create a filesystem for the application's root folder.
7069675509SGreg Roach     *
719458f20aSGreg Roach     * @param string $path_prefix
729458f20aSGreg Roach     *
73f7cf8a15SGreg Roach     * @return FilesystemOperator
7469675509SGreg Roach     */
759458f20aSGreg Roach    public function root(string $path_prefix = ''): FilesystemOperator
7669675509SGreg Roach    {
77b15a83a7SGreg Roach        $adapter = new LocalFilesystemAdapter(self::ROOT_DIR);
789458f20aSGreg Roach
7932bf54bcSGreg Roach        if ($path_prefix !== '') {
8032bf54bcSGreg Roach            $adapter = new PathPrefixedAdapter($adapter, $path_prefix);
819458f20aSGreg Roach        }
829458f20aSGreg Roach
8332bf54bcSGreg Roach        return new Filesystem($adapter);
8469675509SGreg Roach    }
8569675509SGreg Roach
8669675509SGreg Roach    /**
8769675509SGreg Roach     * Describe a filesystem for the application's root folder.
8869675509SGreg Roach     *
8969675509SGreg Roach     * @return string
9069675509SGreg Roach     */
9169675509SGreg Roach    public function rootName(): string
9269675509SGreg Roach    {
9369675509SGreg Roach        return realpath(self::ROOT_DIR) . '/';
9469675509SGreg Roach    }
9569675509SGreg Roach}
96