PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/install/phing/classes/phing/types/selectors/DepthSelector.php

https://github.com/chregu/fluxcms
PHP | 158 lines | 80 code | 16 blank | 62 comment | 15 complexity | 1ffa4e3af6fad1247e37b01f6b4216dd MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * $Id$
  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/types/selectors/BaseExtendSelector.php';
  22. /**
  23. * Selector that filters files based on the how deep in the directory
  24. * tree they are.
  25. *
  26. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  27. * @author Bruce Atherton <bruce@callenish.com> (Ant)
  28. * @version $Revision: 1.6 $
  29. * @package phing.types.selectors
  30. */
  31. class DepthSelector extends BaseExtendSelector {
  32. public $min = -1;
  33. public $max = -1;
  34. const MIN_KEY = "min";
  35. const MAX_KEY = "max";
  36. public function toString() {
  37. $buf = "{depthselector min: ";
  38. $buf .= $this->min;
  39. $buf .= " max: ";
  40. $buf .= $this->max;
  41. $buf .= "}";
  42. return $buf;
  43. }
  44. /**
  45. * The minimum depth below the basedir before a file is selected.
  46. *
  47. * @param min minimum directory levels below basedir to go
  48. */
  49. public function setMin($min) {
  50. $this->min = (int) $min;
  51. }
  52. /**
  53. * The minimum depth below the basedir before a file is selected.
  54. *
  55. * @param min maximum directory levels below basedir to go
  56. */
  57. public function setMax($max) {
  58. $this->max = (int) $max;
  59. }
  60. /**
  61. * When using this as a custom selector, this method will be called.
  62. * It translates each parameter into the appropriate setXXX() call.
  63. *
  64. * @param parameters the complete set of parameters for this selector
  65. */
  66. public function setParameters($parameters) {
  67. parent::setParameters($parameters);
  68. if ($parameters !== null) {
  69. for ($i = 0, $size=count($parameters); $i < $size; $i++) {
  70. $paramname = $parameters[$i]->getName();
  71. switch(strtolower($paramname)) {
  72. case self::MIN_KEY:
  73. $this->setMin($parameters[$i]->getValue());
  74. break;
  75. case self::MAX_KEY:
  76. $this->setMax($parameters[$i]->getValue());
  77. break;
  78. default:
  79. $this->setError("Invalud parameter " . $paramname);
  80. } // switch
  81. }
  82. }
  83. }
  84. /**
  85. * Checks to make sure all settings are kosher. In this case, it
  86. * means that the max depth is not lower than the min depth.
  87. */
  88. public function verifySettings() {
  89. if ($this->min < 0 && $this->max < 0) {
  90. $this->setError("You must set at least one of the min or the " .
  91. "max levels.");
  92. }
  93. if ($this->max < $this->min && $this->max > -1) {
  94. $this->setError("The maximum depth is lower than the minimum.");
  95. }
  96. }
  97. /**
  98. * The heart of the matter. This is where the selector gets to decide
  99. * on the inclusion of a file in a particular fileset. Most of the work
  100. * for this selector is offloaded into SelectorUtils, a static class
  101. * that provides the same services for both FilenameSelector and
  102. * DirectoryScanner.
  103. *
  104. * @param basedir the base directory the scan is being done from
  105. * @param filename is the name of the file to check
  106. * @param file is a java.io.File object the selector can use
  107. * @return whether the file should be selected or not
  108. */
  109. public function isSelected(File $basedir, $filename, File $file) {
  110. $this->validate();
  111. $depth = -1;
  112. // If you felt daring, you could cache the basedir absolute path
  113. $abs_base = $basedir->getAbsolutePath();
  114. $abs_file = $file->getAbsolutePath();
  115. $tok_base = explode(DIRECTORY_SEPARATOR, $abs_base);
  116. $tok_file = explode(DIRECTORY_SEPARATOR, $abs_file);
  117. for($i=0,$size=count($tok_file); $i < $size; $i++) {
  118. $filetoken = $tok_file[$i];
  119. if (isset($tok_base[$i])) {
  120. $basetoken = $tok_base[$i];
  121. // Sanity check. Ditch it if you want faster performance
  122. if ($basetoken !== $filetoken) {
  123. throw new BuildException("File " . $filename .
  124. " does not appear within " . $abs_base . "directory");
  125. }
  126. } else { // no more basepath tokens
  127. $depth++;
  128. if ($this->max > -1 && $depth > $this->max) {
  129. return false;
  130. }
  131. }
  132. }
  133. if (isset($tok_base[$i + 1])) {
  134. throw new BuildException("File " . $filename .
  135. " is outside of " . $abs_base . "directory tree");
  136. }
  137. if ($this->min > -1 && $depth < $this->min) {
  138. return false;
  139. }
  140. return true;
  141. }
  142. }