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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
69cad905
Commit
69cad905
authored
May 08, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring Console commands
added method wrappers in console/Controller issue #33
parent
55cc8813
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
152 additions
and
3 deletions
+152
-3
Controller.php
framework/console/Controller.php
+152
-3
Console.php
framework/helpers/base/Console.php
+0
-0
No files found.
framework/console/Controller.php
View file @
69cad905
...
@@ -11,6 +11,7 @@ use Yii;
...
@@ -11,6 +11,7 @@ use Yii;
use
yii\base\Action
;
use
yii\base\Action
;
use
yii\base\InlineAction
;
use
yii\base\InlineAction
;
use
yii\base\InvalidRouteException
;
use
yii\base\InvalidRouteException
;
use
yii\helpers\Console
;
/**
/**
* Controller is the base class of console command classes.
* Controller is the base class of console command classes.
...
@@ -35,6 +36,12 @@ class Controller extends \yii\base\Controller
...
@@ -35,6 +36,12 @@ class Controller extends \yii\base\Controller
public
$interactive
=
true
;
public
$interactive
=
true
;
/**
/**
* @var bool whether to enable ANSI style in output. If not set it will be auto detected.
* Default is disabled on Windows systems and enabled on linux.
*/
public
$ansi
;
/**
* Runs an action with the specified action ID and parameters.
* Runs an action with the specified action ID and parameters.
* If the action ID is empty, the method will use [[defaultAction]].
* If the action ID is empty, the method will use [[defaultAction]].
* @param string $id the ID of the action to be executed.
* @param string $id the ID of the action to be executed.
...
@@ -115,6 +122,136 @@ class Controller extends \yii\base\Controller
...
@@ -115,6 +122,136 @@ class Controller extends \yii\base\Controller
}
}
/**
/**
* Formats a string with ANSI codes
*
* You may pass additional parameters using the constants defined in [[yii\helpers\base\Console]].
*
* Example:
* ~~~
* $this->formatString('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
* ~~~
*
* @param string $string the string to be formatted
* @return string
*/
public
function
formatString
(
$string
)
{
if
(
$this
->
ansi
===
true
||
$this
->
ansi
===
null
&&
Console
::
streamSupportsAnsiColors
(
STDOUT
))
{
$args
=
func_get_args
();
array_shift
(
$args
);
$string
=
Console
::
ansiFormat
(
$string
,
$args
);
}
return
$string
;
}
/**
* Prints a string to STDOUT
*
* You may optionally format the string with ANSI codes by
* passing additional parameters using the constants defined in [[yii\helpers\base\Console]].
*
* Example:
* ~~~
* $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
* ~~~
*
* @param string $string the string to print
* @return int|boolean Number of bytes printed or false on error
*/
public
function
stdout
(
$string
)
{
if
(
$this
->
ansi
===
true
||
$this
->
ansi
===
null
&&
Console
::
streamSupportsAnsiColors
(
STDOUT
))
{
$args
=
func_get_args
();
array_shift
(
$args
);
$string
=
Console
::
ansiFormat
(
$string
,
$args
);
}
return
Console
::
stdout
(
$string
);
}
/**
* Prints a string to STDERR
*
* You may optionally format the string with ANSI codes by
* passing additional parameters using the constants defined in [[yii\helpers\base\Console]].
*
* Example:
* ~~~
* $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
* ~~~
*
* @param string $string the string to print
* @return int|boolean Number of bytes printed or false on error
*/
public
function
stderr
(
$string
)
{
if
(
$this
->
ansi
===
true
||
$this
->
ansi
===
null
&&
Console
::
streamSupportsAnsiColors
(
STDERR
))
{
$args
=
func_get_args
();
array_shift
(
$args
);
$string
=
Console
::
ansiFormat
(
$string
,
$args
);
}
return
fwrite
(
STDERR
,
$string
);
}
/**
* Asks the user for input. Ends when the user types a carriage return (PHP_EOL). Optionally, It also provides a
* prompt.
*
* @param string $prompt the prompt (optional)
* @return string the user's input
*/
public
function
input
(
$prompt
=
null
)
{
if
(
isset
(
$prompt
))
{
static
::
stdout
(
$prompt
);
}
return
static
::
stdin
();
}
/**
* Prints text to STDOUT appended with a carriage return (PHP_EOL).
*
* @param string $text
* @param bool $raw
*
* @return mixed Number of bytes printed or bool false on error
*/
public
function
output
(
$text
=
null
)
{
return
static
::
stdout
(
$text
.
PHP_EOL
);
}
/**
* Prints text to STDERR appended with a carriage return (PHP_EOL).
*
* @param string $text
* @param bool $raw
*
* @return mixed Number of bytes printed or false on error
*/
public
function
error
(
$text
=
null
)
{
return
static
::
stderr
(
$text
.
PHP_EOL
);
}
/**
* Prompts the user for input and validates it
*
* @param string $text prompt string
* @param array $options the options to validate the input:
* - required: whether it is required or not
* - default: default value if no input is inserted by the user
* - pattern: regular expression pattern to validate user input
* - validator: a callable function to validate input. The function must accept two parameters:
* - $input: the user input to validate
* - $error: the error value passed by reference if validation failed.
* @return string the user input
*/
public
function
prompt
(
$text
,
$options
=
array
())
{
return
Console
::
prompt
(
$text
,
$options
);
}
/**
* Asks user to confirm by typing y or n.
* Asks user to confirm by typing y or n.
*
*
* @param string $message to echo out before waiting for user input
* @param string $message to echo out before waiting for user input
...
@@ -124,15 +261,27 @@ class Controller extends \yii\base\Controller
...
@@ -124,15 +261,27 @@ class Controller extends \yii\base\Controller
public
function
confirm
(
$message
,
$default
=
false
)
public
function
confirm
(
$message
,
$default
=
false
)
{
{
if
(
$this
->
interactive
)
{
if
(
$this
->
interactive
)
{
echo
$message
.
' (yes|no) ['
.
(
$default
?
'yes'
:
'no'
)
.
']:'
;
return
Console
::
confirm
(
$message
,
$default
);
$input
=
trim
(
fgets
(
STDIN
));
return
empty
(
$input
)
?
$default
:
!
strncasecmp
(
$input
,
'y'
,
1
);
}
else
{
}
else
{
return
true
;
return
true
;
}
}
}
}
/**
/**
* Gives the user an option to choose from. Giving '?' as an input will show
* a list of options to choose from and their explanations.
*
* @param string $prompt the prompt message
* @param array $options Key-value array of options to choose from
*
* @return string An option character the user chose
*/
public
static
function
select
(
$prompt
,
$options
=
array
())
{
return
Console
::
select
(
$prompt
,
$options
);
}
/**
* Returns the names of the global options for this command.
* Returns the names of the global options for this command.
* A global option requires the existence of a public member variable whose
* A global option requires the existence of a public member variable whose
* name is the option name.
* name is the option name.
...
...
framework/helpers/base/Console.php
View file @
69cad905
This diff is collapsed.
Click to expand it.
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