zend-blog-3-backend
90 строк · 1.9 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 07.10.17
6* Time: 16:33
7*/
8
9namespace App\Service;
10
11use League\Flysystem\FilesystemOperator;
12use League\Flysystem\StorageAttributes;
13
14class BackupService
15{
16public const DUMPS_PATH = '/db_dumps';
17public const DUMPS_COUNT = 14;
18public const IMAGES_PATH = '/blog_images';
19
20private FilesystemOperator $flySystem;
21
22/**
23* @param SystemParametersStorage $storage
24*/
25public 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*/
37public function fileExists(string $remotePath): bool
38{
39return $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*/
50public 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*/
63public 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*/
75public 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 */
84foreach ($listing as $item) {
85$files[] = $item->path();
86}
87
88return $files;
89}
90}
91