Feature #4426 » 4426_v2.patch

Karsten Dambekalns, 2009-09-30 12:47

View differences:

Configuration/FLOW3.yaml Locally Modified (Based On LOCAL)
7 7
# Please refer to the default configuration file or the FLOW3 manual for #
8 8
# possible configuration options.                                        #
9 9
#                                                                        #
10

  
11
cli:
12
  commandSets:
13
    package:
14
      controller: F3\FLOW3\Package\Controller\ManagerController
15
      actions: [listAvailable, listActive, create, activate, deactivate, delete]
16
    cache:
17
      controller: F3\FLOW3\Cache\Controller\ManagerController
18
      actions: [flush]
19
    testing:
20
      controller: F3\Testing\Controller\CLIController
21
      actions: [run]
22
    kickstart:
23
      controller: F3\Kickstart\Controller\KickstartController
24
      actions: [generatePackage]
Packages/Framework/FLOW3/Classes/CLI/Command/Command.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Command;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * CLI command
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 * @scope prototype
31
 */
32
class Command implements \F3\FLOW3\CLI\Command\CommandInterface {
33

  
34
	const PATTERN_MATCH_COMMANDNAME = '/^[A-Za-z][A-Za-z0-9_-]+$/';
35

  
36
	/**
37
	 * command name
38
	 * @var string
39
	 */
40
	protected $commandName = '';
41

  
42
	/**
43
	 * sub command name
44
	 * @var string
45
	 */
46
	protected $subCommandName = NULL;
47

  
48
	/**
49
	 * description of this command
50
	 * @var string
51
	 */
52
	protected $description = '';
53

  
54
	/**
55
	 * registered arguments
56
	 * @var array
57
	 */
58
	protected $registeredArguments = array();
59

  
60
	/**
61
	 * callback to be invoked on execution of the current command
62
	 * @var \Closure
63
	 */
64
	protected $onExecuteCallback;
65

  
66
	/**
67
	 * Constructor
68
	 *
69
	 * @param string $commandName the command name
70
	 * @param string $subCommandName the sub command name (optional)
71
	 * @param string $description description of this command (optional)
72
	 * @author Bastian Waidelich <bastian@typo3.org>
73
	 */
74
	public function __construct($commandName, $subCommandName = NULL, $description = '') {
75
		if (preg_match(self::PATTERN_MATCH_COMMANDNAME, $commandName) !== 1) {
76
			throw new \F3\FLOW3\CLI\Exception\InvalidCommandName('"' . $commandName . '" is not a valid command name.', 1254178133);
77
		}
78
		if ($subCommandName !== NULL && preg_match(self::PATTERN_MATCH_COMMANDNAME, $subCommandName) !== 1) {
79
			throw new \F3\FLOW3\CLI\Exception\InvalidSubCommandName('"' . $subCommandName . '" is not a valid sub command name.', 1254178137);
80
		}
81
		$this->commandName = $commandName;
82
		$this->subCommandName = $subCommandName;
83
		$this->description = $description;
84
	}
85

  
86
	/**
87
	 * Returns the command name
88
	 *
89
	 * @return string
90
	 * @api
91
	 * @author Bastian Waidelich <bastian@typo3.org>
92
	 */
93
	public function getCommandName() {
94
		return $this->commandName;
95
	}
96

  
97
	/**
98
	 * Returns the sub command name
99
	 *
100
	 * @return string
101
	 * @api
102
	 * @author Bastian Waidelich <bastian@typo3.org>
103
	 */
104
	public function getSubCommandName() {
105
		return $this->subCommandName;
106
	}
107

  
108
	/**
109
	 * If sub command name is not set, only the command name is returned.
110
	 * Otherwise command name and sub command name are returned, separated by a blank
111
	 *
112
	 * @return string
113
	 * @api
114
	 * @author Bastian Waidelich <bastian@typo3.org>
115
	 */
116
	public function getSignature() {
117
		$commandSignature = $this->commandName;
118
		if ($this->subCommandName !== NULL) {
119
			$commandSignature .= ' ' . $this->subCommandName;
120
		}
121
		return $commandSignature;
122
	}
123

  
124
	/**
125
	 * Returns description for this command
126
	 *
127
	 * @return string
128
	 * @api
129
	 * @author Bastian Waidelich <bastian@typo3.org>
130
	 */
131
	public function getDescription() {
132
		return $this->description;
133
	}
134

  
135
	/**
136
	 * Adds a new argument to this command
137
	 *
138
	 * @param \F3\FLOW3\CLI\Command\CommandArgumentInterface $commandArgument command argument to be registered
139
	 * @return void
140
	 * @api
141
	 * @author Bastian Waidelich <bastian@typo3.org>
142
	 */
143
	public function registerArgument(\F3\FLOW3\CLI\Command\CommandArgumentInterface $argument) {
144
		$argumentName = $argument->getName();
145
		if (isset($this->registeredArguments[$argumentName])) {
146
			throw new \F3\FLOW3\CLI\Exception\InvalidCommandArgument('An argument with the name "' . $argumentName . '" is already registered for this command.', 1254177667);
147
		}
148
		if ($argument->isRequired()) {
149
			foreach($this->registeredArguments as $registeredArgument) {
150
				if (!$registeredArgument->isRequired()) {
151
					throw new \F3\FLOW3\CLI\Exception\InvalidCommandArgument('You can\'t register required command arguments after optional arguments.', 1254152841);
152
				}
153
			}
154
		}
155
		$this->registeredArguments[$argumentName] = $argument;
156
	}
157

  
158
	/**
159
	 * returns all registered arguments
160
	 *
161
	 * @return array
162
	 * @api
163
	 * @author Bastian Waidelich <bastian@typo3.org>
164
	 */
165
	public function getRegisteredArguments() {
166
		return $this->registeredArguments;
167
	}
168

  
169
	/**
170
	 * Registers a callback that will be envoked when the command is executed
171
	 *
172
	 * @param \Closure $callback the callback to be invoked on execution of this command
173
	 * @return void
174
	 * @api
175
	 */
176
	public function onExecute(\Closure $callback) {
177
		$this->onExecuteCallback = $callback;
178
	}
179

  
180
	/**
181
	 * Executes this command
182
	 *
183
	 * @param array $arguments simple arguments array (keyless) to be passed to the command e.g. array('foo', 'bar')
184
	 * @return void
185
	 * @api
186
	 */
187
	public function execute(array $arguments = array()) {
188
		$purgedArguments = array();
189
		$index = 0;
190
		foreach($this->registeredArguments as $registeredArgument) {
191
			if (isset($arguments[$index])) {
192
				$purgedArguments[$registeredArgument->getName()] = $arguments[$index];
193
			} elseif ($registeredArgument->isRequired()) {
194
				throw new \F3\FLOW3\CLI\Exception\MissingArgument('Missing required argument "' . $registeredArgument->getName() . '".', 1254177164);
195
			}
196
			$index ++;
197
		}
198
		if ($this->onExecuteCallback instanceof \Closure) {
199
			call_user_func_array($this->onExecuteCallback, array($this, $purgedArguments));
200
		}
201
	}
202

  
203
}
204

  
205
?>
Packages/Framework/FLOW3/Classes/CLI/Command/CommandArgument.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Command;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * CLI command argument
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 * @scope prototype
31
 */
