GlobMapper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * $Id: GlobMapper.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. include_once 'phing/mappers/FileNameMapper.php';
  22. /**
  23. * description here
  24. *
  25. * @author Andreas Aderhold, andi@binarycloud.com
  26. * @version $Revision: 905 $
  27. * @package phing.mappers
  28. */
  29. class GlobMapper implements FileNameMapper {
  30. /**
  31. * Part of &quot;from&quot; pattern before the *.
  32. */
  33. private $fromPrefix = null;
  34. /**
  35. * Part of &quot;from&quot; pattern after the *.
  36. */
  37. private $fromPostfix = null;
  38. /**
  39. * Length of the prefix (&quot;from&quot; pattern).
  40. */
  41. private $prefixLength;
  42. /**
  43. * Length of the postfix (&quot;from&quot; pattern).
  44. */
  45. private $postfixLength;
  46. /**
  47. * Part of &quot;to&quot; pattern before the *.
  48. */
  49. private $toPrefix = null;
  50. /**
  51. * Part of &quot;to&quot; pattern after the *.
  52. */
  53. private $toPostfix = null;
  54. function main($_sourceFileName) {
  55. if (($this->fromPrefix === null)
  56. || !StringHelper::startsWith($this->fromPrefix, $_sourceFileName)
  57. || !StringHelper::endsWith($this->fromPostfix, $_sourceFileName)) {
  58. return null;
  59. }
  60. $varpart = $this->_extractVariablePart($_sourceFileName);
  61. $substitution = $this->toPrefix.$varpart.$this->toPostfix;
  62. return array($substitution);
  63. }
  64. function setFrom($from) {
  65. $index = strrpos($from, '*');
  66. if ($index === false) {
  67. $this->fromPrefix = $from;
  68. $this->fromPostfix = "";
  69. } else {
  70. $this->fromPrefix = substr($from, 0, $index);
  71. $this->fromPostfix = substr($from, $index+1);
  72. }
  73. $this->prefixLength = strlen($this->fromPrefix);
  74. $this->postfixLength = strlen($this->fromPostfix);
  75. }
  76. /**
  77. * Sets the &quot;to&quot; pattern. Required.
  78. */
  79. function setTo($to) {
  80. $index = strrpos($to, '*');
  81. if ($index === false) {
  82. $this->toPrefix = $to;
  83. $this->toPostfix = "";
  84. } else {
  85. $this->toPrefix = substr($to, 0, $index);
  86. $this->toPostfix = substr($to, $index+1);
  87. }
  88. }
  89. private function _extractVariablePart($_name) {
  90. // ergh, i really hate php's string functions .... all but natural
  91. $start = ($this->prefixLength === 0) ? 0 : $this->prefixLength;
  92. $end = ($this->postfixLength === 0) ? strlen($_name) : strlen($_name) - $this->postfixLength;
  93. $len = $end-$start;
  94. return substr($_name, $start, $len);
  95. }
  96. }