PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/chregu/fluxcms
PHP | 113 lines | 42 code | 14 blank | 57 comment | 8 complexity | ab9eddb73e74eb71fca631b0582a2965 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 selects a certain kind of file: directory or regular file.
  24. *
  25. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  26. * @author Jeff Turner <jefft@apache.org> (Ant)
  27. * @version $Revision: 1.2 $
  28. * @package phing.types.selectors
  29. */
  30. class TypeSelector extends BaseExtendSelector {
  31. private $type;
  32. /** Key to used for parameterized custom selector */
  33. const TYPE_KEY = "type";
  34. /** Valid types */
  35. private static $types = array('file', 'dir');
  36. /**
  37. * @return string A string describing this object
  38. */
  39. public function toString() {
  40. $buf = "{typeselector type: " . $this->type . "}";
  41. return $buf;
  42. }
  43. /**
  44. * Set the type of file to require.
  45. * @param string $type The type of file - 'file' or 'dir'
  46. */
  47. public function setType($type) {
  48. $this->type = $type;
  49. }
  50. /**
  51. * When using this as a custom selector, this method will be called.
  52. * It translates each parameter into the appropriate setXXX() call.
  53. *
  54. * @param array $parameters the complete set of parameters for this selector
  55. */
  56. public function setParameters($parameters) {
  57. parent::setParameters($parameters);
  58. if ($parameters !== null) {
  59. for ($i = 0, $size=count($parameters); $i < $size; $i++) {
  60. $paramname = $parameters[$i]->getName();
  61. if (self::TYPE_KEY == strtolower($paramname)) {
  62. $this->setType($parameters[$i]->getValue());
  63. } else {
  64. $this->setError("Invalid parameter " . $paramname);
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Checks to make sure all settings are kosher. In this case, it
  71. * means that the pattern attribute has been set.
  72. *
  73. */
  74. public function verifySettings() {
  75. if ($this->type === null) {
  76. $this->setError("The type attribute is required");
  77. } elseif (!in_array($this->type, self::$types, true)) {
  78. $this->setError("Invalid type specified; must be one of (" . implode(self::$types) . ")");
  79. }
  80. }
  81. /**
  82. * The heart of the matter. This is where the selector gets to decide
  83. * on the inclusion of a file in a particular fileset.
  84. *
  85. * @param File $basedir the base directory the scan is being done from
  86. * @param string $filename is the name of the file to check
  87. * @param File $file is a File object the selector can use
  88. * @return boolean Whether the file should be selected or not
  89. */
  90. public function isSelected(File $basedir, $filename, File $file) {
  91. // throw BuildException on error
  92. $this->validate();
  93. if ($file->isDirectory()) {
  94. return $this->type === 'dir';
  95. } else {
  96. return $this->type === 'file';
  97. }
  98. }
  99. }