home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / mappers / RegexpMapper.php < prev   
Encoding:
PHP Script  |  2006-09-14  |  3.1 KB  |  98 lines

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