Changeset 362
- Timestamp:
- 03/12/07 07:22:39 (2 years ago)
- Files:
-
- trunk/build/mvn (added)
- trunk/build/mvn/build.xml (added)
- trunk/src/main/php/org/apache (added)
- trunk/src/main/php/org/apache/maven (added)
- trunk/src/main/php/org/apache/maven/artifact (added)
- trunk/src/main/php/org/apache/maven/artifact/versioning (added)
- trunk/src/main/php/org/apache/maven/artifact/versioning/mvnInvalidVersionSpecificationException.php (added)
- trunk/src/main/php/org/apache/maven/artifact/versioning/mvnOverConstrainedVersionException.php (added)
- trunk/src/main/php/org/apache/maven/artifact/versioning/mvnRestriction.php (added)
- trunk/src/main/php/org/apache/maven/artifact/versioning/mvnVersionRange.php (moved) (moved from trunk/src/main/php/info/phing/tasks/MavenVersionRange.php) (25 diffs)
- trunk/src/test/php/org (added)
- trunk/src/test/php/org/apache (added)
- trunk/src/test/php/org/apache/maven (added)
- trunk/src/test/php/org/apache/maven/artifact (added)
- trunk/src/test/php/org/apache/maven/artifact/versioning (added)
- trunk/src/test/php/org/apache/maven/artifact/versioning/mvnVersionRangeTestCase.php (added)
- trunk/src/test/php/org/apache/maven/mvnTestSuite.php (added)
- trunk/src/test/php/org/apache/maven/run.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/org/apache/maven/artifact/versioning/mvnVersionRange.php
r350 r362 1 1 <?php 2 class MavenVersionRange 2 /* 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 */ 20 require_once dirname(__FILE__) . '/mvnInvalidVersionSpecificationException.php'; 21 require_once dirname(__FILE__) . '/mvnOverConstrainedVersionException.php'; 22 require_once dirname(__FILE__) . '/mvnRestriction.php'; 23 /** 24 * Constructs a version range from a specification. 25 * 26 * @author Brett Porter <brett@apache.org> 27 * @author Frank Kleine <mikey@stubbles.net> 28 * @package apache_maven 29 * @subpackage artifact_versioning 30 */ 31 /** 32 * Constructs a version range from a specification. 33 * 34 * @package apache_maven 35 * @subpackage artifact_versioning 36 */ 37 class mvnVersionRange 3 38 { 4 39 /** 5 40 * the recommended version 6 41 * 7 * @var MavenArtifactVersion8 */ 9 protected $recommendedVersion ;42 * @var string 43 */ 44 protected $recommendedVersion = null; 10 45 /** 11 46 * the list of restrictions 12 47 * 13 * @var array 14 */ 15 protected $restrictions ;48 * @var array<mvnRestriction> 49 */ 50 protected $restrictions = array(); 16 51 17 52 /** 18 53 * constructor 19 54 * 20 * @param MavenArtifactVersion$recommendedVersion optional the recommended version21 * @param array $restrictions optional the list of restrictions22 */ 23 private function __construct( MavenArtifactVersion$recommendedVersion = null, array $restrictions = array())55 * @param string $recommendedVersion optional the recommended version 56 * @param array<mvnRestriction> $restrictions optional the list of restrictions 57 */ 58 private function __construct($recommendedVersion = null, array $restrictions = array()) 24 59 { 25 60 $this->recommendedVersion = $recommendedVersion; … … 30 65 * returns the recommended version 31 66 * 32 * @return MavenArtifactVersion67 * @return string 33 68 */ 34 69 public function getRecommendedVersion() … … 40 75 * returns the list of restrictions 41 76 * 42 * @return array 77 * @return array<mvnRestriction> 43 78 */ 44 79 public function getRestrictions() … … 48 83 49 84 /** 50 * Createa version range from a string representation85 * creates a version range from a string representation 51 86 * 52 87 * Some spec examples are … … 56 91 * <li><code>[1.0,2.0]</code> Versions 1.0 to 2.0 (both included)</li> 57 92 * <li><code>[1.5,)</code> Versions 1.5 and higher</li> 93 * <li><code>[1.5],[2.3]</code> Versions 1.5 and 2.3</li> 58 94 * <li><code>(,1.0],[1.2,)</code> Versions up to 1.0 (included) and 1.2 or higher</li> 59 95 * </ul> 60 96 * 61 * @param string $spec representation of a version or version range62 * @return MavenVersionRange63 * @throws InvalidVersionSpecificationException97 * @param string $spec representation of a version or version range 98 * @return mvnVersionRange 99 * @throws mvnInvalidVersionSpecificationException 64 100 */ 65 101 public static function createFromVersionSpec($spec) … … 77 113 while (substr($process, 0, 1) == '[' || substr($process, 0, 1) == '(') { 78 114 $index1 = strpos($process, ')'); 115 if (false === $index1) { 116 $index1 = -1; 117 } 118 79 119 $index2 = strpos($process, ']'); 120 if (false === $index2) { 121 $index2 = -1; 122 } 80 123 81 124 $index = $index2; … … 87 130 88 131 if (0 > $index) { 89 throw new InvalidVersionSpecificationException('Unbounded range: ' . $spec);132 throw new mvnInvalidVersionSpecificationException('Unbounded range: ' . $spec); 90 133 } 91 134 … … 97 140 if (null != $upperBound) { 98 141 $lowerBound = $restriction->getLowerBound(); 99 if (null == $lowerBound || $lowerBound->compareTo($upperBound) < 0) {100 throw new InvalidVersionSpecificationException('Ranges overlap: ' . $spec);142 if (null == $lowerBound || version_compare($lowerBound, $upperBound) < 0) { 143 throw new mvnInvalidVersionSpecificationException('Ranges overlap: ' . $spec); 101 144 } 102 145 } 103 146 104 147 $restrictions[] = $restriction; 105 $upperBound = $restriction->getUpperBound();148 $upperBound = $restriction->getUpperBound(); 106 149 107 150 $process = trim(substr($process, ($index + 1))); … … 114 157 if (strlen($process) > 0) { 115 158 if (count($restrictions) > 0) { 116 throw new InvalidVersionSpecificationException('Only fully-qualified sets allowed in multiple set scenario: ' . $spec);159 throw new mvnInvalidVersionSpecificationException('Only fully-qualified sets allowed in multiple set scenario: ' . $spec); 117 160 } else { 118 $version = new DefaultArtifactVersion($process); 119 $restrictions[] = Restriction.EVERYTHING; 161 $version = explode(',', $process); 162 $version = str_replace(')', '', str_replace(']', '', str_replace('(', '', str_replace('[', '', $version[0])))); 163 $restrictions[] = mvnRestriction::everything(); 120 164 } 121 165 } … … 127 171 * creates a version range from one version 128 172 * 129 * @param string $version130 * @return MavenVersionRange173 * @param string $version 174 * @return mvnVersionRange 131 175 */ 132 176 public static function createFromVersion($version) 133 177 { 134 return new self( new DefaultArtifactVersion($version));178 return new self($version); 135 179 } 136 180 … … 139 183 * 140 184 * @param string $spec 141 * @return Restriction142 * @throws throwsInvalidVersionSpecificationException185 * @return mvnRestriction 186 * @throws mvnInvalidVersionSpecificationException 143 187 */ 144 188 private static function parseRestriction($spec) … … 146 190 $lowerBoundInclusive = (substr($spec, 0, 1) == '['); 147 191 $upperBoundInclusive = (substr($spec, -1, 1) == ']'); 148 149 $process = trim(substr($spec, 1, (strlen($spec) - 1))); 150 151 $index = strpos($process, ','); 192 $process = trim(substr($spec, 1, (strlen($spec) - 2))); 193 $index = strpos($process, ','); 194 if (false === $index) { 195 $index = -1; 196 } 152 197 153 198 if (0 > $index) { 154 199 if (false == $lowerBoundInclusive || false == $upperBoundInclusive) { 155 throw new InvalidVersionSpecificationException('Single version must be surrounded by []: ' . $spec); 156 } 157 158 $version = new DefaultArtifactVersion($process); 159 $restriction = new Restriction($version, $lowerBoundInclusive, $version, $upperBoundInclusive); 160 } else { 161 $lowerBound = trim(substr($process, 0, $index)); 162 $upperBound = trim(substr($process, ($index + 1))); 163 if ($lowerBound == $upperBound) { 164 throw new InvalidVersionSpecificationException('Range cannot have identical boundaries: ' . $spec); 165 } 166 167 $lowerVersion = null; 168 if (strlen($lowerBound) > 0) { 169 $lowerVersion = new DefaultArtifactVersion($lowerBound); 170 } 171 172 $upperVersion = null; 173 if (strlen($upperBound) > 0) { 174 $upperVersion = new DefaultArtifactVersion($upperBound); 175 } 176 177 if (null != $upperVersion && null != $lowerVersion && $upperVersion->compareTo($lowerVersion) < 0) { 178 throw new InvalidVersionSpecificationException('Range defies version ordering: ' . $spec); 179 } 180 181 $restriction = new Restriction($lowerVersion, $lowerBoundInclusive, $upperVersion, $upperBoundInclusive); 182 } 183 184 return $restriction; 200 throw new mvnInvalidVersionSpecificationException('Single version must be surrounded by []: ' . $spec); 201 } 202 203 return new mvnRestriction($process, $lowerBoundInclusive, $process, $upperBoundInclusive); 204 } 205 206 $lowerBound = trim(substr($process, 0, $index)); 207 $upperBound = trim(substr($process, ($index + 1))); 208 if ($lowerBound == $upperBound) { 209 throw new mvnInvalidVersionSpecificationException('Range can not have identical boundaries: ' . $spec); 210 } 211 212 $lowerVersion = null; 213 if (strlen($lowerBound) > 0) { 214 $lowerVersion = $lowerBound; 215 } 216 217 $upperVersion = null; 218 if (strlen($upperBound) > 0) { 219 $upperVersion = $upperBound; 220 } 221 222 if (null != $upperVersion && null != $lowerVersion && version_compare($upperVersion, $lowerVersion) < 0) { 223 throw new mvnInvalidVersionSpecificationException('Range defies version ordering: ' . $spec); 224 } 225 226 return new mvnRestriction($lowerVersion, $lowerBoundInclusive, $upperVersion, $upperBoundInclusive); 185 227 } 186 228 … … 188 230 * restricts the version range to the given range 189 231 * 190 * @param MavenVersionRange $restriction191 * @return MavenVersionRange232 * @param mvnVersionRange $restrictTo 233 * @return mvnVersionRange 192 234 */ 193 235 public function restrict(self $restrictTo) … … 201 243 202 244 if (count($restrictions) == 0 && null == $this->recommendedVersion) { 203 //should throw this immediately, but need artifact 204 throw new OverConstrainedVersionException('Restricting incompatible version ranges'); 245 throw new mvnOverConstrainedVersionException('Restricting incompatible version ranges'); 205 246 } 206 247 207 248 $version = null; 208 249 if (count($restrictions) > 0) { 209 $found = false;210 250 foreach ($restrictions as $restriction) { 211 251 if (null != $this->recommendedVersion && $restriction->containsVersion($this->recommendedVersion) == true) { 212 252 // if we find the original, use that 213 253 $version = $this->recommendedVersion ; 214 $found = true;254 break; 215 255 } elseif (null == $version && $restrictTo->getRecommendedVersion() != null && 216 256 $restriction->containsVersion($restrictTo->getRecommendedVersion()) == true) { … … 232 272 * @param string $artifactId 233 273 * @return MavenArtifactVersion 234 * @throws OverConstrainedVersionException274 * @throws mvnOverConstrainedVersionException 235 275 */ 236 276 public function getSelectedVersion($artifactId) … … 241 281 242 282 if (count($this->restrictions) == 0) { 243 throw new OverConstrainedVersionException('The artifact has no valid ranges', $artifactId);283 throw new mvnOverConstrainedVersionException('The artifact ' . $artifactId . ' has no valid ranges'); 244 284 } 245 285 … … 247 287 $version = $restriction->getUpperBound(); 248 288 if (null == $version) { 249 $version = RELEASE;289 $version = 'RELEASE'; 250 290 } 251 291 … … 258 298 * @param string $artifactId 259 299 * @return bool 260 * @throws OverConstrainedVersionException300 * @throws mvnOverConstrainedVersionException 261 301 */ 262 302 public function isSelectedVersionKnown($artifactId) … … 267 307 268 308 if (count($this->restrictions) == 0) { 269 throw new OverConstrainedVersionException('The artifact has no valid ranges', $artifactId);309 throw new mvnOverConstrainedVersionException('The artifact ' . $artifactId . 'has no valid ranges'); 270 310 } 271 311 … … 286 326 { 287 327 if (null != $this->recommendedVersion) { 288 return $this->recommendedVersion ->__toString();328 return $this->recommendedVersion; 289 329 } 290 330 … … 296 336 $buf .= $restriction->isLowerBoundInclusive() ? '[' : '('; 297 337 if ($restriction->getLowerBound() != null ) { 298 $buf .= $restriction->getLowerBound() ->__toString();338 $buf .= $restriction->getLowerBound(); 299 339 } 300 340 301 341 $buf .= ','; 302 342 if ($restriction->getUpperBound() != null ) { 303 $buf .= $restriction->getUpperBound() ->__toString();343 $buf .= $restriction->getUpperBound(); 304 344 } 305 345 … … 313 353 * returns the version that matches 314 354 * 315 * @param array 316 * @return ArtifactVersion355 * @param array<string> 356 * @return string 317 357 */ 318 358 public function matchVersion(array $versions) … … 322 362 if ($this->containsVersion($version) == true) { 323 363 // valid - check if it is greater than the currently matched version 324 if (null == $matched || $version->compareTo($matched) > 0) {364 if (null == $matched || version_compare($matched, $version) > 0) { 325 365 $matched = $version; 326 366 } … … 334 374 * check whether the range contains the given version 335 375 * 336 * @param ArtifactVersion$version376 * @param string $version 337 377 * @return bool 338 378 */ 339 public function containsVersion( ArtifactVersion$version)379 public function containsVersion($version) 340 380 { 341 381 foreach ($this->restrictions as $restriction) { … … 355 395 public function hasRestrictions() 356 396 { 357 return count($this->restrictions) > 0 && null == $this->recommendedVersion;397 return (count($this->restrictions) > 0 && null === $this->recommendedVersion); 358 398 } 359 399 }
