Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
2d7f048b
Commit
2d7f048b
authored
Feb 01, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored usage error in console commands.
parent
7aa80d86
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
38 deletions
+54
-38
Application.php
framework/console/Application.php
+3
-9
BadUsageException.php
framework/console/BadUsageException.php
+33
-0
Controller.php
framework/console/Controller.php
+3
-10
HelpController.php
framework/console/controllers/HelpController.php
+15
-19
No files found.
framework/console/Application.php
View file @
2d7f048b
...
@@ -93,8 +93,7 @@ class Application extends \yii\base\Application
...
@@ -93,8 +93,7 @@ class Application extends \yii\base\Application
if
(
$request
->
getIsConsoleRequest
())
{
if
(
$request
->
getIsConsoleRequest
())
{
return
$this
->
runAction
(
$request
->
route
,
$request
->
params
);
return
$this
->
runAction
(
$request
->
route
,
$request
->
params
);
}
else
{
}
else
{
echo
"Error: this script must be run from the command line."
;
throw
new
BadUsageException
(
\Yii
::
t
(
'yii'
,
'this script must be run from the command line.'
));
return
1
;
}
}
}
}
...
@@ -106,14 +105,14 @@ class Application extends \yii\base\Application
...
@@ -106,14 +105,14 @@ class Application extends \yii\base\Application
* @param string $route the route that specifies the action.
* @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to the action
* @param array $params the parameters to be passed to the action
* @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
* @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
* @throws BadUsageException if the route is invalid
*/
*/
public
function
runAction
(
$route
,
$params
=
array
())
public
function
runAction
(
$route
,
$params
=
array
())
{
{
try
{
try
{
return
parent
::
runAction
(
$route
,
$params
);
return
parent
::
runAction
(
$route
,
$params
);
}
catch
(
InvalidRouteException
$e
)
{
}
catch
(
InvalidRouteException
$e
)
{
echo
"Error: unknown command
\"
$route
\"
.
\n
"
;
throw
new
BadUsageException
(
\Yii
::
t
(
'yii'
,
'Unknown command "{command}".'
,
array
(
'{command}'
=>
$route
)));
return
1
;
}
}
}
}
...
@@ -148,9 +147,4 @@ class Application extends \yii\base\Application
...
@@ -148,9 +147,4 @@ class Application extends \yii\base\Application
),
),
));
));
}
}
public
function
usageError
(
$message
)
{
}
}
}
framework/console/BadUsageException.php
0 → 100644
View file @
2d7f048b
<?php
/**
* BadUsageException class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\console
;
/**
* BadUsageException represents an exception caused by incorrect usage of the end user.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
BadUsageException
extends
\yii\base\Exception
{
/**
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL)
*/
public
$causedByUser
=
true
;
/**
* @return string the user-friendly name of this exception
*/
public
function
getName
()
{
return
\Yii
::
t
(
'yii'
,
'Bad Usage'
);
}
}
framework/console/Controller.php
View file @
2d7f048b
...
@@ -11,7 +11,6 @@ namespace yii\console;
...
@@ -11,7 +11,6 @@ namespace yii\console;
use
Yii
;
use
Yii
;
use
yii\base\Action
;
use
yii\base\Action
;
use
yii\base\InvalidRequestException
;
use
yii\base\InvalidRouteException
;
use
yii\base\InvalidRouteException
;
/**
/**
...
@@ -70,16 +69,16 @@ class Controller extends \yii\base\Controller
...
@@ -70,16 +69,16 @@ class Controller extends \yii\base\Controller
* @param Action $action the currently requested action
* @param Action $action the currently requested action
* @param array $missingParams the names of the missing parameters
* @param array $missingParams the names of the missing parameters
* @param array $unknownParams the unknown parameters (name=>value)
* @param array $unknownParams the unknown parameters (name=>value)
* @throws
InvalidRequest
Exception if there are missing or unknown parameters
* @throws
BadUsage
Exception if there are missing or unknown parameters
*/
*/
public
function
validateActionParams
(
$action
,
$missingParams
,
$unknownParams
)
public
function
validateActionParams
(
$action
,
$missingParams
,
$unknownParams
)
{
{
if
(
!
empty
(
$missingParams
))
{
if
(
!
empty
(
$missingParams
))
{
throw
new
InvalidRequest
Exception
(
Yii
::
t
(
'yii'
,
'Missing required options: {params}'
,
array
(
throw
new
BadUsage
Exception
(
Yii
::
t
(
'yii'
,
'Missing required options: {params}'
,
array
(
'{params}'
=>
implode
(
', '
,
$missingParams
),
'{params}'
=>
implode
(
', '
,
$missingParams
),
)));
)));
}
elseif
(
!
empty
(
$unknownParams
))
{
}
elseif
(
!
empty
(
$unknownParams
))
{
throw
new
InvalidRequest
Exception
(
Yii
::
t
(
'yii'
,
'Unknown options: {params}'
,
array
(
throw
new
BadUsage
Exception
(
Yii
::
t
(
'yii'
,
'Unknown options: {params}'
,
array
(
'{params}'
=>
implode
(
', '
,
$unknownParams
),
'{params}'
=>
implode
(
', '
,
$unknownParams
),
)));
)));
}
}
...
@@ -103,12 +102,6 @@ class Controller extends \yii\base\Controller
...
@@ -103,12 +102,6 @@ class Controller extends \yii\base\Controller
}
}
}
}
public
function
usageError
(
$message
)
{
echo
"
\n
Error:
$message
\n
"
;
Yii
::
$application
->
end
(
1
);
}
public
function
globalOptions
()
public
function
globalOptions
()
{
{
return
array
();
return
array
();
...
...
framework/console/controllers/HelpController.php
View file @
2d7f048b
...
@@ -9,7 +9,9 @@
...
@@ -9,7 +9,9 @@
namespace
yii\console\controllers
;
namespace
yii\console\controllers
;
use
Yii
;
use
yii\base\Application
;
use
yii\base\Application
;
use
yii\console\BadUsageException
;
use
yii\base\InlineAction
;
use
yii\base\InlineAction
;
use
yii\console\Controller
;
use
yii\console\Controller
;
use
yii\util\StringHelper
;
use
yii\util\StringHelper
;
...
@@ -47,27 +49,28 @@ class HelpController extends Controller
...
@@ -47,27 +49,28 @@ class HelpController extends Controller
* @param array $args additional anonymous command line arguments.
* @param array $args additional anonymous command line arguments.
* You may provide a command name to display its detailed information.
* You may provide a command name to display its detailed information.
* @return integer the exit status
* @return integer the exit status
* @throws BadUsageException if the command for help is unknown
*/
*/
public
function
actionIndex
(
$args
=
array
())
public
function
actionIndex
(
$args
=
array
())
{
{
if
(
empty
(
$args
))
{
if
(
empty
(
$args
))
{
$
status
=
$
this
->
getHelp
();
$this
->
getHelp
();
}
else
{
}
else
{
$result
=
\
Yii
::
$application
->
createController
(
$args
[
0
]);
$result
=
Yii
::
$application
->
createController
(
$args
[
0
]);
if
(
$result
===
false
)
{
if
(
$result
===
false
)
{
echo
"Error: no help for unknown command
\"
{
$args
[
0
]
}
\"
.
\n
"
;
throw
new
BadUsageException
(
Yii
::
t
(
'yii'
,
'No help for unknown command "{command}".'
,
array
(
return
1
;
'{command}'
=>
$args
[
0
],
)));
}
}
list
(
$controller
,
$actionID
)
=
$result
;
list
(
$controller
,
$actionID
)
=
$result
;
if
(
$actionID
===
''
)
{
if
(
$actionID
===
''
)
{
$
status
=
$
this
->
getControllerHelp
(
$controller
);
$this
->
getControllerHelp
(
$controller
);
}
else
{
}
else
{
$
status
=
$
this
->
getActionHelp
(
$controller
,
$actionID
);
$this
->
getActionHelp
(
$controller
,
$actionID
);
}
}
}
}
return
$status
;
}
}
/**
/**
...
@@ -76,7 +79,7 @@ class HelpController extends Controller
...
@@ -76,7 +79,7 @@ class HelpController extends Controller
*/
*/
public
function
getCommands
()
public
function
getCommands
()
{
{
$commands
=
$this
->
getModuleCommands
(
\
Yii
::
$application
);
$commands
=
$this
->
getModuleCommands
(
Yii
::
$application
);
sort
(
$commands
);
sort
(
$commands
);
return
array_unique
(
$commands
);
return
array_unique
(
$commands
);
}
}
...
@@ -91,7 +94,6 @@ class HelpController extends Controller
...
@@ -91,7 +94,6 @@ class HelpController extends Controller
$actions
=
array_keys
(
$controller
->
actions
());
$actions
=
array_keys
(
$controller
->
actions
());
$class
=
new
\ReflectionClass
(
$controller
);
$class
=
new
\ReflectionClass
(
$controller
);
foreach
(
$class
->
getMethods
()
as
$method
)
{
foreach
(
$class
->
getMethods
()
as
$method
)
{
/** @var $method \ReflectionMethod */
$name
=
$method
->
getName
();
$name
=
$method
->
getName
();
if
(
$method
->
isPublic
()
&&
!
$method
->
isStatic
()
&&
strpos
(
$name
,
'action'
)
===
0
&&
$name
!==
'actions'
)
{
if
(
$method
->
isPublic
()
&&
!
$method
->
isStatic
()
&&
strpos
(
$name
,
'action'
)
===
0
&&
$name
!==
'actions'
)
{
$actions
[]
=
StringHelper
::
camel2id
(
substr
(
$name
,
6
));
$actions
[]
=
StringHelper
::
camel2id
(
substr
(
$name
,
6
));
...
@@ -136,7 +138,6 @@ class HelpController extends Controller
...
@@ -136,7 +138,6 @@ class HelpController extends Controller
/**
/**
* Displays all available commands.
* Displays all available commands.
* @return integer the exit status
*/
*/
protected
function
getHelp
()
protected
function
getHelp
()
{
{
...
@@ -152,13 +153,11 @@ class HelpController extends Controller
...
@@ -152,13 +153,11 @@ class HelpController extends Controller
}
else
{
}
else
{
echo
"
\n
No commands are found.
\n
"
;
echo
"
\n
No commands are found.
\n
"
;
}
}
return
0
;
}
}
/**
/**
* Displays the overall information of the command.
* Displays the overall information of the command.
* @param Controller $controller the controller instance
* @param Controller $controller the controller instance
* @return integer the exit status
*/
*/
protected
function
getControllerHelp
(
$controller
)
protected
function
getControllerHelp
(
$controller
)
{
{
...
@@ -199,22 +198,21 @@ class HelpController extends Controller
...
@@ -199,22 +198,21 @@ class HelpController extends Controller
}
}
echo
"
\n
"
;
echo
"
\n
"
;
}
}
return
0
;
}
}
/**
/**
* Displays the detailed information of a command action.
* Displays the detailed information of a command action.
* @param Controller $controller the controller instance
* @param Controller $controller the controller instance
* @param string $actionID action ID
* @param string $actionID action ID
* @
return integer the exit status
* @
throws BadUsageException if the action does not exist
*/
*/
protected
function
getActionHelp
(
$controller
,
$actionID
)
protected
function
getActionHelp
(
$controller
,
$actionID
)
{
{
$action
=
$controller
->
createAction
(
$actionID
);
$action
=
$controller
->
createAction
(
$actionID
);
if
(
$action
===
null
)
{
if
(
$action
===
null
)
{
echo
'Error: no help for unknown sub-command "'
.
$controller
->
getUniqueId
()
.
"/
$actionID
\"
.
\n
"
;
throw
new
BadUsageException
(
Yii
::
t
(
'yii'
,
'No help for unknown sub-command "{command}".'
,
array
(
return
1
;
'{command}'
=>
$controller
->
getUniqueId
()
.
"/
$actionID
"
,
)));
}
}
if
(
$action
instanceof
InlineAction
)
{
if
(
$action
instanceof
InlineAction
)
{
$method
=
new
\ReflectionMethod
(
$controller
,
'action'
.
$action
->
id
);
$method
=
new
\ReflectionMethod
(
$controller
,
'action'
.
$action
->
id
);
...
@@ -245,8 +243,6 @@ class HelpController extends Controller
...
@@ -245,8 +243,6 @@ class HelpController extends Controller
}
}
echo
"
\n
"
;
echo
"
\n
"
;
}
}
return
0
;
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment