Completed
Push — master ( 159bd4...9ef354 )
by Hong
02:36
created

PathAwareTrait::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Storage
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Storage\Traits;
16
17
use Phossa2\Storage\Path;
18
use Phossa2\Storage\Interfaces\PathAwareInterface;
19
20
/**
21
 * PathAwareTrait
22
 *
23
 * Implementation of PathAwareInterface
24
 *
25
 * @package Phossa2\Storage
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     PathAwareInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait PathAwareTrait
32
{
33
    use MountableTrait;
34
35
    /**
36
     * cache of Path objects
37
     *
38
     * @var    Path[]
39
     * @access protected
40
     */
41
    protected $path_cache = [];
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function path(/*# string */ $path)/*# : Path */
47
    {
48
        // unique key
49
        $key = $this->getCacheKey($path);
50
51
        // try cache
52
        if ($this->hasPathCache($key)) {
53
            return $this->getFromCache($key);
54
        }
55
56
        // new Path object
57
        $obj = $this->newPath($this->normalize($path));
58
59
        // save to cache
60
        $this->saveToCache($key, $obj);
61
62
        return $obj;
63
    }
64
65
    /**
66
     * Normalize the path
67
     *
68
     * Replaces '.', '..', prepends '/', keeps trailing '/'
69
     *
70
     * @param  string $path
71
     * @return string
72
     * @access protected
73
     */
74
    protected function normalize(/*# string */ $path)/*# : string */
75
    {
76
        $pattern = ['~/{2,}~', '~/(\./)+~', '~([^/\.]+/(?R)*\.{2,}/)~', '~\.\./~'];
77
        $replace = ['/', '/', '', ''];
78
        return preg_replace($pattern, $replace, '/' . ltrim($path, '/'));
79
    }
80
81
    /**
82
     * Generate Path object. Override this method if you want to
83
     *
84
     * @param  string $path
85
     * @return Path
86
     * @access protected
87
     */
88
    protected function newPath(/*# string */ $path)/*# : Path */
89
    {
90
        list($mnt, $remain) = $this->splitPath($path);
91
        return new Path($path, $remain, $this->getFilesystemAt($mnt));
92
    }
93
94
    /**
95
     * Split into mount point and the remain
96
     *
97
     * @param  string $path
98
     * @return array [ mountpoint, remain ]
99
     * @access protected
100
     */
101
    protected function splitPath(/*# string */ $path)/*# : array */
102
    {
103
        // mount point
104
        $pref = $this->getMountPoint($path);
105
106
        // remains without leading '/'
107
        $remain = ltrim(substr($path, strlen($pref)), '/');
108
109
        return [$pref, $remain];
110
    }
111
112
    /**
113
     * Merge path
114
     *
115
     * @param  string $prefix
116
     * @param  string $suffix
117
     * @return string
118
     * @access protected
119
     */
120
    protected function mergePath(
121
        /*# string */ $prefix,
122
        /*# string */ $suffix
123
    )/*# : string */ {
124
        return rtrim($prefix, '/') . '/' . ltrim($suffix, '/');
125
    }
126
127
    /**
128
     * Is this path in cache ?
129
     *
130
     * @param  string $key
131
     * @return bool
132
     * @access protected
133
     */
134
    protected function hasPathCache(/*# string */ $key)/*# : bool */
135
    {
136
        return isset($this->path_cache[$key[0]][$key]);
137
    }
138
139
    /**
140
     * Get Path object from local cache
141
     *
142
     * @param  string $key
143
     * @return Path
144
     * @access protected
145
     */
146
    protected function getFromCache(/*# string */ $key)
147
    {
148
        return $this->path_cache[$key[0]][$key];
149
    }
150
151
    /**
152
     * Save Path object to local cache
153
     *
154
     * @param  string $key
155
     * @param  object $obj
156
     * @access protected
157
     */
158
    protected function saveToCache(/*# string */ $key, $obj)
159
    {
160
        // clear stale cache
161
        if (isset($this->path_cache[$key[0]]) &&
162
            sizeof($this->path_cache[$key[0]]) > 1
163
        ) {
164
            $this->path_cache[$key[0]] = [];
165
        }
166
167
        $this->path_cache[$key[0]][$key] = $obj;
168
    }
169
170
    /**
171
     * Generate an unique key for the path
172
     *
173
     * @param  string $path
174
     * @return string
175
     * @access protected
176
     */
177
    protected function getCacheKey(/*# string */ $path)/*# : string */
178
    {
179
        return md5($path);
180
    }
181
}
182