zend-blog-3-backend

Форк
0
/
BackupService.php 
90 строк · 1.9 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 07.10.17
6
 * Time: 16:33
7
 */
8

9
namespace App\Service;
10

11
use League\Flysystem\FilesystemOperator;
12
use League\Flysystem\StorageAttributes;
13

14
class BackupService
15
{
16
    public const DUMPS_PATH = '/db_dumps';
17
    public const DUMPS_COUNT = 14;
18
    public const IMAGES_PATH = '/blog_images';
19

20
    private FilesystemOperator $flySystem;
21

22
    /**
23
     * @param SystemParametersStorage $storage
24
     */
25
    public function __construct(FilesystemOperator $flySystem)
26
    {
27
        $this->flySystem = $flySystem;
28
    }
29

30
    /**
31
     * @param string $remotePath
32
     *
33
     * @throws \League\Flysystem\FilesystemException
34
     *
35
     * @return bool
36
     */
37
    public function fileExists(string $remotePath): bool
38
    {
39
        return $this->flySystem->fileExists($remotePath);
40
    }
41

42
    /**
43
     * @param string $localPath
44
     * @param string $remotePath
45
     *
46
     * @throws \League\Flysystem\FilesystemException
47
     *
48
     * @return void
49
     */
50
    public function upload(string $localPath, string $remotePath): void
51
    {
52
        $fp = fopen($localPath, 'rb');
53
        $this->flySystem->writeStream($remotePath, $fp);
54
    }
55

56
    /**
57
     * @param string $path
58
     *
59
     * @throws \League\Flysystem\FilesystemException
60
     *
61
     * @return void
62
     */
63
    public function delete(string $path): void
64
    {
65
        $this->flySystem->delete($path);
66
    }
67

68
    /**
69
     * @param string $dir
70
     *
71
     * @throws \League\Flysystem\FilesystemException
72
     *
73
     * @return string[]
74
     */
75
    public function filesByDir(string $dir): array
76
    {
77
        $files = [];
78
        $listing = $this
79
            ->flySystem
80
            ->listContents($dir)
81
            ->filter(fn (StorageAttributes $attributes) => $attributes->isFile())
82
        ;
83
        /* @var StorageAttributes $item */
84
        foreach ($listing as $item) {
85
            $files[] = $item->path();
86
        }
87

88
        return $files;
89
    }
90
}
91

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.