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
bd70ac84
Commit
bd70ac84
authored
Feb 07, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #2054: Added support for using custom application configuration with the…
Fixes #2054: Added support for using custom application configuration with the console command runner
parent
afda91e0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
6 deletions
+61
-6
console.md
docs/guide/console.md
+16
-5
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
Application.php
framework/console/Application.php
+41
-0
Request.php
framework/console/Request.php
+3
-1
No files found.
docs/guide/console.md
View file @
bd70ac84
...
...
@@ -58,16 +58,27 @@ exit($exitCode);
```
This script is a part of your application so you're free to adjust it. The
`YII_DEBUG`
constant can be set
`false`
if you do
not want to see stacktrace on error and want to improve overall performance. In both basic and advanced application
not want to see stack
trace on error and want to improve overall performance. In both basic and advanced application
templates it is enabled to provide more developer-friendly environment.
Configuration
-------------
As can be seen in the code above, console application uses its own config files named
`console.php`
so you need to
configure database connection and the rest of the components you're going to use there in that file. If web and console
application configs have a lot in common it's a good idea to move matching parts into their own config files as it was
done in advanced application template.
As can be seen in the code above, console application uses its own config files named
`console.php`
. In this file,
you should specify how to configure various application components and properties.
If your Web application and the console application share a lot of configurations, you may consider moving the common
part into a separate file, and include this file in both of the application configurations, just as what is done
in the "advanced" application template.
Sometimes, you may want to run a console command using an application configuration that is different from the one
specified in the entry script. For example, you may want to use the
`yii migrate`
command to upgrade your
test databases which are configured in each individual test suite. To do so, simply specify the custom application configuration
file via the
`appconfig`
option, like the following,
```
yii <route> --appconfig=path/to/config.php ...
```
Creating your own console commands
...
...
framework/CHANGELOG.md
View file @
bd70ac84
...
...
@@ -93,6 +93,7 @@ Yii Framework 2 Change Log
-
Enh #2008:
`yii message/extract`
is now able to save translation strings to database (kate-kate, samdark)
-
Enh #2043: Added support for custom request body parsers (danschmidt5189, cebe)
-
Enh #2051: Do not save null data into database when using RBAC (qiangxue)
-
Enh #2054: Added support for using custom application configuration with the console command runner (qiangxue)
-
Enh #2079:
-
i18n now falls back to
`en`
from
`en-US`
if message translation isn't found (samdark)
-
View now falls back to
`en`
from
`en-US`
if file not found (samdark)
...
...
framework/console/Application.php
View file @
bd70ac84
...
...
@@ -54,6 +54,11 @@ use yii\base\InvalidRouteException;
class
Application
extends
\yii\base\Application
{
/**
* The option name for specifying the application configuration file path.
*/
const
OPTION_APPCONFIG
=
'appconfig'
;
/**
* @var string the default route of this application. Defaults to 'help',
* meaning the `help` command.
*/
...
...
@@ -68,6 +73,42 @@ class Application extends \yii\base\Application
*/
public
$controller
;
/**
* @inheritdoc
*/
public
function
__construct
(
$config
=
[])
{
$config
=
$this
->
loadConfig
(
$config
);
parent
::
__construct
(
$config
);
}
/**
* Loads the configuration.
* This method will check if the command line option [[OPTION_APPCONFIG]] is specified.
* If so, the corresponding file will be loaded as the application configuration.
* Otherwise, the configuration provided as the parameter will be returned back.
* @param array $config the configuration provided in the constructor.
* @return array the actual configuration to be used by the application.
*/
protected
function
loadConfig
(
$config
)
{
if
(
!
empty
(
$_SERVER
[
'argv'
]))
{
$option
=
'--'
.
self
::
OPTION_APPCONFIG
.
'='
;
foreach
(
$_SERVER
[
'argv'
]
as
$param
)
{
if
(
strpos
(
$param
,
$option
)
!==
false
)
{
$path
=
substr
(
$param
,
strlen
(
$option
));
if
(
!
empty
(
$path
)
&&
is_file
(
$path
))
{
return
require
(
$path
);
}
else
{
die
(
"The configuration file does not exist:
$path
\n
"
);
}
}
}
}
return
$config
;
}
/**
* Initialize the application.
*/
...
...
framework/console/Request.php
View file @
bd70ac84
...
...
@@ -66,7 +66,9 @@ class Request extends \yii\base\Request
foreach
(
$rawParams
as
$param
)
{
if
(
preg_match
(
'/^--(\w+)(=(.*))?$/'
,
$param
,
$matches
))
{
$name
=
$matches
[
1
];
$params
[
$name
]
=
isset
(
$matches
[
3
])
?
$matches
[
3
]
:
true
;
if
(
$name
!==
Application
::
OPTION_APPCONFIG
)
{
$params
[
$name
]
=
isset
(
$matches
[
3
])
?
$matches
[
3
]
:
true
;
}
}
else
{
$params
[]
=
$param
;
}
...
...
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