Passed
Push — main ( 986279...90b18d )
by Sugeng
04:00
created

AbstractComponent::normalizePath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use League\Flysystem\Config;
6
use League\Flysystem\Filesystem;
7
use League\Flysystem\FilesystemAdapter;
8
use yii\base\Component;
9
use yii\helpers\FileHelper;
10
11
/**
12
 * Class AbstractComponent
13
 *
14
 * @package diecoding\flysystem
15
 * 
16
 * @method \League\Flysystem\FilesystemInterface addPlugin(\League\Flysystem\PluginInterface $plugin)
17
 * @method void assertAbsent(string $path)
18
 * @method void assertPresent(string $path)
19
 * @method boolean copy(string $path, string $newpath)
20
 * @method boolean createDir(string $dirname, array $config = null)
21
 * @method boolean delete(string $path)
22
 * @method boolean deleteDir(string $dirname)
23
 * @method \League\Flysystem\Handler get(string $path, \League\Flysystem\Handler $handler = null)
24
 * @method \League\Flysystem\AdapterInterface getAdapter()
25
 * @method \League\Flysystem\Config getConfig()
26
 * @method array|false getMetadata(string $path)
27
 * @method string|false getMimetype(string $path)
28
 * @method integer|false getSize(string $path)
29
 * @method integer|false getTimestamp(string $path)
30
 * @method string|false getVisibility(string $path)
31
 * @method array getWithMetadata(string $path, array $metadata)
32
 * @method boolean has(string $path)
33
 * @method array listContents(string $directory = '', boolean $recursive = false)
34
 * @method array listFiles(string $path = '', boolean $recursive = false)
35
 * @method array listPaths(string $path = '', boolean $recursive = false)
36
 * @method array listWith(array $keys = [], $directory = '', $recursive = false)
37
 * @method boolean put(string $path, string $contents, array $config = [])
38
 * @method boolean putStream(string $path, resource $resource, array $config = [])
39
 * @method string|false read(string $path)
40
 * @method string|false readAndDelete(string $path)
41
 * @method resource|false readStream(string $path)
42
 * @method boolean rename(string $path, string $newpath)
43
 * @method boolean setVisibility(string $path, string $visibility)
44
 * @method boolean update(string $path, string $contents, array $config = [])
45
 * @method boolean updateStream(string $path, resource $resource, array $config = [])
46
 * @method boolean write(string $path, string $contents, array $config = [])
47
 * @method boolean writeStream(string $path, resource $resource, array $config = [])
48
 * 
49
 * @link      https://sugengsulistiyawan.my.id/
50
 * @author    Sugeng Sulistiyawan <[email protected]>
51
 * @copyright Copyright (c) 2023
52
 */
53
abstract class AbstractComponent extends Component
54
{
55
    /**
56
     * @var Config|array|string|null
57
     */
58
    public $config;
59
60
    /** 
61
     * @var string|null 
62
     */
63
    public $basePath;
64
65
    /**
66
     * @var Filesystem
67
     */
68
    protected $filesystem;
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function __call($method, $parameters)
74
    {
75
        return call_user_func_array([$this->filesystem, $method], $parameters);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function init()
82
    {
83
        $adapter          = $this->initAdapter();
84
        $this->config     = $this->config ?? [];
85
        $this->filesystem = new Filesystem($adapter, $this->config);
86
    }
87
88
    /**
89
     * @return Filesystem
90
     */
91
    public function getFilesystem()
92
    {
93
        return $this->filesystem;
94
    }
95
96
    /**
97
     * @return FilesystemAdapter $adapter
98
     */
99
    abstract protected function initAdapter();
100
101
    /**
102
     * Normalizes a file/directory path.
103
     *
104
     * The normalization does the following work:
105
     *
106
     * - Convert all directory separators into `DIRECTORY_SEPARATOR` (e.g. "\a/b\c" becomes "a/b/c")
107
     * - Remove trailing directory separators (e.g. "/a/b/c/" becomes "a/b/c")
108
     * - Remove first directory separators (e.g. "/a/b/c" becomes "a/b/c")
109
     * - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "a/b/c")
110
     * - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "a/c")
111
     *
112
     * Note: For registered stream wrappers, the consecutive slashes rule
113
     * and ".."/"." translations are skipped.
114
     * 
115
     * @param string $path
116
     * @return string
117
     */
118
    public function normalizePath(string $path)
119
    {
120
        $basePath = $this->basePath ? "{$this->basePath}/" : '';
121
        $path     = FileHelper::normalizePath($basePath . $path, "/");
122
123
        return $path[0] === "/" ? substr($path, 1) : $path;
124
    }
125
}
126