summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/contrib/DocBlox/Parallel/Worker.php
blob: 338f4b258bd2883404644b490fb05c94942800b2 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
 * DocBlox
 *
 * PHP Version 5
 *
 * @category  DocBlox
 * @package   Parallel
 * @author    Mike van Riel <mike.vanriel@naenius.com>
 * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
 * @link      http://docblox-project.org
 */

/**
 * Class that represents the execution of a single task within a parallelized
 * frame.
 *
 * @category DocBlox
 * @package  Parallel
 * @author   Mike van Riel <mike.vanriel@naenius.com>
 * @license  http://www.opensource.org/licenses/mit-license.php MIT
 * @link     http://docblox-project.org
 */
class DocBlox_Parallel_Worker
{
    /** @var callback the task to execute for this worker */
    protected $task = null;

    /** @var mixed[] A list of argument to pass to the task */
    protected $arguments = array();

    /** @var int The return code to tell the parent process how it went */
    protected $return_code = -1;

    /** @var mixed The result of the given task */
    protected $result = '';

    /** @var string The error message, if an error occurred */
    protected $error = '';

    /**
     * Creates the worker and sets the task to execute optionally including
     * the arguments that need to be passed to the task.
     *
     * @param callback $task      The task to invoke upon execution.
     * @param mixed[]  $arguments The arguments to provide to the task.
     */
    function __construct($task, array $arguments = array())
    {
        $this->setTask($task);
        $this->arguments = $arguments;
    }

    /**
     * Returns the list of arguments as provided int he constructor.
     *
     * @see DocBlox_Parallel_Worker::__construct()
     *
     * @return mixed[]
     */
    public function getArguments()
    {
        return $this->arguments;
    }

    /**
     * Returns the task as provided in the constructor.
     *
     * @see DocBlox_Parallel_Worker::__construct()
     *
     * @return callback
     */
    public function getTask()
    {
        return $this->task;
    }

    /**
     * Returns the available return code.
     *
     * This method may return -1 if no return code is available yet.
     *
     * @return int
     */
    public function getReturnCode()
    {
        return $this->return_code;
    }

    /**
     * Sets the return code for this worker.
     *
     * Recommended is to use the same codes as are used with
     * {@link http://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
     * exit codes}.
     *
     * In short: 0 means that the task succeeded and a any other positive value
     * indicates an error condition.
     *
     * @param int $return_code Recommended to be a positive number
     *
     * @throw InvalidArgumentException if the code is not a number or negative
     *
     * @return void
     */
    public function setReturnCode($return_code)
    {
        if (!is_numeric($return_code) || ($return_code  < 0)) {
            throw new InvalidArgumentException(
                'Expected the return code to be a positive number'
            );
        }

        $this->return_code = $return_code;
    }

    /**
     * Returns the error message associated with the return code.
     *
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * Sets the error message.
     *
     * @param string $error The error message.
     *
     * @return void
     */
    public function setError($error)
    {
        $this->error = $error;
    }

    /**
     * Returns the result for this task run.
     *
     * @return null|mixed
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
     * Sets the result for this task run.
     *
     * @param mixed $result The value that is returned by the task; can be anything.
     *
     * @return void
     */
    public function setResult($result)
    {
        $this->result = $result;
    }

    /**
     * Invokes the task with the given arguments and processes the output.
     *
     * @return void.
     */
    public function execute()
    {
        $this->setReturnCode(0);
        try {
            $this->setResult(
                call_user_func_array($this->getTask(), $this->getArguments())
            );
        } catch (Exception $e) {
            $this->setError($e->getMessage());
            $this->setReturnCode($e->getCode());
        }
    }

    /**
     * Sets the task for this worker and validates whether it is callable.
     *
     * @param callback $task The task to execute when the execute method
     *     is invoked.
     *
     * @throws InvalidArgumentException if the given argument is not a callback.
     *
     * @see DocBlox_Parallel_Worker::__construct()
     * @see DocBlox_Parallel_Worker::execute()
     *
     * @return void
     */
    protected function setTask($task)
    {
        if (!is_callable($task)) {
            throw new InvalidArgumentException(
                'Worker task is not a callable object'
            );
        }

        $this->task = $task;
    }
}