32
class CommandArgument implements \F3\FLOW3\CLI\Command\CommandArgumentInterface {
33

  
34
	const PATTERN_MATCH_ARGUMENTNAME = '/^[A-Za-z][A-Za-z0-9_-]+$/';
35

  
36
	/**
37
	 * argument name
38
	 * @var string
39
	 */
40
	protected $name = '';
41

  
42
	/**
43
	 * argument description
44
	 * @var string
45
	 */
46
	protected $description = '';
47

  
48
	/**
49
	 * TRUE, if the argument is required
50
	 * @var boolean
51
	 */
52
	protected $required = FALSE;
53

  
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param string $name name of the argument
58
	 * @param string $description short description of the argument
59
	 * @param boolean $required TRUE if the argument should be required, otherwise FALSE
60
	 * @author Bastian Waidelich <bastian@typo3.org>
61
	 */
62
	public function __construct($name, $description = '', $required = FALSE) {
63
		if (preg_match(self::PATTERN_MATCH_ARGUMENTNAME, $name) !== 1) {
64
			throw new \F3\FLOW3\CLI\Exception\InvalidArgumentName('"' . $name . '" is not a valid argument name.', 1254180727);
65
		}
66
		$this->name = $name;
67
		$this->description = $description;
68
		$this->required = $required;
69
	}
70

  
71
	/**
72
	 * Returns the argument name
73
	 *
74
	 * @return string
75
	 * @api
76
	 * @author Bastian Waidelich <bastian@typo3.org>
77
	 */
78
	public function getName() {
79
		return $this->name;
80
	}
81

  
82
	/**
83
	 * Returns the description for this argument
84
	 *
85
	 * @return string
86
	 * @api
87
	 * @author Bastian Waidelich <bastian@typo3.org>
88
	 */
89
	public function getDescription() {
90
		return $this->description;
91
	}
92

  
93
	/**
94
	 * TRUE, if this argument is required
95
	 *
96
	 * @return string
97
	 * @api
98
	 * @author Bastian Waidelich <bastian@typo3.org>
99
	 */
100
	public function isRequired() {
101
		return $this->required;
102
	}
103

  
104
}
105

  
106
?>
Packages/Framework/FLOW3/Classes/CLI/Command/CommandArgumentInterface.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Command;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * Interface for CLI command arguments
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
interface CommandArgumentInterface {
32

  
33
	/**
34
	 * Returns the name of this argument
35
	 *
36
	 * @return string
37
	 */
38
	public function getName();
39

  
40
	/**
41
	 * Returns the description of this argument
42
	 *
43
	 * @return string
44
	 */
45
	public function getDescription();
46

  
47
	/**
48
	 * Returns FALSE if the argument is optional, otherwise FALSE
49
	 *
50
	 * @return boolean
51
	 */
52
	public function isRequired();
53

  
54
}
55

  
56
?>
Packages/Framework/FLOW3/Classes/CLI/Command/CommandInterface.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Command;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * Interface for CLI commands
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
interface CommandInterface {
32

  
33
	/**
34
	 * Adds a new argument to this command
35
	 *
36
	 * @param \F3\FLOW3\CLI\Command\CommandArgumentInterface $commandArgument command argument to be registered
37
	 * @return void
38
	 * @api
39
	 */
40
	public function registerArgument(\F3\FLOW3\CLI\Command\CommandArgumentInterface $commandArgument);
41

  
42
	/**
43
	 * Returns the Command name
44
	 *
45
	 * @return string
46
	 * @api
47
	 */
48
	public function getCommandName();
49

  
50
	/**
51
	 * Returns the Subcommand name
52
	 *
53
	 * @return string
54
	 * @api
55
	 */
56
	public function getSubCommandName();
57

  
58
	/**
59
	 * If sub command name is not set, only the command name is returned.
60
	 * Otherwise command name and sub command name are returned, separated by a blank
61
	 *
62
	 * @return string
63
	 * @api
64
	 */
65
	public function getSignature();
66

  
67
	/**
68
	 * Returns description for this command
69
	 *
70
	 * @return string
71
	 * @api
72
	 */
73
	public function getDescription();
74

  
75
	/**
76
	 * returns all registered arguments
77
	 *
78
	 * @return array
79
	 * @api
80
	 */
81
	public function getRegisteredArguments();
82

  
83
	/**
84
	 * Registers a callback that will be invoked when the command is executed
85
	 *
86
	 * @return void
87
	 * @api
88
	 */
89
	public function onExecute(\Closure $callback);
90

  
91
	/**
92
	 * Executes this command
93
	 *
94
	 * @param array $arguments simple arguments to be passed to the command e.g. array('foo', 'bar')
95
	 * @return void
96
	 * @api
97
	 */
98
	public function execute(array $arguments = array());
99
}
100

  
101
?>
Packages/Framework/FLOW3/Classes/CLI/Exception.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * A generic CLI Exception
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class Exception extends \F3\FLOW3\Exception {
32

  
33
}
34

  
35
?>
Packages/Framework/FLOW3/Classes/CLI/Exception/InvalidArgumentName.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Exception;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * An invalid argument name Exception
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class InvalidArgumentName extends \F3\FLOW3\CLI\Exception {
32

  
33
}
34

  
35
?>
Packages/Framework/FLOW3/Classes/CLI/Exception/InvalidCommandArgument.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Exception;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * An invalid command argument Exception
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class InvalidCommandArgument extends \F3\FLOW3\CLI\Exception {
32

  
33
}
34

  
35
?>
Packages/Framework/FLOW3/Classes/CLI/Exception/InvalidCommandName.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Exception;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * An invalid command name Exception
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class InvalidCommandName extends \F3\FLOW3\CLI\Exception {
32

  
33
}
34

  
35
?>
Packages/Framework/FLOW3/Classes/CLI/Exception/InvalidInput.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Exception;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * An invalid input Exception
27
 * Is thrown if no matching command could be found for a given input
28
 *
29
 * @version $Id$
30
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31
 */
32
class InvalidInput extends \F3\FLOW3\CLI\Exception {
33

  
34
}
35

  
36
?>
Packages/Framework/FLOW3/Classes/CLI/Exception/InvalidSubCommandName.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Exception;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * An invalid sub command name Exception
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class InvalidSubCommandName extends \F3\FLOW3\CLI\Exception {
32

  
33
}
34

  
35
?>
Packages/Framework/FLOW3/Classes/CLI/Exception/MissingArgument.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Exception;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * A missing command argument Exception
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class MissingArgument extends \F3\FLOW3\CLI\Exception {
32

  
33
}
34

  
35
?>
Packages/Framework/FLOW3/Classes/CLI/Host.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * Command Line Interface host
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 * @scope prototype
31
 */
32
class Host {
33

  
34
	/**
35
	 * registered commands
36
	 * @var array
37
	 */
38
	protected $registeredCommands = array();
39

  
40
	/**
41
	 * returns registered commands
42
	 *
43
	 * @return array
44
	 * @api
45
	 * @author Bastian Waidelich <bastian@typo3.org>
46
	 */
47
	public function getRegisteredCommands() {
48
		return $this->registeredCommands;
49
	}
50

  
51
	/**
52
	 * registers a new command
53
	 *
54
	 * @param \F3\FLOW3\CLI\CommandInterface $command
55
	 * @return void
56
	 * @api
57
	 * @author Bastian Waidelich <bastian@typo3.org>
58
	 */
59
	public function registerCommand(\F3\FLOW3\CLI\Command\CommandInterface $command) {
60
		foreach ($this->registeredCommands as $registeredCommand) {
61
			if ($registeredCommand->getSignature() === $command->getSignature()) {
62
				throw new \F3\FLOW3\CLI\Exception('A command with the signature "' . $command->getSignature() . '" has already been registered.', 1254151895);
63
			}
64
		}
65
		$this->registeredCommands[] = $command;
66
	}
67

  
68
	/**
69
	 * Outputs given text to the console
70
	 *
71
	 * @param string $text string to be printed
72
	 * @return void
73
	 * @api
74
	 * @author Bastian Waidelich <bastian@typo3.org>
75
	 */
76
	public function output($text) {
77
		print $text;
78
	}
79

  
80
	/**
81
	 * Outputs given text to the console and appends a line-break
82
	 *
83
	 * @param string $text string to be printed
84
	 * @return void
85
	 * @api
86
	 * @author Bastian Waidelich <bastian@typo3.org>
87
	 */
88
	public function outputLine($text = '') {
89
		$this->output($text . PHP_EOL);
90
	}
91

  
92
	/**
93
	 * Prints available commands and their arguments including description to the console
94
	 *
95
	 * @return void
96
	 * @api
97
	 * @author Bastian Waidelich <bastian@typo3.org>
98
	 * @see getHelp()
99
	 */
100
	public function outputHelp() {
101
		$this->output($this->getHelp());
102
	}
103

  
104
	/**
105
	 * Returns available commands and their arguments including description
106
	 * formatted to output to the console
107
	 *
108
	 * @return string formatted list of registered commands and command arguments
109
	 * @author Bastian Waidelich <bastian@typo3.org>
110
	 */
111
	protected function getHelp() {
112
		$output = '';
113
		$lastCommandName = NULL;
114
		foreach($this->registeredCommands as $command) {
115
			if ($lastCommandName !== NULL && $lastCommandName !== $command->getCommandName()) {
116
				$output .= PHP_EOL;
117
			}
118
			$output .= $command->getSignature();
119
			$arguments = $command->getRegisteredArguments();
120
			if (count($arguments) > 0) {
121
				$argumentSignatures = array();
122
				foreach($arguments as $argument) {
123
					$argumentSignature = '<' . $argument->getName() . '>';
124
					if (!$argument->isRequired()) {
125
						$argumentSignature = '[' . $argumentSignature . ']';
126
					}
127
					$argumentSignatures[] = $argumentSignature;
128
				}
129
				$output .= ' ' . implode(' ', $argumentSignatures);
130
			}
131
			$output .= PHP_EOL;
132
			$output .= '    ' . $command->getDescription() . PHP_EOL;
133
			foreach ($arguments as $argument) {
134
				$output .= '    - ' . $argument->getName();
135
				if (!$argument->isRequired()) {
136
					$output .= ' (optional)';
137
				}
138
				$output .= ': ' . $argument->getDescription();
139
				$output .= PHP_EOL;
140
			}
141
			$output .= PHP_EOL;
142
			$lastCommandName = $command->getCommandName();
143
		}
144
		return $output;
145
	}
146

  
147
	/**
148
	 * Parses given string and executes respective commands
149
	 *
150
	 * @return string $input command to be executed in the format "CommandName SubCommandName argument1 argument2"
151
	 * @return void
152
	 * @author Bastian Waidelich <bastian@typo3.org>
153
	 */
154
	public function parseInput($input) {
155
		$trimmedInput = trim($input);
156
		if ($trimmedInput === '') {
157
			throw new \F3\FLOW3\CLI\Exception\InvalidInput('Could not parse empty input.', 1254231716);
158
		}
159
		$words = explode(' ', $trimmedInput);
160
		if (count($words) > 1) {
161
			foreach($this->registeredCommands as $command) {
162
				if ($command->getSignature() === $words[0] . ' ' . $words[1]) {
163
					$command->execute(array_slice($words, 2));
164
					return;
165
				}
166
			}
167
		}
168
		foreach($this->registeredCommands as $command) {
169
			if ($command->getSignature() === $words[0]) {
170
				$command->execute(array_slice($words, 1));
171
				return;
172
			}
173
		}
174
		throw new \F3\FLOW3\CLI\Exception\InvalidInput('Could not parse empty input.', 1254231716);
175
	}
176

  
177
	/**
178
	 * Reads a string from the CLI.
179
	 *
180
	 * @return string the trimmed string entered by the user
181
	 * @api
182
	 * @author Bastian Waidelich <bastian@typo3.org>
183
	 */
184
	public function readLine() {
185
		return trim(fread(STDIN, 1024));
186
	}
187
}
188

  
189
?>
Packages/Framework/FLOW3/Classes/Reflection/MethodReflection.php Locally Modified (Based On LOCAL)
107 107
	}
