"Fossies" - the Fresh Open Source Software Archive

Member "icinga-web-1.14.1/lib/phing/classes/phing/types/AbstractFileSet.php" (19 Dec 2017, 17767 Bytes) of package /linux/misc/old/icinga-web-1.14.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractFileSet.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /*
    3  *  $Id: AbstractFileSet.php 744 2010-03-09 10:08:31Z 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 include_once 'phing/types/DataType.php';
   24 include_once 'phing/types/PatternSet.php';
   25 include_once 'phing/types/selectors/BaseSelector.php';
   26 include_once 'phing/types/selectors/SelectorContainer.php';
   27 include_once 'phing/types/selectors/BaseSelectorContainer.php';
   28 
   29 // Load all of the selectors (not really necessary but
   30 // helps reveal parse errors right away)
   31 
   32 include_once 'phing/types/selectors/AndSelector.php';
   33 include_once 'phing/types/selectors/ContainsSelector.php';
   34 include_once 'phing/types/selectors/ContainsRegexpSelector.php';
   35 include_once 'phing/types/selectors/DateSelector.php';
   36 include_once 'phing/types/selectors/DependSelector.php';
   37 include_once 'phing/types/selectors/DepthSelector.php';
   38 include_once 'phing/types/selectors/ExtendSelector.php';
   39 include_once 'phing/types/selectors/FilenameSelector.php';
   40 include_once 'phing/types/selectors/MajoritySelector.php';
   41 include_once 'phing/types/selectors/NoneSelector.php';
   42 include_once 'phing/types/selectors/NotSelector.php';
   43 include_once 'phing/types/selectors/OrSelector.php';
   44 include_once 'phing/types/selectors/PresentSelector.php';
   45 include_once 'phing/types/selectors/SizeSelector.php';
   46 include_once 'phing/types/selectors/TypeSelector.php';
   47 
   48 include_once 'phing/util/DirectoryScanner.php';
   49 
   50 /**
   51  * The FileSet class provides methods and properties for accessing
   52  * and managing filesets. It extends ProjectComponent and thus inherits
   53  * all methods and properties (not explicitly declared). See ProjectComponent
   54  * for further detail.
   55  *
   56  * TODO:
   57  *   - merge this with patternsets: FileSet extends PatternSet !!!
   58  *     requires additional mods to the parsing algo
   59  *         [HL] .... not sure if that really makes so much sense.  I think
   60  *            that perhaps they should use common utility class if there really
   61  *            is that much shared functionality
   62  *
   63  * @author    Andreas Aderhold <andi@binarycloud.com>
   64  * @author    Hans Lellelid <hans@xmpl.org>
   65  * @version    $Revision: 744 $ $Date: 2010-03-09 11:08:31 +0100 (Tue, 09 Mar 2010) $
   66  * @see        ProjectComponent
   67  * @package    phing.types
   68  */
   69 class AbstractFileSet extends DataType implements SelectorContainer {
   70     
   71     // These vars are public for cloning purposes
   72     
   73     /**
   74      * @var boolean
   75      */
   76     public $useDefaultExcludes = true;
   77     
   78     /** 
   79      * Whether to expand/dereference symbolic links, default is false
   80      * @var boolean
   81      */
   82     protected $expandSymbolicLinks = false;
   83     
   84     /**
   85      * @var PatternSet
   86      */
   87     public $defaultPatterns;
   88     
   89     public $additionalPatterns = array();
   90     public $dir;
   91     public $isCaseSensitive = true;    
   92     public $selectors = array();
   93     
   94     function __construct($fileset = null) {
   95         if ($fileset !== null && ($fileset instanceof FileSet)) {
   96             $this->dir = $fileset->dir;
   97             $this->defaultPatterns = $fileset->defaultPatterns;
   98             $this->additionalPatterns = $fileset->additionalPatterns;
   99             $this->useDefaultExcludes = $fileset->useDefaultExcludes;
  100             $this->isCaseSensitive = $fileset->isCaseSensitive;
  101             $this->selectors = $fileset->selectors;
  102         }
  103         $this->defaultPatterns = new PatternSet();
  104     }
  105     
  106     /** 
  107      * Sets whether to expand/dereference symbolic links, default is false
  108      * @var boolean
  109      */
  110     function setExpandSymbolicLinks($expandSymbolicLinks)
  111     {
  112         $this->expandSymbolicLinks = $expandSymbolicLinks;
  113     }
  114 
  115     /**
  116     * Makes this instance in effect a reference to another PatternSet
  117     * instance.
  118     * You must not set another attribute or nest elements inside
  119     * this element if you make it a reference.
  120     */
  121     function setRefid(Reference $r) {
  122         if ((isset($this->dir) && !is_null($this->dir)) || $this->defaultPatterns->hasPatterns()) {
  123             throw $this->tooManyAttributes();
  124         }
  125         if (!empty($this->additionalPatterns)) {
  126             throw $this->noChildrenAllowed();
  127         }
  128         if (!empty($this->selectors)) {
  129             throw $this->noChildrenAllowed();
  130         }
  131         parent::setRefid($r);
  132     }
  133 
  134 
  135     function setDir($dir) {
  136         if ($this->isReference()) {
  137             throw $this->tooManyAttributes();
  138         }
  139         if ($dir instanceof PhingFile) {
  140             $dir = $dir->getPath();
  141         }
  142         $this->dir = new PhingFile((string) $dir);
  143     }
  144 
  145 
  146     function getDir(Project $p) {
  147         if ($this->isReference()) {
  148             return $this->getRef($p)->getDir($p);
  149         }
  150         return $this->dir;
  151     }
  152 
  153 
  154     function createPatternSet() {
  155         if ($this->isReference()) {
  156             throw $this->noChildrenAllowed();
  157         }
  158         $num = array_push($this->additionalPatterns, new PatternSet());
  159         return $this->additionalPatterns[$num-1];
  160     }
  161 
  162     /**
  163     * add a name entry on the include list
  164     */
  165     function createInclude() {
  166         if ($this->isReference()) {
  167             throw $this->noChildrenAllowed();
  168         }
  169         return $this->defaultPatterns->createInclude();
  170     }
  171 
  172     /**
  173      * add a name entry on the include files list
  174      */
  175     function createIncludesFile() {
  176         if ($this->isReference()) {
  177             throw $this->noChildrenAllowed();
  178         }
  179         return $this->defaultPatterns->createIncludesFile();
  180     }
  181 
  182     /**
  183      * add a name entry on the exclude list
  184      */
  185     function createExclude() {
  186         if ($this->isReference()) {
  187             throw $this->noChildrenAllowed();
  188         }
  189         return $this->defaultPatterns->createExclude();
  190     }
  191 
  192     /**
  193      * add a name entry on the include files list
  194      */
  195     function createExcludesFile() {
  196         if ($this->isReference()) {
  197             throw $this->noChildrenAllowed();
  198             return;
  199         }
  200         return $this->defaultPatterns->createExcludesFile();
  201     }
  202 
  203     /**
  204      * Sets the set of include patterns. Patterns may be separated by a comma
  205      * or a space.
  206      */
  207     function setIncludes($includes) {
  208         if ($this->isReference()) {
  209             throw $this->tooManyAttributes();
  210         }
  211         $this->defaultPatterns->setIncludes($includes);
  212     }
  213 
  214     /**
  215      * Sets the set of exclude patterns. Patterns may be separated by a comma
  216      * or a space.
  217      */
  218     function setExcludes($excludes) {
  219         if ($this->isReference()) {
  220             throw $this->tooManyAttributes();
  221         }
  222         $this->defaultPatterns->setExcludes($excludes);
  223     }
  224 
  225     /**
  226      * Sets the name of the file containing the includes patterns.
  227      *
  228      * @param $incl The file to fetch the include patterns from.
  229      * @throws BE
  230      */
  231     function setIncludesfile($incl) {
  232         if ($this->isReference()) {
  233             throw $this->tooManyAttributes();
  234         }
  235         $this->defaultPatterns->setIncludesfile($incl);
  236     }
  237 
  238     /**
  239      * Sets the name of the file containing the includes patterns.
  240      *
  241      * @param $excl The file to fetch the exclude patterns from.
  242      * @throws BE
  243      */
  244     function setExcludesfile($excl) {
  245         if ($this->isReference()) {
  246             throw $this->tooManyAttributes();
  247         }
  248         $this->defaultPatterns->setExcludesfile($excl);
  249     }
  250 
  251     /**
  252      * Sets whether default exclusions should be used or not.
  253      *
  254      * @param $useDefaultExcludes "true"|"on"|"yes" when default exclusions
  255      *                           should be used, "false"|"off"|"no" when they
  256      *                           shouldn't be used.
  257      */
  258     function setDefaultexcludes($useDefaultExcludes) {
  259         if ($this->isReference()) {
  260             throw $this->tooManyAttributes();
  261         }
  262         $this->useDefaultExcludes = $useDefaultExcludes;
  263     }
  264 
  265     /**
  266      * Sets case sensitivity of the file system
  267      */
  268     function setCaseSensitive($isCaseSensitive) {
  269         $this->isCaseSensitive = $isCaseSensitive;
  270     }
  271 
  272     /** returns a reference to the dirscanner object belonging to this fileset */
  273     function getDirectoryScanner(Project $p) {
  274         if ($this->isReference()) {
  275             $o = $this->getRef($p);
  276             return $o->getDirectoryScanner($p);
  277         }
  278 
  279         if ($this->dir === null) {
  280             throw new BuildException("No directory specified for fileset.");
  281         }
  282         if (!$this->dir->exists()) {
  283             throw new BuildException("Directory ".$this->dir->getAbsolutePath()." not found.");
  284         }
  285         if (!$this->dir->isLink() || !$this->expandSymbolicLinks) {
  286             if (!$this->dir->isDirectory()) {
  287                 throw new BuildException($this->dir->getAbsolutePath()." is not a directory.");
  288             }
  289         }
  290         $ds = new DirectoryScanner();
  291         $ds->setExpandSymbolicLinks($this->expandSymbolicLinks);
  292         $this->setupDirectoryScanner($ds, $p);
  293         $ds->scan();
  294         return $ds;
  295     }
  296 
  297     /** feed dirscanner with infos defined by this fileset */
  298     protected function setupDirectoryScanner(DirectoryScanner $ds, Project $p) {
  299         if ($ds === null) {
  300             throw new Exception("DirectoryScanner cannot be null");
  301         }
  302         // FIXME - pass dir directly wehn dirscanner supports File
  303         $ds->setBasedir($this->dir->getPath());
  304         
  305         foreach($this->additionalPatterns as $addPattern) {
  306             $this->defaultPatterns->append($addPattern, $p);
  307         }              
  308 
  309         $ds->setIncludes($this->defaultPatterns->getIncludePatterns($p));
  310         $ds->setExcludes($this->defaultPatterns->getExcludePatterns($p));
  311 
  312         $p->log("FileSet: Setup file scanner in dir " . $this->dir->__toString() . " with " . $this->defaultPatterns->toString(), Project::MSG_DEBUG);
  313         
  314         if ($ds instanceof SelectorScanner) {
  315             $ds->setSelectors($this->getSelectors($p));
  316         }
  317         
  318         if ($this->useDefaultExcludes) {
  319             $ds->addDefaultExcludes();
  320         }
  321         $ds->setCaseSensitive($this->isCaseSensitive);
  322     }
  323 
  324 
  325     /**
  326      * Performs the check for circular references and returns the
  327      * referenced FileSet.
  328      */
  329     function getRef(Project $p) {
  330         if (!$this->checked) {
  331             $stk = array();
  332             array_push($stk, $this);
  333             $this->dieOnCircularReference($stk, $p);            
  334         }
  335 
  336         $o = $this->ref->getReferencedObject($p);
  337         if (!($o instanceof FileSet)) {
  338             $msg = $this->ref->getRefId()." doesn't denote a fileset";
  339             throw new BuildException($msg);
  340         } else {
  341             return $o;
  342         }
  343     }
  344     
  345     // SelectorContainer methods
  346 
  347     /**
  348      * Indicates whether there are any selectors here.
  349      *
  350      * @return boolean Whether any selectors are in this container
  351      */
  352     public function hasSelectors() {
  353         if ($this->isReference() && $this->getProject() !== null) {
  354             return $this->getRef($this->getProject())->hasSelectors();
  355         }
  356         return !empty($this->selectors);
  357     }
  358 
  359     /**
  360      * Indicates whether there are any patterns here.
  361      *
  362      * @return boolean Whether any patterns are in this container.
  363      */
  364     public function hasPatterns() {
  365     
  366         if ($this->isReference() && $this->getProject() !== null) {
  367             return $this->getRef($this->getProject())->hasPatterns();            
  368         }
  369 
  370         if ($this->defaultPatterns->hasPatterns($this->getProject())) {
  371             return true;
  372         }
  373 
  374         for($i=0,$size=count($this->additionalPatterns); $i < $size; $i++) {
  375             $ps = $this->additionalPatterns[$i];
  376             if ($ps->hasPatterns($this->getProject())) {
  377                 return true;
  378             }
  379         }
  380 
  381         return false;
  382     }
  383     
  384     /**
  385      * Gives the count of the number of selectors in this container
  386      *
  387      * @return int The number of selectors in this container
  388      */
  389     public function selectorCount() {
  390         if ($this->isReference() && $this->getProject() !== null) {
  391             try {
  392                 return $this->getRef($this->getProject())->selectorCount();
  393             } catch (Exception $e) {
  394                 throw $e;
  395             }
  396         }
  397         return count($this->selectors);
  398     }
  399 
  400     /**
  401      * Returns the set of selectors as an array.
  402      *
  403      * @return an array of selectors in this container
  404      */
  405     public function getSelectors(Project $p) {
  406         if ($this->isReference()) {
  407             return $this->getRef($p)->getSelectors($p);            
  408         } else {
  409             // *copy* selectors
  410             $result = array();
  411             for($i=0,$size=count($this->selectors); $i < $size; $i++) {
  412                 $result[] = clone $this->selectors[$i];
  413             }
  414             return $result;
  415         }
  416     }
  417 
  418     /**
  419      * Returns an array for accessing the set of selectors.
  420      *
  421      * @return array The array of selectors
  422      */
  423     public function selectorElements() {
  424         if ($this->isReference() && $this->getProject() !== null) {
  425             return $this->getRef($this->getProject())->selectorElements();            
  426         }
  427         return $this->selectors;
  428     }
  429 
  430     /**
  431      * Add a new selector into this container.
  432      *
  433      * @param selector the new selector to add
  434      */
  435     public function appendSelector(FileSelector $selector) {
  436         if ($this->isReference()) {
  437             throw $this->noChildrenAllowed();
  438         }
  439         $this->selectors[] = $selector;
  440     }    
  441 
  442     /* Methods below all add specific selectors */
  443 
  444     /**
  445      * add a "Select" selector entry on the selector list
  446      */
  447     public function createSelector() {
  448         $o = new SelectSelector();
  449         $this->appendSelector($o);
  450         return $o;
  451     }
  452 
  453     /**
  454      * add an "And" selector entry on the selector list
  455      */
  456     public function createAnd() {
  457         $o = new AndSelector();
  458         $this->appendSelector($o);
  459         return $o;
  460     }
  461 
  462     /**
  463      * add an "Or" selector entry on the selector list
  464      */
  465     public function createOr() {
  466         $o = new OrSelector();
  467         $this->appendSelector($o);
  468         return $o;
  469     }
  470 
  471     /**
  472      * add a "Not" selector entry on the selector list
  473      */
  474     public function createNot() {
  475         $o = new NotSelector();
  476         $this->appendSelector($o);
  477         return $o;
  478     }
  479 
  480     /**
  481      * add a "None" selector entry on the selector list
  482      */
  483     public function createNone() {
  484         $o = new NoneSelector();
  485         $this->appendSelector($o);
  486         return $o;
  487     }
  488 
  489     /**
  490      * add a majority selector entry on the selector list
  491      */
  492     public function createMajority() {
  493         $o = new MajoritySelector();
  494         $this->appendSelector($o);
  495         return $o;
  496     }
  497 
  498     /**
  499      * add a selector date entry on the selector list
  500      */
  501     public function createDate() {
  502         $o = new DateSelector();
  503         $this->appendSelector($o);
  504         return $o;
  505     }
  506 
  507     /**
  508      * add a selector size entry on the selector list
  509      */
  510     public function createSize() {
  511         $o = new SizeSelector();
  512         $this->appendSelector($o);
  513         return $o;
  514     }
  515 
  516     /**
  517      * add a selector filename entry on the selector list
  518      */
  519     public function createFilename() {
  520         $o = new FilenameSelector();
  521         $this->appendSelector($o);
  522         return $o;
  523     }
  524 
  525     /**
  526      * add an extended selector entry on the selector list
  527      */
  528     public function createCustom() {
  529         $o = new ExtendSelector();
  530         $this->appendSelector($o);
  531         return $o;
  532     }
  533 
  534     /**
  535      * add a contains selector entry on the selector list
  536      */
  537     public function createContains() {
  538         $o = new ContainsSelector();
  539         $this->appendSelector($o);
  540         return $o;
  541     }
  542 
  543     /**
  544      * add a contains selector entry on the selector list
  545      */
  546     public function createContainsRegexp() {
  547         $o = new ContainsRegexpSelector();
  548         $this->appendSelector($o);
  549         return $o;
  550     }
  551 
  552     /**
  553      * add a present selector entry on the selector list
  554      */
  555     public function createPresent() {
  556         $o = new PresentSelector();
  557         $this->appendSelector($o);
  558         return $o;
  559     }
  560 
  561     /**
  562      * add a depth selector entry on the selector list
  563      */
  564     public function createDepth() {
  565         $o = new DepthSelector();
  566         $this->appendSelector($o);
  567         return $o;
  568     }
  569 
  570     /**
  571      * add a depends selector entry on the selector list
  572      */
  573     public function createDepend() {        
  574         $o = new DependSelector();
  575         $this->appendSelector($o);
  576         return $o;
  577     }
  578     
  579     /**
  580      * add a type selector entry on the selector list
  581      */
  582     public function createType() {
  583         $o = new TypeSelector();
  584         $this->appendSelector($o);
  585         return $o;
  586     }
  587 }