IncludePathTask.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * $Id: IncludePathTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. include_once 'phing/types/Path.php';
  23. /**
  24. * Adds a normalized path to the PHP include_path.
  25. *
  26. * This provides a way to alter the include_path without editing any global php.ini settings
  27. * or PHP_CLASSPATH environment variable.
  28. *
  29. * <code>
  30. * <includepath classpath="new/path/here"/>
  31. * </code>
  32. *
  33. * @author Hans Lellelid <hans@xmpl.org>
  34. * @version $Revision: 905 $
  35. * @package phing.tasks.system
  36. */
  37. class IncludePathTask extends Task {
  38. /**
  39. * Classname of task to register.
  40. * This can be a dot-path -- relative to a location on PHP include_path.
  41. * E.g. path.to.MyClass -> path/to/MyClass.php
  42. * @var string
  43. */
  44. private $classname;
  45. /**
  46. * Path to add to PHP include_path to aid in finding specified class.
  47. * @var Path
  48. */
  49. private $classpath;
  50. /**
  51. * Refid to already defined classpath
  52. */
  53. private $classpathId;
  54. /**
  55. * Set the classpath to be used when searching for component being defined
  56. *
  57. * @param Path $classpath An Path object containing the classpath.
  58. */
  59. public function setClasspath(Path $classpath) {
  60. if ($this->classpath === null) {
  61. $this->classpath = $classpath;
  62. } else {
  63. $this->classpath->append($classpath);
  64. }
  65. }
  66. /**
  67. * Create the classpath to be used when searching for component being defined
  68. */
  69. public function createClasspath() {
  70. if ($this->classpath === null) {
  71. $this->classpath = new Path($this->project);
  72. }
  73. return $this->classpath->createPath();
  74. }
  75. /**
  76. * Reference to a classpath to use when loading the files.
  77. */
  78. public function setClasspathRef(Reference $r) {
  79. $this->classpathId = $r->getRefId();
  80. $this->createClasspath()->setRefid($r);
  81. }
  82. /** Main entry point */
  83. public function main() {
  84. // Apparently casting to (string) no longer invokes __toString() automatically.
  85. if (is_object($this->classpath)) {
  86. $this->classpath = $this->classpath->__toString();
  87. }
  88. if (empty($this->classpath)) {
  89. throw new BuildException("Provided classpath was empty.");
  90. }
  91. $curr_parts = explode(PATH_SEPARATOR, get_include_path());
  92. $add_parts = explode(PATH_SEPARATOR, $this->classpath);
  93. $new_parts = array_diff($add_parts, $curr_parts);
  94. if ($new_parts) {
  95. $this->log("Prepending new include_path components: " . implode(PATH_SEPARATOR, $new_parts), Project::MSG_VERBOSE);
  96. set_include_path(implode(PATH_SEPARATOR, array_merge($new_parts, $curr_parts)));
  97. }
  98. }
  99. }