PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/chregu/fluxcms
PHP | 157 lines | 117 code | 7 blank | 33 comment | 4 complexity | 16fa333db9cbd69cd25a56c3ae72e586 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. include_once 'phing/types/selectors/BaseExtendSelector.php';
  22. /**
  23. * Selector that filters files based on the filename.
  24. *
  25. * @author Hans Lellelid, hans@xmpl.org (Phing)
  26. * @author Bruce Atherton, bruce@callenish.com (Ant)
  27. * @package phing.types.selectors
  28. */
  29. class FilenameSelector extends BaseExtendSelector {
  30. private $pattern = null;
  31. private $casesensitive = true;
  32. private $negated = false;
  33. const NAME_KEY = "name";
  34. const CASE_KEY = "casesensitive";
  35. const NEGATE_KEY = "negate";
  36. public function toString() {
  37. $buf = "{filenameselector name: ";
  38. $buf .= $this->pattern;
  39. $buf .= " negate: ";
  40. if ($this->negated) {
  41. $buf .= "true";
  42. } else {
  43. $buf .= "false";
  44. }
  45. $buf .= " casesensitive: ";
  46. if ($this->casesensitive) {
  47. $buf .= "true";
  48. } else {
  49. $buf .= "false";
  50. }
  51. $buf .= "}";
  52. return $buf;
  53. }
  54. /**
  55. * The name of the file, or the pattern for the name, that
  56. * should be used for selection.
  57. *
  58. * @param pattern the file pattern that any filename must match
  59. * against in order to be selected.
  60. */
  61. public function setName($pattern) {
  62. $pattern = str_replace('\\', DIRECTORY_SEPARATOR, $pattern);
  63. $pattern = str_replace('/', DIRECTORY_SEPARATOR, $pattern);
  64. if (StringHelper::endsWith(DIRECTORY_SEPARATOR, $pattern)) {
  65. $pattern .= "**";
  66. }
  67. $this->pattern = $pattern;
  68. }
  69. /**
  70. * Whether to ignore case when checking filenames.
  71. *
  72. * @param casesensitive whether to pay attention to case sensitivity
  73. */
  74. public function setCasesensitive($casesensitive) {
  75. $this->casesensitive = $casesensitive;
  76. }
  77. /**
  78. * You can optionally reverse the selection of this selector,
  79. * thereby emulating an &lt;exclude&gt; tag, by setting the attribute
  80. * negate to true. This is identical to surrounding the selector
  81. * with &lt;not&gt;&lt;/not&gt;.
  82. *
  83. * @param negated whether to negate this selection
  84. */
  85. public function setNegate($negated) {
  86. $this->negated = $negated;
  87. }
  88. /**
  89. * When using this as a custom selector, this method will be called.
  90. * It translates each parameter into the appropriate setXXX() call.
  91. *
  92. * @param array $parameters the complete set of parameters for this selector
  93. */
  94. public function setParameters($parameters) {
  95. parent::setParameters($parameters);
  96. if ($parameters !== null) {
  97. for ($i=0, $len=count($parameters); $i < $len; $i++) {
  98. $paramname = $parameters[$i]->getName();
  99. switch(strtolower($paramname)) {
  100. case self::NAME_KEY:
  101. $this->setName($parameters[$i]->getValue());
  102. break;
  103. case self::CASE_KEY:
  104. $this->setCasesensitive($parameters[$i]->getValue());
  105. break;
  106. case self::NEGATE_KEY:
  107. $this->setNegate($parameters[$i]->getValue());
  108. break;
  109. default:
  110. $this->setError("Invalid parameter " . $paramname);
  111. }
  112. } // for each param
  113. } // if params
  114. }
  115. /**
  116. * Checks to make sure all settings are kosher. In this case, it
  117. * means that the name attribute has been set.
  118. *
  119. */
  120. public function verifySettings() {
  121. if ($this->pattern === null) {
  122. $this->setError("The name attribute is required");
  123. }
  124. }
  125. /**
  126. * The heart of the matter. This is where the selector gets to decide
  127. * on the inclusion of a file in a particular fileset. Most of the work
  128. * for this selector is offloaded into SelectorUtils, a static class
  129. * that provides the same services for both FilenameSelector and
  130. * DirectoryScanner.
  131. *
  132. * @param basedir the base directory the scan is being done from
  133. * @param filename is the name of the file to check
  134. * @param file is a java.io.File object the selector can use
  135. * @return whether the file should be selected or not
  136. */
  137. public function isSelected(File $basedir, $filename, File $file) {
  138. $this->validate();
  139. return (SelectorUtils::matchPath($this->pattern, $filename, $this->casesensitive)
  140. === !($this->negated));
  141. }
  142. }