summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/types/selectors/ExtendSelector.php
blob: cc939254e163516aef94306b7f2e28121e623e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

/*
 * $Id: ExtendSelector.php,v 1.10 2005/05/26 13:10:53 mrook Exp $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
 */

include_once 'phing/util/StringHelper.php';

/**
 * Selector that selects files by forwarding the request on to other classes.
 *
 * TODO:
 *        Consider adding Path (org.apache.tools.ant.types.Path) support to this class
 *         and to the Mappers class.  See Ant versions for implimentation details.
 *
 * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 * @package phing.types.selectors
 */
class ExtendSelector extends BaseSelector {

    private $classname;
    private $dynselector;
    private $parameters = array();

    /**
     * Sets the classname of the custom selector.
     *
     * @param classname is the class which implements this selector
     */
    public function setClassname($classname) {
        $this->classname = $classname;
    }

    /**
     * Instantiates the identified custom selector class.
     */
    public function selectorCreate() {
        if ($this->classname !== null && $this->classname !== "") {      
            try {
                // assume it's fully qualified, import it
                $cls = Phing::import($this->classname);
       
                // make sure class exists
                if (class_exists($cls)) {
                    $this->dynselector = new $cls();
                } else {
                    $this->setError("Selector " . $this->classname . " not initialized, no such class");
                }            
            } catch (Exception $e) {
                $this->setError("Selector " . $this->classname . " not initialized, could not create class: " . $e->getMessage());
            }            
        } else {
            $this->setError("There is no classname specified");
        }
    }

    /**
     * Create new parameters to pass to custom selector.
     *
     * @param p The new Parameter object
     */
    public function addParam(Parameter $p) {
        $this->parameters[] = $p;
    }

    /**
     * These are errors specific to ExtendSelector only. If there are
     * errors in the custom selector, it should throw a BuildException
     * when isSelected() is called.
     */
    public function verifySettings() {
        // Creation is done here rather than in isSelected() because some
        // containers may do a validation pass before running isSelected(),
        // but we need to check for the existence of the created class.
        if ($this->dynselector === null) {
            $this->selectorCreate();
        }
        
        if (empty($this->classname)) {
            $this->setError("The classname attribute is required");
        } elseif ($this->dynselector === null) {
            $this->setError("Internal Error: The custom selector was not created");
        } elseif ( !($this->dynselector instanceof ExtendFileSelector) && (count($this->parameters) > 0)) {
            $this->setError("Cannot set parameters on custom selector that does not "
                   . "implement ExtendFileSelector.");
        }
    }


    /**
     * Allows the custom selector to choose whether to select a file. This
     * is also where the Parameters are passed to the custom selector,
     * since we know we must have them all by now. And since we must know
     * both classpath and classname, creating the class is deferred to here
     * as well.
     *
     * @throws BuildException
     */
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file) {
        
        $this->validate();
        
        if (count($this->parameters) > 0 && $this->dynselector instanceof ExtendFileSelector) {            
            // We know that dynselector must be non-null if no error message
            $this->dynselector->setParameters($this->parameters);
        }
        return $this->dynselector->isSelected($basedir, $filename, $file);
    }

}