Completed
Push — master ( 58cc6a...0628b8 )
by Cheren
02:56
created

Path::url()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 2
nop 2
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 Cake\Core\Configure;
19
use JBZoo\Utils\FS;
20
use JBZoo\Utils\Arr;
21
use JBZoo\Path\Exception;
22
use JBZoo\Path\Path as JBPAth;
23
24
/**
25
 * Class Path
26
 *
27
 * @package Core\Path
28
 */
29
class Path extends JBPAth
30
{
31
32
    const LS_MODE_DIR = 'dir';
33
    const LS_MODE_FILE = 'file';
34
35
    /**
36
     * Flag of result path (If true, is real path. If false, is relative path).
37
     *
38
     * @var string
39
     */
40
    protected $_isReal = false;
41
42
    /**
43
     * Get a list of directories from a resource.
44
     *
45
     * @param string $resource
46
     * @param bool $recursive
47
     * @param null $filter
48
     * @return mixed
49
     */
50
    public function dirs($resource, $recursive = false, $filter = null)
51
    {
52
        return $this->ls($resource, self::LS_MODE_DIR, $recursive, $filter);
53
    }
54
55
    /**
56
     * Get url to a file.
57
     *
58
     * @param string $source
59
     * @param bool $full
60
     * @return null|string
61
     */
62
    public function url($source, $full = true)
63
    {
64
        $stamp = Configure::read('Asset.timestamp');
65
        $url = parent::url($source, $full);
66
67
        if ($url !== null && $stamp && strpos($url, '?') === false) {
68
            $fullPath = $this->get($source);
69
            $url .= '?' . @filemtime($fullPath);
70
        }
71
72
        return $url;
73
    }
74
75
    /**
76
     * Get path instance.
77
     *
78
     * @param string $key
79
     * @return Path
80
     * @throws Exception
81
     */
82
    public static function getInstance($key = 'default')
83
    {
84
        if ((string) $key = '') {
85
            throw new Exception('Invalid object key');
86
        }
87
88
        if (!Arr::key($key, self::$_objects)) {
89
            self::$_objects[$key] = new self();
90
        }
91
92
        return self::$_objects[$key];
93
    }
94
95
    /**
96
     * Get a list of files or diretories from a resource.
97
     *
98
     * @param string $resource
99
     * @param string $mode
100
     * @param bool $recursive
101
     * @param null $filter
102
     * @return array
103
     * @SuppressWarnings(PHPMD.ShortMethodName)
104
     */
105
    public function ls($resource, $mode = self::LS_MODE_FILE, $recursive = false, $filter = null)
106
    {
107
        $files = [];
108
        list (, $paths, $path) = $this->_parse($resource);
109
110
        foreach ((array) $paths as $_path) {
111
            if (file_exists($_path . '/' . $path)) {
112
                foreach ($this->_list(FS::clean($_path . '/' . $path), '', $mode, $recursive, $filter) as $file) {
113
                    if (!Arr::in($file, $files)) {
114
                        $files[] = $file;
115
                    }
116
                }
117
            }
118
        }
119
120
        return $files;
121
    }
122
123
    /**
124
     * Get the list of files or directories in a given path.
125
     *
126
     * @param string $path
127
     * @param string $prefix
128
     * @param string $mode
129
     * @param bool $recursive
130
     * @param null $filter
131
     * @return array
132
     */
133
    protected function _list($path, $prefix = '', $mode = 'file', $recursive = false, $filter = null)
134
    {
135
        $files  = [];
136
        $ignore = ['.', '..', '.DS_Store', '.svn', '.git', '.gitignore', '.gitmodules', 'cgi-bin'];
137
138
        if ($scan = @scandir($path)) {
139
            foreach ($scan as $file) {
140
                // continue if ignore match
141
                if (Arr::in($file, $ignore)) {
142
                    continue;
143
                }
144
145
                if (is_dir($path . '/' . $file)) {
146
                    // add dir
147 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...
148
                        // continue if no regex filter match
149
                        if ($filter && !preg_match($filter, $file)) {
150
                            continue;
151
                        }
152
153
                        $files[] = $prefix . $file;
154
                    }
155
156
                    // continue if not recursive
157
                    if (!$recursive) {
158
                        continue;
159
                    }
160
161
                    // read subdirectory
162
                    $files = array_merge(
163
                        $files,
164
                        $this->_list($path . '/' . $file, $prefix . $file . '/', $mode, $recursive, $filter)
165
                    );
166
167 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...
168
                    // add file
169
                    if ($mode === 'file') {
170
                        // continue if no regex filter match
171
                        if ($filter && !preg_match($filter, $file)) {
172
                            continue;
173
                        }
174
175
                        $files[] = $prefix.$file;
176
                    }
177
                }
178
            }
179
        }
180
181
        return $files;
182
    }
183
}
184