RegexpMapper.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * $Id: RegexpMapper.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. require_once 'phing/mappers/FileNameMapper.php';
  22. include_once 'phing/util/StringHelper.php';
  23. include_once 'phing/util/regexp/Regexp.php';
  24. /**
  25. * Uses regular expressions to perform filename transformations.
  26. *
  27. * @author Andreas Aderhold <andi@binarycloud.com>
  28. * @author Hans Lellelid <hans@velum.net>
  29. * @version $Revision: 905 $
  30. * @package phing.mappers
  31. */
  32. class RegexpMapper implements FileNameMapper {
  33. /**
  34. * @var string
  35. */
  36. private $to;
  37. /**
  38. * The Regexp engine.
  39. * @var Regexp
  40. */
  41. private $reg;
  42. function __construct() {
  43. // instantiage regexp matcher here
  44. $this->reg = new Regexp();
  45. }
  46. /**
  47. * Sets the &quot;from&quot; pattern. Required.
  48. */
  49. function setFrom($from) {
  50. $this->reg->SetPattern($from);
  51. }
  52. /**
  53. * Sets the &quot;to&quot; pattern. Required.
  54. */
  55. function setTo($to) {
  56. // [HL] I'm changing the way this works for now to just use string
  57. //$this->to = StringHelper::toCharArray($to);
  58. $this->to = $to;
  59. }
  60. function main($sourceFileName) {
  61. if ($this->reg === null || $this->to === null || !$this->reg->matches((string) $sourceFileName)) {
  62. return null;
  63. }
  64. return array($this->replaceReferences($sourceFileName));
  65. }
  66. /**
  67. * Replace all backreferences in the to pattern with the matched groups.
  68. * groups of the source.
  69. * @param string $source The source filename.
  70. */
  71. private function replaceReferences($source) {
  72. // FIXME
  73. // Can't we just use engine->replace() to handle this? the Preg engine
  74. // will automatically convert \1 references to $1
  75. // the expression has already been processed (when ->matches() was run in Main())
  76. // so no need to pass $source again to the engine.
  77. $groups = (array) $this->reg->getGroups();
  78. // replace \1 with value of $groups[1] and return the modified "to" string
  79. return preg_replace('/\\\([\d]+)/e', "\$groups[$1]", $this->to);
  80. }
  81. }