|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the SVN-Buddy library. |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
|
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Repository\RevisionLog; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class PathCollisionDetector |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Known paths. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $_knownPaths = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Expanded array of paths. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $_expandedPaths = array('/' => 1); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Longest path length. |
|
33
|
|
|
* |
|
34
|
|
|
* @var integer |
|
35
|
|
|
*/ |
|
36
|
|
|
private $_longestPathLength = 1; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Adds paths. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $paths Paths. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
7 |
|
public function addPaths(array $paths) |
|
46
|
|
|
{ |
|
47
|
7 |
|
foreach ( $paths as $path ) { |
|
48
|
7 |
|
$parent_path = rtrim($path, '/'); |
|
49
|
7 |
|
$this->_knownPaths[$path] = true; |
|
50
|
|
|
|
|
51
|
|
|
do { |
|
52
|
7 |
|
$this->_expandedPaths[$parent_path . '/'] = strlen($parent_path . '/'); |
|
53
|
7 |
|
$parent_path = dirname($parent_path); |
|
54
|
7 |
|
} while ( $parent_path !== '/' && $parent_path !== '' ); |
|
55
|
7 |
|
} |
|
56
|
|
|
|
|
57
|
7 |
|
$path_count = count($this->_expandedPaths); |
|
58
|
|
|
|
|
59
|
7 |
|
if ( $path_count > 1 ) { |
|
60
|
5 |
|
$this->_longestPathLength = call_user_func_array('max', $this->_expandedPaths); |
|
61
|
5 |
|
} |
|
62
|
2 |
|
elseif ( $path_count === 1 ) { |
|
63
|
2 |
|
$this->_longestPathLength = current($this->_expandedPaths); |
|
64
|
2 |
|
} |
|
65
|
|
|
else { |
|
66
|
|
|
$this->_longestPathLength = 0; // @codeCoverageIgnore |
|
67
|
|
|
} |
|
68
|
7 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks path collision. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $path Path. |
|
74
|
|
|
* |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
9 |
|
public function isCollision($path) |
|
78
|
|
|
{ |
|
79
|
9 |
|
if ( isset($this->_knownPaths[$path]) || !$this->_knownPaths ) { |
|
80
|
4 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
$path = substr($path, 0, $this->_longestPathLength); |
|
84
|
|
|
|
|
85
|
5 |
|
return isset($this->_expandedPaths[$path]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|