Completed
Push — master ( 0f28ca...ccc575 )
by Siad
17:36 queued 13s
created

classes/phing/dispatch/DispatchUtils.php (1 issue)

Severity
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * Determines and Executes the action method for the task.
22
 *
23
 * @author  Siad Ardroumli <[email protected]>
24
 * @package phing.dispatch
25
 */
26
class DispatchUtils
27
{
28
    /**
29
     * Determines and Executes the action method for the task.
30
     *
31
     * @param  object $task the task to execute.
32
     * @throws BuildException on error.
33
     */
34 614
    public static function main($task)
35
    {
36 614
        $methodName = "main";
37 614
        $dispatchable = null;
38
        try {
39 614
            if ($task instanceof Dispatchable) {
40
                $dispatchable = $task;
41 614
            } elseif ($task instanceof UnknownElement) {
42 604
                $ue = $task;
43 604
                $realThing = $ue->getRealThing();
44 604
                if ($realThing != null && $realThing instanceof Dispatchable && $realThing instanceof Task) {
45 1
                    $dispatchable = $realThing;
46
                }
47
            }
48 614
            if ($dispatchable != null) {
49 1
                $mName = null;
0 ignored issues
show
The assignment to $mName is dead and can be removed.
Loading history...
50
51 1
                $name = trim($dispatchable->getActionParameterName());
52 1
                if (empty($name)) {
53
                    throw new BuildException(
54
                        "Action Parameter Name must not be empty for Dispatchable Task."
55
                    );
56
                }
57 1
                $mName = "get" . ucfirst($name);
58
                try {
59 1
                    $c = new ReflectionClass($dispatchable);
60 1
                    $actionM = $c->getMethod($mName);
61 1
                    $o = $actionM->invoke($dispatchable);
62 1
                    $methodName = trim((string) $o);
63 1
                    if (empty($methodName)) {
64
                        throw new ReflectionException();
65
                    }
66
                } catch (ReflectionException $re) {
67
                    throw new BuildException(
68
                        "Dispatchable Task attribute '" . $name . "' not set or value is empty."
69
                    );
70
                }
71 1
                $executeM = $c->getMethod($methodName);
72 1
                $executeM->invoke($dispatchable);
73
74 1
                if ($task instanceof UnknownElement) {
75 1
                    $task->setRealThing(null);
76
                }
77
            } else {
78
                try {
79 614
                    $refl = new ReflectionClass($task);
80 614
                    $executeM = $refl->getMethod($methodName);
81
                } catch (ReflectionException $re) {
82
                    throw new BuildException("No public " . $methodName . "() in " . get_class($task));
83
                }
84 614
                $executeM->invoke($task);
85 584
                if ($task instanceof UnknownElement) {
86 575
                    $task->setRealThing(null);
87
                }
88
            }
89 140
        } catch (ReflectionException $e) {
90
            throw new BuildException($e);
91
        }
92 584
    }
93
}
94