Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Task.php 2761 2007-10-07 23:42:29Z zYne $
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, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Task
24
 *
25
 * Abstract class used for writing Doctrine Tasks
26
 *
27
 * @package     Doctrine
28
 * @subpackage  Task
29
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
30
 * @link        www.doctrine-project.org
31
 * @since       1.0
32
 * @version     $Revision: 2761 $
33
 * @author      Jonathan H. Wage <jwage@mac.com>
34
 */
35
abstract class Doctrine_Task
36
{
37
    public $dispatcher           =   null,
38
           $taskName             =   null,  /*Treat as protected*/
39
           $description          =   null,
40
           $arguments            =   array(),
41
           $requiredArguments    =   array(),
42
           $optionalArguments    =   array();
43
 
44
    /**
45
     * __construct
46
     *
47
     * Since this is an abstract classes that extend this must follow a patter of Doctrine_Task_{TASK_NAME}
48
     * This is what determines the task name for executing it.
49
     *
50
     * @return void
51
     */
52
    public function __construct($dispatcher = null)
53
    {
54
        $this->dispatcher = $dispatcher;
55
 
56
        $taskName = $this->getTaskName();
57
 
58
        //Derive the task name only if it wasn't entered at design-time
59
        if (! strlen($taskName)) {
60
            $taskName = self::deriveTaskName(get_class($this));
61
        }
62
 
63
        /*
64
         * All task names must be passed through Doctrine_Task::setTaskName() to make sure they're valid.  We're most
65
         * interested in validating manually-entered task names, which are as good as arguments.
66
         */
67
        $this->setTaskName($taskName);
68
    }
69
 
70
    /**
71
     * Returns the name of the task the specified class _would_ implement
72
     *
73
     * N.B. This method does not check if the specified class is actually a Doctrine Task
74
     *
75
     * This is public so we can easily test its reactions to fully-qualified class names, without having to add
76
     * PHP 5.3-specific test code
77
     *
78
     * @param string $className
79
     * @return string|bool
80
     */
81
    public static function deriveTaskName($className)
82
    {
83
        $nameParts = explode('\\', $className);
84
 
85
        foreach ($nameParts as &$namePart) {
86
            $prefix = __CLASS__ . '_';
87
            $baseName = strpos($namePart, $prefix) === 0 ? substr($namePart, strlen($prefix)) : $namePart;
88
            $namePart = str_replace('_', '-', Doctrine_Inflector::tableize($baseName));
89
        }
90
 
91
        return implode('-', $nameParts);
92
    }
93
 
94
    /**
95
     * notify
96
     *
97
     * @param string $notification
98
     * @return void
99
     */
100
    public function notify($notification = null)
101
    {
102
        if (is_object($this->dispatcher) && method_exists($this->dispatcher, 'notify')) {
103
            $args = func_get_args();
104
 
105
            return call_user_func_array(array($this->dispatcher, 'notify'), $args);
106
        } else if ( $notification !== null ) {
107
            return $notification;
108
        } else {
109
            return false;
110
        }
111
    }
112
 
113
    /**
114
     * ask
115
     *
116
     * @return void
117
     */
118
    public function ask()
119
    {
120
        $args = func_get_args();
121
 
122
        call_user_func_array(array($this, 'notify'), $args);
123
 
124
        $answer = strtolower(trim(fgets(STDIN)));
125
 
126
        return $answer;
127
    }
128
 
129
    /**
130
     * execute
131
     *
132
     * Override with each task class
133
     *
134
     * @return void
135
     * @abstract
136
     */
137
    abstract function execute();
138
 
139
    /**
140
     * validate
141
     *
142
     * Validates that all required fields are present
143
     *
144
     * @return bool true
145
     */
146
    public function validate()
147
    {
148
        $requiredArguments = $this->getRequiredArguments();
149
 
150
        foreach ($requiredArguments as $arg) {
151
            if ( ! isset($this->arguments[$arg])) {
152
                return false;
153
            }
154
        }
155
 
156
        return true;
157
    }
158
 
159
    /**
160
     * addArgument
161
     *
162
     * @param string $name
163
     * @param string $value
164
     * @return void
165
     */
166
    public function addArgument($name, $value)
167
    {
168
        $this->arguments[$name] = $value;
169
    }
170
 
171
    /**
172
     * getArgument
173
     *
174
     * @param string $name
175
     * @param string $default
176
     * @return mixed
177
     */
178
    public function getArgument($name, $default = null)
179
    {
180
        if (isset($this->arguments[$name]) && $this->arguments[$name] !== null) {
181
            return $this->arguments[$name];
182
        } else {
183
            return $default;
184
        }
185
    }
186
 
187
    /**
188
     * getArguments
189
     *
190
     * @return array $arguments
191
     */
192
    public function getArguments()
193
    {
194
        return $this->arguments;
195
    }
196
 
197
    /**
198
     * setArguments
199
     *
200
     * @param array $args
201
     * @return void
202
     */
203
    public function setArguments(array $args)
204
    {
205
        $this->arguments = $args;
206
    }
207
 
208
    /**
209
     * Returns TRUE if the specified task name is valid, or FALSE otherwise
210
     *
211
     * @param string $taskName
212
     * @return bool
213
     */
214
    protected static function validateTaskName($taskName)
215
    {
216
        /*
217
         * This follows the _apparent_ naming convention.  The key thing is to prevent the use of characters that would
218
         * break a command string - we definitely can't allow spaces, for example.
219
         */
220
        return (bool) preg_match('/^[a-z0-9][a-z0-9\-]*$/', $taskName);
221
    }
222
 
223
    /**
224
     * Sets the name of the task, the name that's used to invoke it through a CLI
225
     *
226
     * @param string $taskName
227
     * @throws InvalidArgumentException If the task name is invalid
228
     */
229
    protected function setTaskName($taskName)
230
    {
231
        if (! self::validateTaskName($taskName)) {
232
            throw new InvalidArgumentException(
233
                sprintf('The task name "%s", in %s, is invalid', $taskName, get_class($this))
234
            );
235
        }
236
 
237
        $this->taskName = $taskName;
238
    }
239
 
240
    /**
241
     * getTaskName
242
     *
243
     * @return string $taskName
244
     */
245
    public function getTaskName()
246
    {
247
        return $this->taskName;
248
    }
249
 
250
    /**
251
     * getDescription
252
     *
253
     * @return string $description
254
     */
255
    public function getDescription()
256
    {
257
        return $this->description;
258
    }
259
 
260
    /**
261
     * getRequiredArguments
262
     *
263
     * @return array $requiredArguments
264
     */
265
    public function getRequiredArguments()
266
    {
267
        return array_keys($this->requiredArguments);
268
    }
269
 
270
    /**
271
     * getOptionalArguments
272
     *
273
     * @return array $optionalArguments
274
     */
275
    public function getOptionalArguments()
276
    {
277
        return array_keys($this->optionalArguments);
278
    }
279
 
280
    /**
281
     * getRequiredArgumentsDescriptions
282
     *
283
     * @return array $requiredArgumentsDescriptions
284
     */
285
    public function getRequiredArgumentsDescriptions()
286
    {
287
        return $this->requiredArguments;
288
    }
289
 
290
    /**
291
     * getOptionalArgumentsDescriptions
292
     *
293
     * @return array $optionalArgumentsDescriptions
294
     */
295
    public function getOptionalArgumentsDescriptions()
296
    {
297
        return $this->optionalArguments;
298
    }
299
}