PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/chregu/fluxcms
PHP | 228 lines | 119 code | 17 blank | 92 comment | 23 complexity | b0152fe4e696281281c26f458ac137d9 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. /**
  22. * Selector that filters files based on their size.
  23. *
  24. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  25. * @author Bruce Atherton <bruce@callenish.com> (Ant)
  26. * @package phing.types.selectors
  27. */
  28. class SizeSelector extends BaseExtendSelector {
  29. private $size = -1;
  30. private $multiplier = 1;
  31. private $sizelimit = -1;
  32. private $cmp = 2;
  33. const SIZE_KEY = "value";
  34. const UNITS_KEY = "units";
  35. const WHEN_KEY = "when";
  36. private static $sizeComparisons = array("less", "more", "equal");
  37. private static $byteUnits = array("K", "k", "kilo", "KILO",
  38. "Ki", "KI", "ki", "kibi", "KIBI",
  39. "M", "m", "mega", "MEGA",
  40. "Mi", "MI", "mi", "mebi", "MEBI",
  41. "G", "g", "giga", "GIGA",
  42. "Gi", "GI", "gi", "gibi", "GIBI",
  43. "T", "t", "tera", "TERA",
  44. /* You wish! */ "Ti", "TI", "ti", "tebi", "TEBI"
  45. );
  46. public function toString() {
  47. $buf = "{sizeselector value: ";
  48. $buf .= $this->sizelimit;
  49. $buf .= "compare: ";
  50. if ($this->cmp === 0) {
  51. $buf .= "less";
  52. } elseif ($this->cmp === 1) {
  53. $buf .= "more";
  54. } else {
  55. $buf .= "equal";
  56. }
  57. $buf .= "}";
  58. return $buf;
  59. }
  60. /**
  61. * A size selector needs to know what size to base its selecting on.
  62. * This will be further modified by the multiplier to get an
  63. * actual size limit.
  64. *
  65. * @param size the size to select against expressed in units
  66. */
  67. public function setValue($size) {
  68. $this->size = $size;
  69. if (($this->multiplier !== 0) && ($this->size > -1)) {
  70. $this->sizelimit = $size * $this->multiplier;
  71. }
  72. }
  73. /**
  74. * Sets the units to use for the comparison. This is a little
  75. * complicated because common usage has created standards that
  76. * play havoc with capitalization rules. Thus, some people will
  77. * use "K" for indicating 1000's, when the SI standard calls for
  78. * "k". Others have tried to introduce "K" as a multiple of 1024,
  79. * but that falls down when you reach "M", since "m" is already
  80. * defined as 0.001.
  81. * <p>
  82. * To get around this complexity, a number of standards bodies
  83. * have proposed the 2^10 standard, and at least one has adopted
  84. * it. But we are still left with a populace that isn't clear on
  85. * how capitalization should work.
  86. * <p>
  87. * We therefore ignore capitalization as much as possible.
  88. * Completely mixed case is not possible, but all upper and lower
  89. * forms are accepted for all long and short forms. Since we have
  90. * no need to work with the 0.001 case, this practice works here.
  91. * <p>
  92. * This function translates all the long and short forms that a
  93. * unit prefix can occur in and translates them into a single
  94. * multiplier.
  95. *
  96. * @param $units The units to compare the size to.
  97. * @return void
  98. */
  99. public function setUnits($units) {
  100. $i = array_search($units, self::$byteUnits, true);
  101. if ($i === false) $i = -1; // make it java-like
  102. $this->multiplier = 0;
  103. if (($i > -1) && ($i < 4)) {
  104. $this->multiplier = 1000;
  105. } elseif (($i > 3) && ($i < 9)) {
  106. $this->multiplier = 1024;
  107. } elseif (($i > 8) && ($i < 13)) {
  108. $this->multiplier = 1000000;
  109. } elseif (($i > 12) && ($i < 18)) {
  110. $this->multiplier = 1048576;
  111. } elseif (($i > 17) && ($i < 22)) {
  112. $this->multiplier = 1000000000;
  113. } elseif (($i > 21) && ($i < 27)) {
  114. $this->multiplier = 1073741824;
  115. } elseif (($i > 26) && ($i < 31)) {
  116. $this->multiplier = 1000000000000;
  117. } elseif (($i > 30) && ($i < 36)) {
  118. $this->multiplier = 1099511627776;
  119. }
  120. if (($this->multiplier > 0) && ($this->size > -1)) {
  121. $this->sizelimit = $this->size * $this->multiplier;
  122. }
  123. }
  124. /**
  125. * This specifies when the file should be selected, whether it be
  126. * when the file matches a particular size, when it is smaller,
  127. * or whether it is larger.
  128. *
  129. * @param cmp The comparison to perform, an EnumeratedAttribute
  130. */
  131. public function setWhen($cmp) {
  132. $c = array_search($cmp, self::$sizeComparisons, true);
  133. if ($c !== false) {
  134. $this->cmp = $c;
  135. }
  136. }
  137. /**
  138. * When using this as a custom selector, this method will be called.
  139. * It translates each parameter into the appropriate setXXX() call.
  140. *
  141. * @param parameters the complete set of parameters for this selector
  142. */
  143. public function setParameters($parameters) {
  144. parent::setParameters($parameters);
  145. if ($parameters !== null) {
  146. for ($i = 0, $size=count($parameters); $i < $size; $i++) {
  147. $paramname = $parameters[$i]->getName();
  148. switch(strtolower($paramname)) {
  149. case self::SIZE_KEY:
  150. try {
  151. $this->setValue($parameters[$i]->getValue());
  152. } catch (Exception $nfe) {
  153. $this->setError("Invalid size setting "
  154. . $parameters[$i]->getValue());
  155. }
  156. break;
  157. case self::UNITS_KEY:
  158. $this->setUnits($parameters[$i]->getValue());
  159. break;
  160. case self::WHEN_KEY:
  161. $this->setWhen($parameters[$i]->getValue());
  162. break;
  163. default:
  164. $this->setError("Invalid parameter " . $paramname);
  165. }
  166. }
  167. }
  168. }
  169. /**
  170. * <p>Checks to make sure all settings are kosher. In this case, it
  171. * means that the size attribute has been set (to a positive value),
  172. * that the multiplier has a valid setting, and that the size limit
  173. * is valid. Since the latter is a calculated value, this can only
  174. * fail due to a programming error.
  175. * </p>
  176. * <p>If a problem is detected, the setError() method is called.
  177. * </p>
  178. */
  179. public function verifySettings() {
  180. if ($this->size < 0) {
  181. $this->setError("The value attribute is required, and must be positive");
  182. } elseif ($this->multiplier < 1) {
  183. $this->setError("Invalid Units supplied, must be K,Ki,M,Mi,G,Gi,T,or Ti");
  184. } elseif ($this->sizelimit < 0) {
  185. $this->setError("Internal error: Code is not setting sizelimit correctly");
  186. }
  187. }
  188. /**
  189. * The heart of the matter. This is where the selector gets to decide
  190. * on the inclusion of a file in a particular fileset.
  191. *
  192. * @param basedir A java.io.File object for the base directory
  193. * @param filename The name of the file to check
  194. * @param file A File object for this filename
  195. * @return whether the file should be selected or not
  196. */
  197. public function isSelected(File $basedir, $filename, File $file) {
  198. $this->validate();
  199. // Directory size never selected for
  200. if ($file->isDirectory()) {
  201. return true;
  202. }
  203. if ($this->cmp === 0) {
  204. return ($file->length() < $this->sizelimit);
  205. } elseif ($this->cmp === 1) {
  206. return ($file->length() > $this->sizelimit);
  207. } else {
  208. return ($file->length() === $this->sizelimit);
  209. }
  210. }
  211. }