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
21440d7a
Commit
21440d7a
authored
Sep 04, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved common help parsing code into HelpParser. Action methods now have default implementation.
parent
d44f4248
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
59 deletions
+120
-59
Action.php
framework/console/Action.php
+9
-7
Controller.php
framework/console/Controller.php
+48
-45
HelpParser.php
framework/console/HelpParser.php
+53
-0
HelpController.php
framework/console/controllers/HelpController.php
+10
-7
No files found.
framework/console/Action.php
View file @
21440d7a
...
@@ -22,25 +22,27 @@ use yii\helpers\Console;
...
@@ -22,25 +22,27 @@ use yii\helpers\Console;
class
Action
extends
\yii\base\Action
class
Action
extends
\yii\base\Action
{
{
/**
/**
* Returns
a short description (one line) of information about the
action.
* Returns
one-line short summary describing this
action.
*
*
* The default implementation returns help information retrieved from the PHPDoc comments.
* You may override this method to return customized summary.
* The default implementation returns first line from the PHPDoc comment.
*
*
* @return string
* @return string
*/
*/
public
function
get
Description
()
public
function
get
HelpSummary
()
{
{
return
null
;
return
HelpParser
::
getSummary
(
new
\ReflectionClass
(
$this
))
;
}
}
/**
/**
* Returns help information for th
e
action.
* Returns help information for th
is
action.
*
*
* The default implementation returns help information retrieved from the PHPDoc comments.
* You may override this method to return customized help.
* The default implementation returns help information retrieved from the PHPDoc comment.
* @return string
* @return string
*/
*/
public
function
getHelp
()
public
function
getHelp
()
{
{
return
null
;
return
HelpParser
::
getDescriptionForConsole
(
new
\ReflectionClass
(
$this
))
;
}
}
}
}
framework/console/Controller.php
View file @
21440d7a
...
@@ -9,6 +9,7 @@ namespace yii\console;
...
@@ -9,6 +9,7 @@ namespace yii\console;
use
Yii
;
use
Yii
;
use
yii\base\Action
;
use
yii\base\Action
;
use
yii\base\InlineAction
;
use
yii\base\InvalidRouteException
;
use
yii\base\InvalidRouteException
;
use
yii\helpers\Console
;
use
yii\helpers\Console
;
...
@@ -56,7 +57,7 @@ class Controller extends \yii\base\Controller
...
@@ -56,7 +57,7 @@ class Controller extends \yii\base\Controller
*/
*/
public
function
isColorEnabled
(
$stream
=
\STDOUT
)
public
function
isColorEnabled
(
$stream
=
\STDOUT
)
{
{
return
$this
->
color
===
null
?
Console
::
streamSupportsAnsiColors
(
$stream
)
:
$this
->
color
;
return
$this
->
color
===
null
?
Console
::
streamSupportsAnsiColors
(
$stream
)
:
$this
->
color
;
}
}
/**
/**
...
@@ -99,7 +100,7 @@ class Controller extends \yii\base\Controller
...
@@ -99,7 +100,7 @@ class Controller extends \yii\base\Controller
*/
*/
public
function
bindActionParams
(
$action
,
$params
)
public
function
bindActionParams
(
$action
,
$params
)
{
{
if
(
$action
instanceof
\yii\base\
InlineAction
)
{
if
(
$action
instanceof
InlineAction
)
{
$method
=
new
\ReflectionMethod
(
$this
,
$action
->
actionMethod
);
$method
=
new
\ReflectionMethod
(
$this
,
$action
->
actionMethod
);
}
else
{
}
else
{
$method
=
new
\ReflectionMethod
(
$action
,
'run'
);
$method
=
new
\ReflectionMethod
(
$action
,
'run'
);
...
@@ -275,67 +276,69 @@ class Controller extends \yii\base\Controller
...
@@ -275,67 +276,69 @@ class Controller extends \yii\base\Controller
}
}
/**
/**
* Returns
a short description (one line) of information about this controller or it's action (if specified)
.
* Returns
one-line short summary describing this controller
.
*
*
* You may override this method to return customized description.
* You may override this method to return customized summary.
* The default implementation returns help information retrieved from the PHPDoc comments
* The default implementation returns first line from the PHPDoc comment.
* of the controller class.
*
*
* @param string $actionID action to get description for. null means overall controller description.
* @return string
* @return string
*/
*/
public
function
get
Description
(
$actionID
=
null
)
public
function
get
HelpSummary
(
)
{
{
$action
=
null
;
return
HelpParser
::
getSummary
(
new
\ReflectionClass
(
$this
));
if
(
$actionID
===
null
)
{
}
$class
=
new
\ReflectionClass
(
$this
);
/**
* Returns one-line short summary describing this controller action.
*
* You may override this method to return customized summary.
* The default implementation returns first line from the PHPDoc comment.
*
* @param string $actionID action to get summary for
* @return string
*/
public
function
getActionHelpSummary
(
$actionID
)
{
$action
=
$this
->
createAction
(
$actionID
);
if
(
$action
instanceof
InlineAction
)
{
$class
=
new
\ReflectionMethod
(
$this
,
$action
->
actionMethod
);
}
else
{
}
else
{
$action
=
$this
->
createAction
(
$actionID
);
$class
=
new
\ReflectionClass
(
$action
);
if
(
$action
instanceof
\yii\base\InlineAction
)
{
$class
=
new
\ReflectionMethod
(
$this
,
$action
->
actionMethod
);
}
else
{
$class
=
new
\ReflectionClass
(
$action
);
}
}
}
$docLines
=
preg_split
(
'~\R~'
,
$class
->
getDocComment
());
return
HelpParser
::
getSummary
(
$class
);
if
(
isset
(
$docLines
[
1
]))
{
return
trim
(
$docLines
[
1
],
' *'
);
}
return
''
;
}
}
/**
/**
* Returns help information for this controller
or it's action (if specified)
.
* Returns help information for this controller.
*
*
* You may override this method to return customized help.
* You may override this method to return customized help.
* The default implementation returns help information retrieved from the PHPDoc comments
* The default implementation returns help information retrieved from the PHPDoc comment.
* of the controller class.
* @param string $actionID action to get help for. null means overall controller help.
* @return string
* @return string
*/
*/
public
function
getHelp
(
$actionID
=
null
)
public
function
getHelp
()
{
{
$action
=
null
;
return
HelpParser
::
getFull
(
new
\ReflectionClass
(
$this
));
if
(
$actionID
===
null
)
{
}
$class
=
new
\ReflectionClass
(
$this
);
}
else
{
$action
=
$this
->
createAction
(
$actionID
);
if
(
$action
instanceof
\yii\base\InlineAction
)
{
/**
$class
=
new
\ReflectionMethod
(
$this
,
$action
->
actionMethod
);
* Returns help information for this controller action.
}
else
{
*
$class
=
new
\ReflectionClass
(
$action
);
* You may override this method to return customized help.
}
* The default implementation returns help information retrieved from the PHPDoc comment.
}
* @param string $actionID action to get help for
* @return string
*/
public
function
getActionHelp
(
$actionID
)
{
$action
=
$this
->
createAction
(
$actionID
);
$comment
=
strtr
(
trim
(
preg_replace
(
'/^\s*\**( |\t)?/m'
,
''
,
trim
(
$class
->
getDocComment
(),
'/'
))),
"
\r
"
,
''
);
if
(
$action
instanceof
InlineAction
)
{
if
(
preg_match
(
'/^\s*@\w+/m'
,
$comment
,
$matches
,
PREG_OFFSET_CAPTURE
))
{
$class
=
new
\ReflectionMethod
(
$this
,
$action
->
actionMethod
);
$comment
=
trim
(
substr
(
$comment
,
0
,
$matches
[
0
][
1
]));
}
else
{
}
$class
=
new
\ReflectionClass
(
$action
);
if
(
$comment
!==
''
)
{
return
rtrim
(
Console
::
renderColoredString
(
Console
::
markdownToAnsi
(
$comment
)));
}
}
return
''
;
return
HelpParser
::
getFull
(
$class
);
}
}
}
}
framework/console/HelpParser.php
0 → 100644
View file @
21440d7a
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\console
;
use
yii\helpers\Console
;
/**
* HelpParser contains methods used to get help information from phpDoc.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class
HelpParser
{
/**
* Returns the first line of docblock.
*
* @param \Reflector $reflector
* @return string
*/
public
static
function
getSummary
(
\Reflector
$reflector
)
{
$docLines
=
preg_split
(
'~\R~'
,
$reflector
->
getDocComment
());
if
(
isset
(
$docLines
[
1
]))
{
return
trim
(
$docLines
[
1
],
' *'
);
}
return
''
;
}
/**
* Returns full description from the docblock.
*
* @param \Reflector $reflector
* @return string
*/
public
static
function
getFull
(
\Reflector
$reflector
)
{
$comment
=
strtr
(
trim
(
preg_replace
(
'/^\s*\**( |\t)?/m'
,
''
,
trim
(
$reflector
->
getDocComment
(),
'/'
))),
"
\r
"
,
''
);
if
(
preg_match
(
'/^\s*@\w+/m'
,
$comment
,
$matches
,
PREG_OFFSET_CAPTURE
))
{
$comment
=
trim
(
substr
(
$comment
,
0
,
$matches
[
0
][
1
]));
}
if
(
$comment
!==
''
)
{
return
rtrim
(
Console
::
renderColoredString
(
Console
::
markdownToAnsi
(
$comment
)));
}
return
''
;
}
}
\ No newline at end of file
framework/console/controllers/HelpController.php
View file @
21440d7a
...
@@ -9,6 +9,7 @@ namespace yii\console\controllers;
...
@@ -9,6 +9,7 @@ namespace yii\console\controllers;
use
Yii
;
use
Yii
;
use
yii\base\Application
;
use
yii\base\Application
;
use
yii\base\InlineAction
;
use
yii\console\Action
;
use
yii\console\Action
;
use
yii\console\Controller
;
use
yii\console\Controller
;
use
yii\console\Exception
;
use
yii\console\Exception
;
...
@@ -61,7 +62,7 @@ class HelpController extends Controller
...
@@ -61,7 +62,7 @@ class HelpController extends Controller
$actions
=
$this
->
getActions
(
$controller
);
$actions
=
$this
->
getActions
(
$controller
);
if
(
$actionID
!==
''
||
count
(
$actions
)
===
1
&&
$actions
[
0
]
===
$controller
->
defaultAction
)
{
if
(
$actionID
!==
''
||
count
(
$actions
)
===
1
&&
$actions
[
0
]
===
$controller
->
defaultAction
)
{
$this
->
getActionHelp
(
$controller
,
$actionID
);
$this
->
get
Controller
ActionHelp
(
$controller
,
$actionID
);
}
else
{
}
else
{
$this
->
getControllerHelp
(
$controller
);
$this
->
getControllerHelp
(
$controller
);
}
}
...
@@ -94,7 +95,8 @@ class HelpController extends Controller
...
@@ -94,7 +95,8 @@ class HelpController extends Controller
$result
=
Yii
::
$app
->
createController
(
$command
);
$result
=
Yii
::
$app
->
createController
(
$command
);
if
(
$result
!==
false
)
{
if
(
$result
!==
false
)
{
list
(
$controller
,
$actionID
)
=
$result
;
list
(
$controller
,
$actionID
)
=
$result
;
$description
=
$controller
->
getDescription
();
/** @var Controller $controller */
$description
=
$controller
->
getHelpSummary
();
}
}
$descriptions
[
$command
]
=
$description
;
$descriptions
[
$command
]
=
$description
;
...
@@ -253,10 +255,10 @@ class HelpController extends Controller
...
@@ -253,10 +255,10 @@ class HelpController extends Controller
$action
=
$controller
->
createAction
(
$actionID
);
$action
=
$controller
->
createAction
(
$actionID
);
if
(
$action
instanceof
Action
)
{
if
(
$action
instanceof
Action
)
{
$description
=
$action
->
get
Description
();
$description
=
$action
->
get
HelpSummary
();
}
}
if
(
$description
===
null
)
{
if
(
$description
===
null
)
{
$description
=
$controller
->
get
Description
(
$actionID
);
$description
=
$controller
->
get
ActionHelpSummary
(
$actionID
);
}
}
return
$description
;
return
$description
;
}
}
...
@@ -267,7 +269,7 @@ class HelpController extends Controller
...
@@ -267,7 +269,7 @@ class HelpController extends Controller
* @param string $actionID action ID
* @param string $actionID action ID
* @throws Exception if the action does not exist
* @throws Exception if the action does not exist
*/
*/
protected
function
getActionHelp
(
$controller
,
$actionID
)
protected
function
get
Controller
ActionHelp
(
$controller
,
$actionID
)
{
{
$action
=
$controller
->
createAction
(
$actionID
);
$action
=
$controller
->
createAction
(
$actionID
);
if
(
$action
===
null
)
{
if
(
$action
===
null
)
{
...
@@ -276,9 +278,10 @@ class HelpController extends Controller
...
@@ -276,9 +278,10 @@ class HelpController extends Controller
]));
]));
}
}
$description
=
null
;
$description
=
null
;
if
(
$action
instanceof
\yii\base\
InlineAction
)
{
if
(
$action
instanceof
InlineAction
)
{
$method
=
new
\ReflectionMethod
(
$controller
,
$action
->
actionMethod
);
$method
=
new
\ReflectionMethod
(
$controller
,
$action
->
actionMethod
);
}
else
{
}
else
{
/** @var Action $action */
$description
=
$action
->
getHelp
();
$description
=
$action
->
getHelp
();
$method
=
new
\ReflectionMethod
(
$action
,
'run'
);
$method
=
new
\ReflectionMethod
(
$action
,
'run'
);
}
}
...
@@ -287,7 +290,7 @@ class HelpController extends Controller
...
@@ -287,7 +290,7 @@ class HelpController extends Controller
$options
=
$this
->
getOptionHelps
(
$controller
,
$actionID
);
$options
=
$this
->
getOptionHelps
(
$controller
,
$actionID
);
if
(
$description
===
null
)
{
if
(
$description
===
null
)
{
$description
=
$controller
->
getHelp
(
$actionID
);
$description
=
$controller
->
get
Action
Help
(
$actionID
);
}
}
if
(
$description
!==
''
)
{
if
(
$description
!==
''
)
{
$this
->
stdout
(
"
\n
DESCRIPTION
\n
"
,
Console
::
BOLD
);
$this
->
stdout
(
"
\n
DESCRIPTION
\n
"
,
Console
::
BOLD
);
...
...
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