108 108

  
109 109
	/**
110
	 * Returns the description for the method
111
	 *
112
	 * @return string The description which has been parsed
113
	 * @author Karsten Dambekalns <karsten@typo3.org>
114
	 */
115
	public function getDescription() {
116
		return $this->getDocCommentParser()->getDescription();
117
	}
118

  
119
	/**
110 120
	 * Returns an instance of the doc comment parser and
111 121
	 * runs the parse() method.
112 122
	 *
Packages/Framework/FLOW3/Classes/Reflection/Service.php Locally Modified (Based On LOCAL)
568 568
	}
569 569

  
570 570
	/**
571
	 * Returns the description of the given method
572
	 *
573
	 * @param string $className Name of the class containing the method
574
	 * @param string $methodName Name of the method to return description of
575
	 * @return string the methods description, without tags
576
	 * @author Bastian Waidelich <bastian@typo3.org>
577
	 * @see F3\FLOW3\Reflection\MethodReflection::getDescription()
578
	 * @api
579
	 */
580
	public function getMethodDescription($className, $methodName) {
581
		$method = new \F3\FLOW3\Reflection\MethodReflection($className, $methodName);
582
		return $method->getDescription();
583
	}
584

  
585
	/**
571 586
	 * Returns an array of parameters of the given method. Each entry contains
572 587
	 * additional information about the parameter position, type hint etc.
573 588
	 *
Packages/Framework/FLOW3/Scripts/FLOW3CLI.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3

  
4
/*                                                                        *
5
 * This script belongs to the FLOW3 framework.                            *
6
 *                                                                        *
7
 * It is free software; you can redistribute it and/or modify it under    *
8
 * the terms of the GNU Lesser General Public License as published by the *
9
 * Free Software Foundation, either version 3 of the License, or (at your *
10
 * option) any later version.                                             *
11
 *                                                                        *
12
 * This script is distributed in the hope that it will be useful, but     *
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
14
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
15
 * General Public License for more details.                               *
16
 *                                                                        *
17
 * You should have received a copy of the GNU Lesser General Public       *
18
 * License along with the script.                                         *
19
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
20
 *                                                                        *
21
 * The TYPO3 project - inspiring people to share!                         *
22
 *                                                                        */
