Task.php 5.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
 *  $Id: Task.php 2761 2007-10-07 23:42:29Z zYne $
 *
 * 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, see
 * <http://www.phpdoctrine.com>.
 */

/**
23 24 25
 * Doctrine_Task
 * 
 * Abstract class used for writing Doctrine Tasks
26 27
 *
 * @package     Doctrine
28
 * @subpackage  Task
29 30 31 32 33 34
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision: 2761 $
 * @author      Jonathan H. Wage <jwage@mac.com>
 */
35
abstract class Doctrine_Task
36
{
37 38
    public $dispatcher           =   null,
           $taskName             =   null,
39
           $description          =   null,
40
           $arguments            =   array(),
41 42
           $requiredArguments    =   array(),
           $optionalArguments    =   array();
43

44 45 46
    /**
     * __construct
     *
47 48 49
     * Since this is an abstract classes that extend this must follow a patter of Doctrine_Task_{TASK_NAME}
     * This is what determines the task name for executing it.
     *
50 51
     * @return void
     */
52
    public function __construct($dispatcher = null)
53
    {
54 55
        $this->dispatcher = $dispatcher;
        
56 57
        $this->taskName = str_replace('_', '-', Doctrine::tableize(str_replace('Doctrine_Task_', '', get_class($this))));
    }
58

59 60 61 62 63 64
    /**
     * notify
     *
     * @param string $notification 
     * @return void
     */
65
    public function notify($notification = null)
66
    {
67 68 69 70
        if (is_object($this->dispatcher) && method_exists($this->dispatcher, 'notify')) {
            $args = func_get_args();
            
            return call_user_func_array(array($this->dispatcher, 'notify'), $args);
71
        } else {
72
            return $notification;
73 74
        }
    }
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    /**
     * ask
     *
     * @return void
     */
    public function ask()
    {
        $args = func_get_args();
        
        call_user_func_array(array($this, 'notify'), $args);
        
        $answer = strtolower(trim(fgets(STDIN)));
        
        return $answer;
    }
91

92 93 94 95 96 97
    /**
     * execute
     *
     * Override with each task class
     *
     * @return void
98
     * @abstract
99
     */
100
    abstract function execute();
101

102 103 104 105 106
    /**
     * validate
     *
     * Validates that all required fields are present
     *
107
     * @return bool true
108 109
     */
    public function validate()
110 111 112 113
    {
        $requiredArguments = $this->getRequiredArguments();
        
        foreach ($requiredArguments as $arg) {
114
            if ( ! isset($this->arguments[$arg])) {
115
                return false;
116 117 118 119 120
            }
        }
        
        return true;
    }
121

122 123 124 125 126 127 128 129 130 131 132
    /**
     * addArgument
     *
     * @param string $name 
     * @param string $value 
     * @return void
     */
    public function addArgument($name, $value)
    {
        $this->arguments[$name] = $value;
    }
133

134 135 136 137 138
    /**
     * getArgument
     *
     * @param string $name 
     * @param string $default 
139
     * @return mixed
140
     */
141
    public function getArgument($name, $default = null)
142
    {
143
        if (isset($this->arguments[$name]) && $this->arguments[$name] !== null) {
144
            return $this->arguments[$name];
145
        } else {
146 147
            return $default;
        }
148
    }
149

150 151 152
    /**
     * getArguments
     *
153
     * @return array $arguments
154
     */
155
    public function getArguments()
156
    {
157
        return $this->arguments;
158
    }
159

160 161 162
    /**
     * setArguments
     *
163
     * @param array $args 
164 165
     * @return void
     */
166
    public function setArguments(array $args)
167 168 169
    {
        $this->arguments = $args;
    }
170

171 172 173
    /**
     * getTaskName
     *
174
     * @return string $taskName
175
     */
176 177 178 179
    public function getTaskName()
    {
        return $this->taskName;
    }
180

181 182 183
    /**
     * getDescription
     *
184
     * @return string $description
185
     */
186 187 188 189
    public function getDescription()
    {
        return $this->description;
    }
190

191 192 193
    /**
     * getRequiredArguments
     *
194
     * @return array $requiredArguments
195
     */
196 197
    public function getRequiredArguments()
    {
198
        return array_keys($this->requiredArguments);
199
    }
200

201 202 203
    /**
     * getOptionalArguments
     *
204
     * @return array $optionalArguments
205
     */
206
    public function getOptionalArguments()
207 208 209
    {
        return array_keys($this->optionalArguments);
    }
210

211 212 213
    /**
     * getRequiredArgumentsDescriptions
     *
214
     * @return array $requiredArgumentsDescriptions
215
     */
216 217 218 219
    public function getRequiredArgumentsDescriptions()
    {
        return $this->requiredArguments;
    }
220

221 222 223
    /**
     * getOptionalArgumentsDescriptions
     *
224
     * @return array $optionalArgumentsDescriptions
225
     */
226
    public function getOptionalArgumentsDescriptions()
227 228 229
    {
        return $this->optionalArguments;
    }
230
}