Completed
Push — master ( 42fd87...58cc6a )
by Cheren
04:33
created

Path::dirs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Path;
17
18
use JBZoo\Utils\FS;
19
use JBZoo\Utils\Arr;
20
use JBZoo\Path\Exception;
21
use JBZoo\Path\Path as JBPAth;
22
23
/**
24
 * Class Path
25
 *
26
 * @package Core\Path
27
 */
28
class Path extends JBPAth
29
{
30
31
    const LS_MODE_DIR = 'dir';
32
    const LS_MODE_FILE = 'file';
33
34
    /**
35
     * Get a list of directories from a resource.
36
     *
37
     * @param string $resource
38
     * @param bool $recursive
39
     * @param null $filter
40
     * @return mixed
41
     */
42
    public function dirs($resource, $recursive = false, $filter = null)
43
    {
44
        return $this->ls($resource, self::LS_MODE_DIR, $recursive, $filter);
45
    }
46
47
    /**
48
     * Get path instance.
49
     *
50
     * @param string $key
51
     * @return Path
52
     * @throws Exception
53
     */
54
    public static function getInstance($key = 'default')
55
    {
56
        if ((string) $key = '') {
57
            throw new Exception('Invalid object key');
58
        }
59
60
        if (!Arr::key($key, self::$_objects)) {
61
            self::$_objects[$key] = new self();
62
        }
63
64
        return self::$_objects[$key];
65
    }
66
67
    /**
68
     * Get a list of files or diretories from a resource.
69
     *
70
     * @param string $resource
71
     * @param string $mode
72
     * @param bool $recursive
73
     * @param null $filter
74
     * @return array
75
     * @SuppressWarnings(PHPMD.ShortMethodName)
76
     */
77
    public function ls($resource, $mode = self::LS_MODE_FILE, $recursive = false, $filter = null)
78
    {
79
        $files = [];
80
        list (, $paths, $path) = $this->_parse($resource);
81
82
        foreach ((array) $paths as $_path) {
83
            if (file_exists($_path . '/' . $path)) {
84
                foreach ($this->_list(FS::clean($_path . '/' . $path), '', $mode, $recursive, $filter) as $file) {
85
                    if (!Arr::in($file, $files)) {
86
                        $files[] = $file;
87
                    }
88
                }
89
            }
90
        }
91
92
        return $files;
93
    }
94
95
    /**
96
     * Get the list of files or directories in a given path.
97
     *
98
     * @param string $path
99
     * @param string $prefix
100
     * @param string $mode
101
     * @param bool $recursive
102
     * @param null $filter
103
     * @return array
104
     */
105
    protected function _list($path, $prefix = '', $mode = 'file', $recursive = false, $filter = null)
106
    {
107
        $files  = [];
108
        $ignore = ['.', '..', '.DS_Store', '.svn', '.git', '.gitignore', '.gitmodules', 'cgi-bin'];
109
110
        if ($scan = @scandir($path)) {
111
            foreach ($scan as $file) {
112
                // continue if ignore match
113
                if (Arr::in($file, $ignore)) {
114
                    continue;
115
                }
116
117
                if (is_dir($path . '/' . $file)) {
118
                    // add dir
119 View Code Duplication
                    if ($mode === 'dir') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
                        // continue if no regex filter match
121
                        if ($filter && !preg_match($filter, $file)) {
122
                            continue;
123
                        }
124
125
                        $files[] = $prefix . $file;
126
                    }
127
128
                    // continue if not recursive
129
                    if (!$recursive) {
130
                        continue;
131
                    }
132
133
                    // read subdirectory
134
                    $files = array_merge(
135
                        $files,
136
                        $this->_list($path . '/' . $file, $prefix . $file . '/', $mode, $recursive, $filter)
137
                    );
138
139 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
                    // add file
141
                    if ($mode === 'file') {
142
                        // continue if no regex filter match
143
                        if ($filter && !preg_match($filter, $file)) {
144
                            continue;
145
                        }
146
147
                        $files[] = $prefix.$file;
148
                    }
149
                }
150
            }
151
        }
152
153
        return $files;
154
    }
155
}
156