1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Console GetoptPlus/Exception |
5
|
|
|
* |
6
|
|
|
* All rights reserved. |
7
|
|
|
* Redistribution and use in source and binary forms, with or without modification, |
8
|
|
|
* are permitted provided that the following conditions are met: |
9
|
|
|
* + Redistributions of source code must retain the above copyright notice, |
10
|
|
|
* this list of conditions and the following disclaimer. |
11
|
|
|
* + Redistributions in binary form must reproduce the above copyright notice, |
12
|
|
|
* this list of conditions and the following disclaimer in the documentation and/or |
13
|
|
|
* other materials provided with the distribution. |
14
|
|
|
* + The names of its contributors may not be used to endorse or promote |
15
|
|
|
* products derived from this software without specific prior written permission. |
16
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
20
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
21
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
23
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
24
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
25
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27
|
|
|
* |
28
|
|
|
* @category Console |
29
|
|
|
* @package Console_GetoptPlus |
30
|
|
|
* @author Michel Corne <[email protected]> |
31
|
|
|
* @copyright 2008 Michel Corne |
32
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
33
|
|
|
* @version SVN: $Id: Exception.php 47 2008-01-10 11:03:38Z mcorne $ |
34
|
|
|
* @link http://pear.php.net/package/Console_GetoptPlus |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
require_once 'PEAR/Exception.php'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Handling of error messages and exceptions. |
41
|
|
|
* |
42
|
|
|
* @category Console |
43
|
|
|
* @package Console_GetoptPlus |
44
|
|
|
* @author Michel Corne <[email protected]> |
45
|
|
|
* @copyright 2008 Michel Corne |
46
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
47
|
|
|
* @version Release: @package_version@ |
48
|
|
|
* @link http://pear.php.net/package/Console_GetoptPlus |
49
|
|
|
* @see PEAR_Exception |
50
|
|
|
*/ |
51
|
|
|
class Console_GetoptPlus_Exception extends PEAR_Exception |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* The error messages |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
* @access private |
58
|
|
|
*/ |
59
|
|
|
private $messages = array(// / |
60
|
|
|
'unknow' => array(1, 'Console_Getopt: unknown error ID (%s)'), |
61
|
|
|
// original Console_Getopt error messages |
62
|
|
|
'ambigous' => array(10, 'Console_Getopt: option --%s is ambiguous'), |
63
|
|
|
'mandatory' => array(11, 'Console_Getopt: option requires an argument --%s'), |
64
|
|
|
'noargument' => array(12, 'Console_Getopt: option --%s doesn\'t allow an argument'), |
65
|
|
|
'noargs' => array(13, 'Console_Getopt: Could not read cmd args (register_argc_argv=Off?)'), |
66
|
|
|
'unrecognized' => array(14, 'Console_Getopt: unrecognized option --%s'), |
67
|
|
|
// additional Console_GetoptPlus_Getopt error messages |
68
|
|
|
'duplicate' => array(20, 'Console_Getopt: duplicate option name definition --%s'), |
69
|
|
|
'invalid' => array(21, 'Console_Getopt: invalid long option definition %s'), |
70
|
|
|
'string' => array(22, 'Console_Getopt: short options definition must be a string'), |
71
|
|
|
'syntax' => array(23, 'Console_Getopt: short options definition syntax error %s'), |
72
|
|
|
// additional Console_GetoptPlus error messages |
73
|
|
|
'missing' => array(30, 'Console_GetoptPlus: unknown option name #%s'), |
74
|
|
|
'type' => array(31, 'Console_GetoptPlus: unknown option type %s'), |
75
|
|
|
'convert' => array(32, 'Console_GetoptPlus: wrong option name conversion %s'), |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Triggers the exception. |
80
|
|
|
* |
81
|
|
|
* @param mixed $exception the exception ID and optional message part, |
82
|
|
|
* e.g. "string" or array("invalid", '--foo') |
83
|
|
|
* @return void |
|
|
|
|
84
|
|
|
* @access public |
85
|
|
|
*/ |
86
|
|
|
public function __construct($exception) |
87
|
|
|
{ |
88
|
|
|
// extracts the exception ID and message parameters |
89
|
|
|
settype($exception, 'array'); |
90
|
|
|
$id = current($exception); |
91
|
|
|
// resets the exception ID if no corresponding message (programmatic error!) |
92
|
|
|
isset($this->messages[$id]) or $exception = array(null, $id) and $id = 'unknow'; |
|
|
|
|
93
|
|
|
// extracts the exception code and pattern |
94
|
|
|
list($code, $format) = $this->messages[$id]; |
95
|
|
|
$exception[0] = $format; |
96
|
|
|
// completes the message, throws the exception |
97
|
|
|
$message = call_user_func_array('sprintf', $exception); |
98
|
|
|
parent::__construct($message, $code); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
?> |
|
|
|
|
103
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.