1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
<?php
/*
* $Id: Path.php 557 2009-08-29 13:54:38Z mrook $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/types/DataType.php';
include_once 'phing/util/PathTokenizer.php';
include_once 'phing/types/FileSet.php';
/**
* This object represents a path as used by include_path or PATH
* environment variable.
*
* This class has been adopted from the Java Ant equivalent. The ability have
* path structures in Phing is important; however, because of how PHP classes interact
* the ability to specify CLASSPATHs makes less sense than Java.Rather than providing
* CLASSPATH for any tasks that take classes as parameters, perhaps a better
* solution in PHP is to have an IncludePath task, which prepends paths to PHP's include_path
* INI variable. This gets around the problem that simply using a path to load the initial
* PHP class is not enough (in most cases the loaded class may assume that it is on the global
* PHP include_path, and will try to load dependent classes accordingly). The other option is
* to provide a way for this class to add paths to the include path, if desired -- or to create
* an IncludePath subclass. Once added, though, when would a path be removed from the include path?
*
* <p>
* <code>
* <sometask><br>
* <somepath><br>
* <pathelement location="/path/to/file" /><br>
* <pathelement path="/path/to/class2;/path/to/class3" /><br>
* <pathelement location="/path/to/file3" /><br>
* </somepath><br>
* </sometask><br>
* </code>
* <p>
* The object implemention <code>sometask</code> must provide a method called
* <code>createSomepath</code> which returns an instance of <code>Path</code>.
* Nested path definitions are handled by the Path object and must be labeled
* <code>pathelement</code>.<p>
*
* The path element takes a parameter <code>path</code> which will be parsed
* and split into single elements. It will usually be used
* to define a path from an environment variable.
*
* @author Hans Lellelid <hans@xmpl.org> (Phing)
* @author Thomas.Haas@softwired-inc.com (Ant)
* @author Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
* @package phing.types
*/
class Path extends DataType {
private $elements = array();
/**
* Constructor for internally instantiated objects sets project.
* @param Project $project
* @param string $path (for use by IntrospectionHelper)
*/
public function __construct($project = null, $path = null) {
if ($project !== null) {
$this->setProject($project);
}
if ($path !== null) {
$this->createPathElement()->setPath($path);
}
}
/**
* Adds a element definition to the path.
* @param $location the location of the element to add (must not be
* <code>null</code> nor empty.
* @throws BuildException
*/
public function setDir(PhingFile $location) {
if ($this->isReference()) {
throw $this->tooManyAttributes();
}
$this->createPathElement()->setDir($location);
}
/**
* Parses a path definition and creates single PathElements.
* @param path the path definition.
* @throws BuildException
*/
public function setPath($path) {
if ($this->isReference()) {
throw $this->tooManyAttributes();
}
$this->createPathElement()->setPath($path);
}
/**
* Makes this instance in effect a reference to another Path instance.
*
* <p>You must not set another attribute or nest elements inside
* this element if you make it a reference.</p>
* @throws BuildException
*/
public function setRefid(Reference $r) {
if (!empty($this->elements)) {
throw $this->tooManyAttributes();
}
$this->elements[] = $r;
parent::setRefid($r);
}
/**
* Creates the nested <code><pathelement></code> element.
* @throws BuildException
*/
public function createPathElement() {
if ($this->isReference()) {
throw $this->noChildrenAllowed();
}
$pe = new PathElement($this);
$this->elements[] = $pe;
return $pe;
}
/**
* Adds a nested <code><fileset></code> element.
* @throws BuildException
*/
public function addFileset(FileSet $fs) {
if ($this->isReference()) {
throw $this->noChildrenAllowed();
}
$this->elements[] = $fs;
$this->checked = false;
}
/**
* Adds a nested <code><dirset></code> element.
* @throws BuildException
*/
public function addDirset(DirSet $dset) {
if ($this->isReference()) {
throw $this->noChildrenAllowed();
}
$this->elements[] = $dset;
$this->checked = false;
}
/**
* Creates a nested <code><path></code> element.
* @throws BuildException
*/
public function createPath() {
if ($this->isReference()) {
throw $this->noChildrenAllowed();
}
$p = new Path($this->project);
$this->elements[] = $p;
$this->checked = false;
return $p;
}
/**
* Append the contents of the other Path instance to this.
*/
public function append(Path $other) {
if ($other === null) {
return;
}
$l = $other->listPaths();
foreach($l as $path) {
if (!in_array($path, $this->elements, true)) {
$this->elements[] = $path;
}
}
}
/**
* Adds the components on the given path which exist to this
* Path. Components that don't exist, aren't added.
*
* @param Path $source - Source path whose components are examined for existence.
*/
public function addExisting(Path $source) {
$list = $source->listPaths();
foreach($list as $el) {
$f = null;
if ($this->project !== null) {
$f = $this->project->resolveFile($el);
} else {
$f = new PhingFile($el);
}
if ($f->exists()) {
$this->setDir($f);
} else {
$this->log("dropping " . $f->__toString() . " from path as it doesn't exist",
Project::MSG_VERBOSE);
}
}
}
/**
* Returns all path elements defined by this and nested path objects.
* @return array List of path elements.
*/
public function listPaths() {
if (!$this->checked) {
// make sure we don't have a circular reference here
$stk = array();
array_push($stk, $this);
$this->dieOnCircularReference($stk, $this->project);
}
$result = array();
for ($i = 0, $elSize=count($this->elements); $i < $elSize; $i++) {
$o = $this->elements[$i];
if ($o instanceof Reference) {
$o = $o->getReferencedObject($this->project);
// we only support references to paths right now
if (!($o instanceof Path)) {
$msg = $r->getRefId() . " doesn't denote a path";
throw new BuildException($msg);
}
}
if (is_string($o)) {
$result[] = $o;
} elseif ($o instanceof PathElement) {
$parts = $o->getParts();
if ($parts === null) {
throw new BuildException("You must either set location or"
. " path on <pathelement>");
}
foreach($parts as $part) {
$result[] = $part;
}
} elseif ($o instanceof Path) {
$p = $o;
if ($p->getProject() === null) {
$p->setProject($this->getProject());
}
$parts = $p->listPaths();
foreach($parts as $part) {
$result[] = $part;
}
} elseif ($o instanceof DirSet) {
$dset = $o;
$ds = $dset->getDirectoryScanner($this->project);
$dirstrs = $ds->getIncludedDirectories();
$dir = $dset->getDir($this->project);
foreach($dirstrs as $dstr) {
$d = new PhingFile($dir, $dstr);
$result[] = $d->getAbsolutePath();
}
} elseif ($o instanceof FileList) {
$fl = $o;
$dirstrs = $fl->getFiles($this->project);
$dir = $fl->getDir($this->project);
foreach($dirstrs as $dstr) {
$d = new PhingFile($dir, $dstr);
$result[] = $d->getAbsolutePath();
}
}
}
return array_unique($result);
}
/**
* Returns a textual representation of the path, which can be used as
* CLASSPATH or PATH environment variable definition.
* @return string A textual representation of the path.
*/
public function __toString() {
$list = $this->listPaths();
// empty path return empty string
if (empty($list)) {
return "";
}
return implode(PATH_SEPARATOR, $list);
}
/**
* Splits a PATH (with : or ; as separators) into its parts.
* @param Project $project
* @param string $source
*/
public static function translatePath(Project $project, $source) {
$result = array();
if ($source == null) {
return "";
}
$tok = new PathTokenizer($source);
while ($tok->hasMoreTokens()) {
$pathElement = $tok->nextToken();
try {
$element = self::resolveFile($project, $pathElement);
for ($i = 0, $_i=strlen($element); $i < $_i; $i++) {
self::translateFileSep($element, $i);
}
$result[] = $element;
} catch (BuildException $e) {
$this->project->log("Dropping path element " . $pathElement
. " as it is not valid relative to the project",
Project::MSG_VERBOSE);
}
}
return $result;
}
/**
* Returns its argument with all file separator characters
* replaced so that they match the local OS conventions.
*/
public static function translateFile($source) {
if ($source == null) {
return "";
}
$result = $source;
for ($i = 0, $_i=strlen($source); $i < $_i; $i++) {
self::translateFileSep($result, $i);
}
return $result;
}
/**
* Translates all occurrences of / or \ to correct separator of the
* current platform and returns whether it had to do any
* replacements.
*/
protected static function translateFileSep(&$buffer, $pos) {
if ($buffer{$pos} == '/' || $buffer{$pos} == '\\') {
$buffer{$pos} = DIRECTORY_SEPARATOR;
return true;
}
return false;
}
/**
* How many parts does this Path instance consist of.
* DEV NOTE: expensive call! list is generated, counted, and then
* discareded.
* @return int
*/
public function size() {
return count($this->listPaths());
}
/**
* Return a Path that holds the same elements as this instance.
*/
public function __clone() {
$p = new Path($this->project);
$p->append($this);
return $p;
}
/**
* Overrides the version of DataType to recurse on all DataType
* child elements that may have been added.
* @throws BuildException
*/
public function dieOnCircularReference(&$stk, Project $p) {
if ($this->checked) {
return;
}
// elements can contain strings, FileSets, Reference, etc.
foreach($this->elements as $o) {
if ($o instanceof Reference) {
$o = $o->getReferencedObject($p);
}
if ($o instanceof DataType) {
if (in_array($o, $stk, true)) {
throw $this->circularReference();
} else {
array_push($stk, $o);
$o->dieOnCircularReference($stk, $p);
array_pop($stk);
}
}
}
$this->checked = true;
}
/**
* Resolve a filename with Project's help - if we know one that is.
*
* <p>Assume the filename is absolute if project is null.</p>
*/
private static function resolveFile(Project $project, $relativeName) {
if ($project !== null) {
$f = $project->resolveFile($relativeName);
return $f->getAbsolutePath();
}
return $relativeName;
}
}
/**
* Helper class, holds the nested <code><pathelement></code> values.
*
* @package phing.types
*/
class PathElement {
private $parts = array();
private $outer;
public function __construct(Path $outer) {
$this->outer = $outer;
}
public function setDir(PhingFile $loc) {
$this->parts = array(Path::translateFile($loc->getAbsolutePath()));
}
public function setPath($path) {
$this->parts = Path::translatePath($this->outer->getProject(), $path);
}
public function getParts() {
return $this->parts;
}
}
|