23

  
24
/**
25
 * FLOW3 CLI
26
 *
27
 * @version $Id$
28
 * @author Bastian Waidelich <bastian@typo3.org>
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser Public License, version 3 or later
30
 */
31
require(__DIR__ . '/../Classes/Core/Bootstrap.php');
32
$flow3 = new \F3\FLOW3\Core\Bootstrap(getenv('FLOW3_CONTEXT'));
33
$flow3->initializeClassLoader();
34
$flow3->initializeConfiguration();
35
$flow3->initializeObjectManager();
36
$flow3->initializePackages();
37

  
38
$reflectionService = new \F3\FLOW3\Reflection\Service();
39
$FLOW3Package = new \F3\FLOW3\Package\Package('FLOW3', FLOW3_PATH_FLOW3);
40
$configurationManager = new \F3\FLOW3\Configuration\Manager(getenv('FLOW3_CONTEXT'));
41
$configurationManager->injectConfigurationSource(new \F3\FLOW3\Configuration\Source\YAMLSource());
42
$configurationManager->setPackages(array('FLOW3' => $FLOW3Package));
43
$settings = $configurationManager->getSettings('FLOW3');
44

  
45
$host = new \F3\FLOW3\CLI\Host();
46

  
47
	// register configured actions as command
48
foreach ($settings['cli']['commandSets'] as $commandSetName => $commandSetSettings) {
49
	$controllerClassName = $commandSetSettings['controller'];
50
	foreach($commandSetSettings['actions'] as $actionName) {
51
		$actionMethodName = $actionName . 'Action';
52
		$actionMethodDescription = $reflectionService->getMethodDescription($controllerClassName, $actionMethodName);
53
		$actionMethodParameters = $reflectionService->getMethodParameters($controllerClassName, $actionMethodName);
54
		$actionMethodTagsValues = $reflectionService->getMethodTagsValues($controllerClassName, $actionMethodName);
55
		$actionMethodParamAnnotations = array();
56
		if (isset($actionMethodTagsValues['param'])) {
57
			$actionMethodParamAnnotations = $actionMethodTagsValues['param'];
58
		}
59
		$command = new \F3\FLOW3\CLI\Command\Command($commandSetName, $actionName, $actionMethodDescription);
60
		$i = 0;
61
		foreach($actionMethodParameters as $actionMethodParameterName => $actionMethodParameter) {
62
			$actionMethodParameterDescription = '';
63
			if (isset($actionMethodParamAnnotations[$i])) {
64
				$explodedAnnotation = explode(' ', $actionMethodParamAnnotations[$i]);
65
				$actionMethodParameterDescription = implode(' ', array_slice($explodedAnnotation, 2));
66
			}
67
			$commandArgument = new \F3\FLOW3\CLI\Command\CommandArgument($actionMethodParameterName, $actionMethodParameterDescription, !$actionMethodParameter['optional']);
68
			$command->registerArgument($commandArgument);
69
			$i ++;
70
		}
71
		$command->onExecute(function(\F3\FLOW3\CLI\Command\Command $command, array $arguments) use($host, $controllerClassName, $actionMethodName, $actionMethodParameters) {
72
				$argumentString = '';
73
				if (count($arguments) > 0) {
74
					$argumentString = '"' . implode('", "', $arguments) . '"';
75
				}
76
				$host->outputLine('executing');
77
				$host->outputLine($controllerClassName . '::' . $actionMethodName . '(' . $argumentString . ')');
78
				$host->outputLine('execution of actions not yet implemented ;)');
79
				$host->outputLine();
80
			});
81
		$host->registerCommand($command);
82
	}
83
}
84

  
85
	// register "help" command
86
$helpCommand = new \F3\FLOW3\CLI\Command\Command('help', NULL, 'Display this help');
87
$helpCommand->onExecute(function() use ($host) {
88
		$host->outputHelp();
89
	});
90
$host->registerCommand($helpCommand);
91

  
92
	// register "quit" command
93
$quitCommand = new \F3\FLOW3\CLI\Command\Command('quit', NULL, 'Quit CLI mode');
94
$quitCommand->onExecute(function() use ($host) {
95
		$host->outputLine('exiting.');
96
		exit;
97
	});
98
$host->registerCommand($quitCommand);
99

  
100

  
101
	// start
102
$host->outputLine('FLOW3 Command Line Interface');
103
$host->outputLine('============================');
104
$host->outputHelp();
105
do {
106
	$host->output('Enter command: ');
107
	$input = $host->readLine();
108
	try {
109
		$host->parseInput($input);
110
	} catch(\F3\FLOW3\CLI\Exception\InvalidInput $e) {
111
		$host->outputLine('Error: Could not find a matching command.');
112
		$host->outputLine('Enter \'help\' to get a list of available commands.');
113
	} catch(\F3\FLOW3\CLI\Exception\MissingArgument $e) {
114
		$host->outputLine('Error: Not all required arguments were specified.');
115
		$host->outputLine('Enter \'help\' to get a list of available commands.');
116
	}
117
} while(TRUE);
118

  
119
?>
Packages/Framework/FLOW3/Tests/CLI/Command/CommandArgumentTest.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Command;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * Testcase for CLI CommandArgument class
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class CommandArgumentTest extends \F3\Testing\BaseTestCase {
32

  
33
	/**
34
	 * @test
35
	 * @author Bastian Waidelich <bastian@typo3.org>
36
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidArgumentName
37
	 */
38
	public function constructRejectsEmptyArgumentNames() {
39
		new \F3\FLOW3\CLI\Command\CommandArgument('');
40
	}
41

  
42
	/**
43
	 * @test
44
	 * @author Bastian Waidelich <bastian@typo3.org>
45
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidArgumentName
46
	 */
47
	public function constructRejectsInvalidArgumentNames() {
48
		new \F3\FLOW3\CLI\Command\CommandArgument('123');
49
	}
50

  
51
}
52
?>
Packages/Framework/FLOW3/Tests/CLI/Command/CommandTest.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI\Command;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * Testcase for CLI Command class
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class CommandTest extends \F3\Testing\BaseTestCase {
32

  
33
	/**
34
	 * @test
35
	 * @author Bastian Waidelich <bastian@typo3.org>
36
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidCommandName
37
	 */
38
	public function constructRejectsEmptyCommandNames() {
39
		new \F3\FLOW3\CLI\Command\Command('');
40
	}
41

  
42
	/**
43
	 * @test
44
	 * @author Bastian Waidelich <bastian@typo3.org>
45
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidCommandName
46
	 */
47
	public function constructRejectsInvalidCommandNames() {
48
		new \F3\FLOW3\CLI\Command\Command('123');
49
	}
50

  
51
	/**
52
	 * @test
53
	 * @author Bastian Waidelich <bastian@typo3.org>
54
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidSubCommandName
55
	 */
56
	public function constructRejectsEmptySubCommandNames() {
57
		new \F3\FLOW3\CLI\Command\Command('Foo', '');
58
	}
59

  
60
	/**
61
	 * @test
62
	 * @author Bastian Waidelich <bastian@typo3.org>
63
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidSubCommandName
64
	 */
65
	public function constructRejectsInvalidSubCommandNames() {
66
		new \F3\FLOW3\CLI\Command\Command('Foo', 'b*r');
67
	}
68

  
69
	/**
70
	 * @test
71
	 * @author Bastian Waidelich <bastian@typo3.org>
72
	 */
73
	public function registerArgumentAddsArgumentToArgumentsCollection() {
74
		$mockArgument1 = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
75
		$mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('Argument1'));
76
		$mockArgument2 = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
77
		$mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('Argument2'));
