|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2017 by TEQneers GmbH & Co. KG |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
* in the Software without restriction, including without limitation the rights |
|
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
* furnished to do so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
13
|
|
|
* all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
21
|
|
|
* THE SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Git Stream Wrapper for PHP |
|
26
|
|
|
* |
|
27
|
|
|
* @category TQ |
|
28
|
|
|
* @package TQ_VCS |
|
29
|
|
|
* @subpackage VCS |
|
30
|
|
|
* @copyright Copyright (C) 2018 by TEQneers GmbH & Co. KG |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
namespace TQ\Vcs\StreamWrapper; |
|
34
|
|
|
use TQ\Vcs\Repository\RepositoryInterface; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates path information for a given stream URL |
|
38
|
|
|
* |
|
39
|
|
|
* @author Stefan Gehrig <gehrigteqneers.de> |
|
40
|
|
|
* @category TQ |
|
41
|
|
|
* @package TQ_VCS |
|
42
|
|
|
* @subpackage VCS |
|
43
|
|
|
* @copyright Copyright (C) 2018 by TEQneers GmbH & Co. KG |
|
44
|
|
|
*/ |
|
45
|
|
|
abstract class AbstractPathFactory implements PathFactoryInterface |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* The repository registry |
|
49
|
|
|
* |
|
50
|
|
|
* @var RepositoryRegistry |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $map; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The registered protocol |
|
56
|
|
|
* |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $protocol; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Creates a path factory |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $protocol The protocol (such as "vcs") |
|
65
|
|
|
* @param RepositoryRegistry $map The repository registry |
|
66
|
|
|
*/ |
|
67
|
121 |
|
public function __construct($protocol, RepositoryRegistry $map = null) |
|
68
|
|
|
{ |
|
69
|
121 |
|
$this->protocol = $protocol; |
|
70
|
121 |
|
$this->map = $map ?: new RepositoryRegistry(); |
|
71
|
121 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the repository registry |
|
75
|
|
|
* |
|
76
|
|
|
* @return RepositoryRegistry |
|
77
|
|
|
*/ |
|
78
|
12 |
|
public function getRegistry() |
|
79
|
|
|
{ |
|
80
|
12 |
|
return $this->map; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the repository for the given path information |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $pathInfo An array containing information about the path |
|
87
|
|
|
* @return RepositoryInterface |
|
88
|
|
|
*/ |
|
89
|
114 |
|
protected function getRepository(array $pathInfo) |
|
90
|
|
|
{ |
|
91
|
114 |
|
if ($pathInfo['host'] === PathInformationInterface::GLOBAL_PATH_HOST) { |
|
92
|
106 |
|
return $this->createRepositoryForPath($pathInfo['path']); |
|
93
|
|
|
} else { |
|
94
|
8 |
|
return $this->map->getRepository($pathInfo['host']); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Creates a new Repository instance for the given path |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $path The path |
|
102
|
|
|
* @return RepositoryInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
abstract protected function createRepositoryForPath($path); |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the path information for a given stream URL |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $streamUrl The URL given to the stream function |
|
110
|
|
|
* @return PathInformation The path information representing the stream URL |
|
111
|
|
|
*/ |
|
112
|
114 |
|
public function createPathInformation($streamUrl) |
|
113
|
|
|
{ |
|
114
|
114 |
|
$pathInfo = $this->parsePath($streamUrl); |
|
115
|
114 |
|
$repository = $this->getRepository($pathInfo); |
|
116
|
114 |
|
$ref = isset($pathInfo['fragment']) ? $pathInfo['fragment'] : 'HEAD'; |
|
117
|
|
|
|
|
118
|
114 |
|
$arguments = array(); |
|
119
|
114 |
|
if (isset($pathInfo['query'])) { |
|
120
|
4 |
|
parse_str($pathInfo['query'], $arguments); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
114 |
|
$fullPath = $repository->resolveFullPath($pathInfo['path']); |
|
124
|
114 |
|
$url = $this->protocol.'://'.$fullPath |
|
125
|
114 |
|
.'#'.$ref |
|
126
|
114 |
|
.'?'.http_build_query($arguments); |
|
127
|
|
|
|
|
128
|
114 |
|
return new PathInformation($repository, $url, $fullPath, $ref, $arguments); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns path information for a given stream path |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $streamUrl The URL given to the stream function |
|
135
|
|
|
* @return array An array containing information about the path |
|
136
|
|
|
* @throws \InvalidArgumentException If the URL is invalid |
|
137
|
|
|
*/ |
|
138
|
119 |
|
public function parsePath($streamUrl) |
|
139
|
|
|
{ |
|
140
|
|
|
// normalize directory separators |
|
141
|
119 |
|
$path = str_replace(array('\\', '/'), '/', $streamUrl); |
|
142
|
|
|
//fix path if fragment has been munged into the path (e.g. when using the RecursiveIterator) |
|
143
|
119 |
|
$path = preg_replace('~^(.+?)(#[^/]+)(.*)$~', '$1$3$2', $path); |
|
144
|
|
|
|
|
145
|
|
|
/// fix /// paths to __global__ "host" |
|
146
|
119 |
|
$protocol = $this->protocol; |
|
147
|
119 |
|
if (strpos($path, $protocol.':///') === 0) { |
|
148
|
110 |
|
$path = str_replace($protocol.':///', $protocol.'://'.PathInformationInterface::GLOBAL_PATH_HOST.'/', $path); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
119 |
|
$info = parse_url($path); |
|
152
|
119 |
|
if ($info === false) { |
|
153
|
|
|
throw new \InvalidArgumentException('Url "'.$streamUrl.'" is not a valid path'); |
|
154
|
|
|
} |
|
155
|
119 |
|
if (isset($info['path']) && preg_match('~^/\w:.+~', $info['path'])) { |
|
156
|
1 |
|
$info['path'] = ltrim($info['path'], '/'); |
|
157
|
118 |
|
} else if (!isset( $info['path'])) { |
|
158
|
4 |
|
$info['path'] = '/'; |
|
159
|
|
|
} |
|
160
|
119 |
|
return $info; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.