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 / tasks / ext / pearpackage / Fileset.php
Encoding:
PHP Script  |  2006-03-10  |  7.5 KB  |  232 lines

  1. <?php
  2. /*
  3.  *  $Id: Fileset.php 43 2006-03-10 14:31:51Z 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. include_once 'phing/system/io/PhingFile.php';
  23.  
  24. /**
  25.  * Builds list of files for PEAR_PackageFileManager using a Phing FileSet.
  26.  *
  27.  * Some code here is taken from PEAR_PackageFileManager_File -- getting results from flat
  28.  * array into the assoc array expected from getFileList().
  29.  * 
  30.  * @author   Greg Beaver 
  31.  * @author   Hans Lellelid <hans@xmpl.org>
  32.  * @package  phing.tasks.ext.pearpackage
  33.  * @version  $Revision: 1.7 $
  34.  */
  35. class PEAR_PackageFileManager_Fileset {
  36.  
  37.     /**
  38.      * @access private
  39.      * @var PEAR_PackageFileManager
  40.      */
  41.     private $parent;
  42.     
  43.     /**
  44.      * Curent Phing Project.
  45.      * @var Project
  46.      */
  47.     private $project;
  48.     
  49.     /**
  50.      * FileSets to use.
  51.      * @var array FileSet[]
  52.      */
  53.     private $filesets = array();
  54.  
  55.     /**
  56.      * Set up the FileSet filelist generator
  57.      *
  58.      * 'project' and 'filesets' are the only options that this class uses.
  59.      * 
  60.      * @param PEAR_PackageFileManager
  61.      * @param array
  62.      */
  63.     function __construct($parent, $options)
  64.     {
  65.         $this->parent = $parent;
  66.         $this->project = $options['phing_project'];
  67.         $this->filesets = $options['phing_filesets'];
  68.     }
  69.     
  70.     /**
  71.      * Generate the <filelist></filelist> section
  72.      * of the package file.
  73.      *
  74.      * This function performs the backend generation of the array
  75.      * containing all files in this package
  76.      * @return array structure of all files to include
  77.      */
  78.     function getFileList() {    
  79.  
  80.         $allfiles = array();        
  81.         
  82.         foreach($this->filesets as $fs) {
  83.             $ds = $fs->getDirectoryScanner($this->project);
  84.             
  85.             $files = $ds->getIncludedFiles();
  86.             
  87.             // We need to store these files keyed by the basedir from DirectoryScanner
  88.             // so that we can resolve the fullpath of the file later.
  89.             if (isset($allfiles[$ds->getBasedir()]))
  90.             {
  91.                 $allfiles[$ds->getBasedir()] = array_merge($allfiles[$ds->getBasedir()], $files);
  92.             }
  93.             else
  94.             {
  95.                 $allfiles[$ds->getBasedir()] = $files;
  96.             }
  97.         }
  98.         
  99.         $struc = array();
  100.         
  101.         foreach($allfiles as $basedir => $files) {
  102.         
  103.             foreach($files as $file) {
  104.                             
  105.                 // paths are relative to $basedir above
  106.                 $path = strtr(dirname($file), DIRECTORY_SEPARATOR, '/');
  107.     
  108.                 if (!$path || $path == '.') {
  109.                     $path = '/'; // for array index
  110.                 }
  111.                 
  112.                 $parts = explode('.', basename($file));
  113.                 $ext = array_pop($parts);
  114.                 if (strlen($ext) == strlen($file)) {
  115.                     $ext = '';
  116.                 }
  117.                 
  118.                 $f = new PhingFile($basedir, $file);
  119.                 
  120.                 $struc[$path][] = array('file' => basename($file),
  121.                                         'ext' => $ext,
  122.                                         'path' => (($path == '/') ? basename($file) : $path . '/' . basename($file)),
  123.                                         'fullpath' => $f->getAbsolutePath());        
  124.             }                                        
  125.         }
  126.                 
  127.         uksort($struc,'strnatcasecmp');
  128.         foreach($struc as $key => $ind) {
  129.             usort($ind, array($this, 'sortfiles'));
  130.             $struc[$key] = $ind;
  131.         }
  132.  
  133.         $tempstruc = $struc;
  134.         $struc = array('/' => $tempstruc['/']);
  135.         $bv = 0;
  136.         foreach($tempstruc as $key => $ind) {
  137.             $save = $key;
  138.             if ($key != '/') {
  139.                 $struc['/'] = $this->setupDirs($struc['/'], explode('/', $key), $tempstruc[$key]);
  140.             }
  141.         }
  142.         uksort($struc['/'], array($this, 'mystrucsort'));
  143.  
  144.         return $struc;
  145.     }
  146.  
  147.     /**
  148.      * Recursively move contents of $struc into associative array
  149.      *
  150.      * The contents of $struc have many indexes like 'dir/subdir/subdir2'.
  151.      * This function converts them to
  152.      * array('dir' => array('subdir' => array('subdir2')))
  153.      * @param array struc is array('dir' => array of files in dir,
  154.      *              'dir/subdir' => array of files in dir/subdir,...)
  155.      * @param array array form of 'dir/subdir/subdir2' array('dir','subdir','subdir2')
  156.      * @return array same as struc but with array('dir' =>
  157.      *              array(file1,file2,'subdir' => array(file1,...)))
  158.      */
  159.     private function setupDirs($struc, $dir, $contents) {
  160.     
  161.         if (!count($dir)) {
  162.             foreach($contents as $dir => $files) {
  163.                 if (is_string($dir)) {
  164.                     if (strpos($dir, '/')) {
  165.                         $test = true;
  166.                         $a = $contents[$dir];
  167.                         unset($contents[$dir]);
  168.                         $b = explode('/', $dir);
  169.                         $c = array_shift($b);
  170.                         if (isset($contents[$c])) {
  171.                             $contents[$c] = $this->setDir($contents[$c], $this->setupDirs(array(), $b, $a));
  172.                         } else {
  173.                             $contents[$c] = $this->setupDirs(array(), $b, $a);
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.             return $contents;
  179.         }
  180.         $me = array_shift($dir);
  181.         if (!isset($struc[$me])) {
  182.             $struc[$me] = array();
  183.         }
  184.         $struc[$me] = $this->setupDirs($struc[$me], $dir, $contents);
  185.         return $struc;
  186.     }
  187.     
  188.     /**
  189.      * Recursively add all the subdirectories of $contents to $dir without erasing anything in
  190.      * $dir
  191.      * @param array
  192.      * @param array
  193.      * @return array processed $dir
  194.      */
  195.     function setDir($dir, $contents)
  196.     {
  197.         while(list($one,$two) = each($contents)) {
  198.             if (isset($dir[$one])) {
  199.                 $dir[$one] = $this->setDir($dir[$one], $contents[$one]);
  200.             } else {
  201.                 $dir[$one] = $two;
  202.             }
  203.         }
  204.         return $dir;
  205.     }
  206.     
  207.     /**
  208.      * Sorting functions for the file list
  209.      * @param string
  210.      * @param string
  211.      * @access private
  212.      */
  213.     function sortfiles($a, $b)
  214.     {
  215.         return strnatcasecmp($a['file'],$b['file']);
  216.     }
  217.     
  218.     function mystrucsort($a, $b)
  219.     {
  220.         if (is_numeric($a) && is_string($b)) return 1;
  221.         if (is_numeric($b) && is_string($a)) return -1;
  222.         if (is_numeric($a) && is_numeric($b))
  223.         {
  224.             if ($a > $b) return 1;
  225.             if ($a < $b) return -1;
  226.             if ($a == $b) return 0;
  227.         }
  228.         return strnatcasecmp($a,$b);
  229.     }
  230. }
  231. ?>
  232.