78

  
79
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
80
		$command->registerArgument($mockArgument1);
81
		$command->registerArgument($mockArgument2);
82

  
83
		$expectedResult = array('Argument1' => $mockArgument1, 'Argument2' => $mockArgument2);
84
		$actualResult = $command->getRegisteredArguments();
85
		$this->assertEquals($expectedResult, $actualResult);
86
	}
87

  
88
	/**
89
	 * @test
90
	 * @author Bastian Waidelich <bastian@typo3.org>
91
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidCommandArgument
92
	 */
93
	public function registerArgumentThrowsExceptionIfTryingToRegisterRequiredArgumentsAfterOptionalArguments() {
94
		$mockOptionalArgument = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
95
		$mockOptionalArgument->expects($this->atLeastOnce())->method('isRequired')->will($this->returnValue(FALSE));
96
		$mockOptionalArgument->expects($this->any())->method('getName')->will($this->returnValue('Argument1'));
97
		$mockRequiredArgument = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
98
		$mockRequiredArgument->expects($this->atLeastOnce())->method('isRequired')->will($this->returnValue(TRUE));
99
		$mockRequiredArgument->expects($this->any())->method('getName')->will($this->returnValue('Argument2'));
100

  
101
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
102
		$command->registerArgument($mockOptionalArgument);
103
		$command->registerArgument($mockRequiredArgument);
104
	}
105

  
106
	/**
107
	 * @test
108
	 * @author Bastian Waidelich <bastian@typo3.org>
109
	 * @expectedException \F3\FLOW3\CLI\Exception\InvalidCommandArgument
110
	 */
111
	public function registerArgumentThrowsExceptionIfTryingToRegisterMultipleArgumentsWithTheSameName() {
112
		$mockOptionalArgument = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
113
		$mockOptionalArgument->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('ArgumentName'));
114
		$mockRequiredArgument = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
115
		$mockRequiredArgument->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('ArgumentName'));
116

  
117
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
118
		$command->registerArgument($mockOptionalArgument);
119
		$command->registerArgument($mockRequiredArgument);
120
	}
121

  
122
	/**
123
	 * @test
124
	 * @author Bastian Waidelich <bastian@typo3.org>
125
	 */
126
	public function executePassesCommandToTheOnExecuteCallback() {
127
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
128

  
129
		$that = $this;
130
		$callback = function(\F3\FLOW3\CLI\Command\Command $passedCommand, array $passedArguments) use ($that, $command) {
131
			$that->assertSame($passedCommand, $command);
132
		};
133
		$command->onExecute($callback);
134
		$command->execute();
135
	}
136

  
137
	/**
138
	 * @test
139
	 * @author Bastian Waidelich <bastian@typo3.org>
140
	 */
141
	public function executePassesArgumentsToTheOnExecuteCallback() {
142
		$mockArgument1 = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
143
		$mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('Argument1'));
144
		$mockArgument2 = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
145
		$mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('Argument2'));
146

  
147
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
148
		$command->registerArgument($mockArgument1);
149
		$command->registerArgument($mockArgument2);
150

  
151
		$that = $this;
152
		$callback = function(\F3\FLOW3\CLI\Command\Command $passedCommand, array $passedArguments) use ($that, $command) {
153
			$that->assertEquals(array('Argument1' => 'foo', 'Argument2' => 'bar'), $passedArguments);
154
		};
155
		$command->onExecute($callback);
156
		$command->execute(array('foo', 'bar'));
157
	}
158

  
159
	/**
160
	 * @test
161
	 * @author Bastian Waidelich <bastian@typo3.org>
162
	 * @expectedException \F3\FLOW3\CLI\Exception\MissingArgument
163
	 */
164
	public function executeThrowsExceptionIfARequiredArgumentIsMissing() {
165
		$mockRequiredArgument1 = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
166
		$mockRequiredArgument1->expects($this->atLeastOnce())->method('isRequired')->will($this->returnValue(TRUE));
167
		$mockRequiredArgument1->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('Argument1'));
168
		$mockRequiredArgument2 = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
169
		$mockRequiredArgument2->expects($this->atLeastOnce())->method('isRequired')->will($this->returnValue(TRUE));
170
		$mockRequiredArgument2->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('Argument2'));
171

  
172
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
173
		$command->registerArgument($mockRequiredArgument1);
174
		$command->registerArgument($mockRequiredArgument2);
175

  
176
		$command->execute(array('foo'));
177
	}
178

  
179
	/**
180
	 * @test
181
	 * @author Bastian Waidelich <bastian@typo3.org>
182
	 */
183
	public function executeOnlyPassesRegisteredArgumentsToTheOnExecuteCallback() {
184
		$mockArgument = $this->getMock('F3\FLOW3\CLI\Command\CommandArgumentInterface');
185
		$mockArgument->expects($this->any())->method('getName')->will($this->returnValue('Argument1'));
186

  
187
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
188
		$command->registerArgument($mockArgument);
189

  
190
		$that = $this;
191
		$callback = function(\F3\FLOW3\CLI\Command\Command $passedCommand, array $passedArguments) use ($that, $command) {
192
			$that->assertEquals(array('Argument1' => 'foo'), $passedArguments);
193
		};
194
		$command->onExecute($callback);
195
		$command->execute(array('foo', 'these', 'will', 'be', 'stripped'));
196
	}
197

  
198
	/**
199
	 * @test
200
	 * @author Bastian Waidelich <bastian@typo3.org>
201
	 */
202
	public function getSignatureReturnsCommandNameIfSubCommandNameIsNotSet() {
203
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand');
204

  
205
		$expectedResult = 'SomeCommand';
206
		$actualResult = $command->getSignature();
207
		$this->assertEquals($expectedResult, $actualResult);
208
	}
209

  
210
	/**
211
	 * @test
212
	 * @author Bastian Waidelich <bastian@typo3.org>
213
	 */
214
	public function getSignatureReturnsCommandNameAndSubCommandNameIfSubCommandNameIsSet() {
215
		$command = new \F3\FLOW3\CLI\Command\Command('SomeCommand', 'SomeSubCommand');
216

  
217
		$expectedResult = 'SomeCommand SomeSubCommand';
218
		$actualResult = $command->getSignature();
219
		$this->assertEquals($expectedResult, $actualResult);
220
	}
221

  
222
}
223
?>
Packages/Framework/FLOW3/Tests/CLI/HostTest.php Locally New
1
<?php
2
declare(ENCODING = 'utf-8');
3
namespace F3\FLOW3\CLI;
4

  
5
/*                                                                        *
6
 * This script belongs to the FLOW3 framework.                            *
7
 *                                                                        *
8
 * It is free software; you can redistribute it and/or modify it under    *
9
 * the terms of the GNU Lesser General Public License as published by the *
10
 * Free Software Foundation, either version 3 of the License, or (at your *
11
 * option) any later version.                                             *
12
 *                                                                        *
13
 * This script is distributed in the hope that it will be useful, but     *
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
15
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
16
 * General Public License for more details.                               *
17
 *                                                                        *
18
 * You should have received a copy of the GNU Lesser General Public       *
19
 * License along with the script.                                         *
20
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
21
 *                                                                        *
22
 * The TYPO3 project - inspiring people to share!                         *
23
 *                                                                        */
24

  
25
/**
26
 * Testcase for CLI Host class
27
 *
28
 * @version $Id$
29
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30
 */
31
class HostTest extends \F3\Testing\BaseTestCase {
32

  
33
	/**
34
	 * @test
35
	 * @author Bastian Waidelich <bastian@typo3.org>
36
	 */
37
	public function registerCommandAddsCommandToCommandsCollection() {
38
		$mockCommand1 = $this->getMock('F3\FLOW3\CLI\Command\CommandInterface');
39
		$mockCommand1->expects($this->any())->method('getSignature')->will($this->returnValue('command1'));
40
		$mockCommand2 = $this->getMock('F3\FLOW3\CLI\Command\CommandInterface');
41
		$mockCommand2->expects($this->any())->method('getSignature')->will($this->returnValue('command2'));
42
		$host = new \F3\FLOW3\CLI\Host();
43
		$host->registerCommand($mockCommand1);
44
		$host->registerCommand($mockCommand2);
45
		$expectedResult = array($mockCommand1, $mockCommand2);
46
		$actualResult = $host->getRegisteredCommands();
47
		$this->assertEquals($expectedResult, $actualResult);
48
	}
49

  
50
	/**
51
	 * @test
52
	 * @author Bastian Waidelich <bastian@typo3.org>
53
	 * @expectedException \F3\FLOW3\CLI\Exception
54
	 */
55
	public function registerCommandThrowsExceptionIfACommandWithTheSameCommandNameWasAlreadyRegistered() {
56
		$mockCommand1 = $this->getMock('F3\FLOW3\CLI\Command\CommandInterface');
... This diff was truncated because it exceeds the maximum size that can be displayed.
(3-3/7)