New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Console commands run #1764
Comments
Isn't |
@qiangxue yep, but i would like to have handful method, because for those developers that are not into core, it could be a little be difficult to guess for that. So useful method that can be a wrapper is just the case. |
The |
yes. And currently how would i call my migration command for example |
|
hm, so actions parameters should only be after controller properties? for example
and not
Anyway |
No, you can put action parameters before options or mix them. I still don't understand the difference between |
Also note that if you want to create new application instance, the old one will be gone because of the singleton. -- update: actually, the one app is still there, but the new one will take |
for end user it is simpler. Also one
yes i know, very bad practice to be true, dont know why it was accepted. Anyway we can save |
How will the new application instance be created? |
i was thinking about new Application(require(Yii::getAlias('@app/console/config.php'))) or just require |
I think we should still use We can introduce an extra parameter
|
Note that this method is not just used by console controllers, it can also be used by web controllers. |
yes, thats why we should add
same as said above. But this could be used in HMVC for example in Yii1 it was like
not sure if it is needed since it is singleton, maybe then just remove |
...I still don't get why you insist in adding
We still need the singleton. Otherwise how will you access application components?
This is not good. Internal code should not need to know how the configuration is obtained. The configuration could be coming from some cache. That's why I suggested saving the configuration within the application so that it could create a new one using it. The drawback is that it requires extra memory. |
only because for console it makes more sense. also call is simle wrapper against Controller:run(). Or you sugges for end user to do this
like in Yii1, dont see any changes here. Yii::setApplication($app) vs Yii::$app. Our benefit is to eliminate strange unexpected behavior as setting in constructor Yii::$app.
true, memory question i think can be solved by application property |
No, it's as simple as |
But we dont need |
Why? This is just a parameter controlling how an action should be run. Supposedly, this is also useful for implementing HMVC. That is, it may be needed by web controllers. |
Because if you want full |
True, there's more work to do to fully support HMVC, which is not in our near-term plan. |
Yes, thats why i think to not confuse users we can add simple wrapper |
My point is that the name |
nope, if we will not add HMVC now. Then
can back-up it, i guess. |
From my point of view this discussion is pointless.
I can see only one appropriate solution for this: extract the migration functionality into the separated class (maybe application component), while the “MigrateCommand” should be only an interface wrapper around it. This will allow developer to reuse the migration mechanism in any place and any interface he wants.
I am afraid it is unreachable with the current framework architecture: just see “Pagination::getPage()” for an instance. If some listing should be a part of “slave” controller, how we should handle such things? Introducing a new method “call” does not make any sense. There is “run()” method already, it usage is not so complex as you think, while it give developer more clear understanding of the process. |
not sure about it, and once again there was discussion also about |
Noone actually asked :) |
Yes, nobody actually knew about it i think. |
My opinion: call one controller from another is a bad practice of code reusage. It breaks the meaning of MVC. If you wish some piece of code to be used in 2 different controllers, then extract it into separated class: component, helper, model, whatever.
I don’t get it. We have “request” and “response” components already. Their usage ensures the particular controller returns its result as “Response” instance, while request parameters are fetched from “Request” instance. Console controller has a property “interactive”, which determines if it would try to fetch user input or not. |
yes, i wish
Environment in which new command will be executed. |
@klimov-paul i understand, but once again your use-case is specific, you only described drawbacks and no features. I said about some features, dont know why i am fighting to be true) In L4 you can easily call another command from command, if Yii architecture is not good enough for that, then what is the question? :) |
What is wrong with |
name is wrong, if we are not talking about isolation. |
Why? |
Method Module::runAction() accept route and action params, while returning Response instance. It should be just what you need. |
because end user is calling command and not running action. it is more intuitive to do |
nope, @qiangxue answered above about |
http://www.google.com/trends/explore#q=run%20command%2C%20call%20command&cmpt=q
|
@adamaltman its not only about the name :D |
$oldApp = \Yii::$app;
new \yii\console\Application([
'id' => 'Command runner',
'basePath' => '@app',
'components' => [
'db' => $oldApp->db,
],
);
\Yii::$app->runAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
\Yii:$app = $oldApp; @qiangxue can it be done simpler? |
Do you mean running a console command from a Web application? |
From a web application or tests or another console application or whatever. |
What about using |
Many hosts won't allow you using it. |
@qiangxue so should we introduce shortcut method for it? |
i am fine with |
Hi guys! I tried as suggested Samdark, but it's not worked: class ConsoleAppHelper
{
public static function runAction( $route, $params = [ ], $controllerNamespace = null )
{
$oldApp = \Yii::$app;
// fcgi doesn't have STDIN and STDOUT defined by default
defined( 'STDIN' ) or define( 'STDIN', fopen( 'php://stdin', 'r' ) );
defined( 'STDOUT' ) or define( 'STDOUT', fopen( 'php://stdout', 'w' ) );
/** @noinspection PhpIncludeInspection */
$config = require( \Yii::getAlias( '@app/config/console.php' ) );
$consoleApp = new ConsoleApplication( $config );
if (!is_null( $controllerNamespace )) {
$consoleApp->controllerNamespace = $controllerNamespace;
}
// use current connection to DB
\Yii::$app->db = $oldApp->db;
$exitCode = \Yii::$app->runAction( $route, array_merge( $params, [ 'interactive' => false ] ) );
\Yii::$app = $oldApp;
return $exitCode;
}
} I didn't see results (it should be generate file in runtime path) of run action. |
I've fixed some errors in the code above, and it work now, but I can't specify parameters to the action. /**
* Class ConsoleAppHelper
* @package app\components
*/
class ConsoleAppHelper
{
public static function runAction( $route, $params = [ ], $controllerNamespace = null )
{
$oldApp = \Yii::$app;
// fcgi doesn't have STDIN and STDOUT defined by default
defined( 'STDIN' ) or define( 'STDIN', fopen( 'php://stdin', 'r' ) );
defined( 'STDOUT' ) or define( 'STDOUT', fopen( 'php://stdout', 'w' ) );
/** @noinspection PhpIncludeInspection */
$config = require( \Yii::getAlias( '@app/config/console.php' ) );
$consoleApp = new ConsoleApplication( $config );
if (!is_null( $controllerNamespace )) {
$consoleApp->controllerNamespace = $controllerNamespace;
}
try {
// use current connection to DB
\Yii::$app->set( 'db', $oldApp->db );
ob_start();
$exitCode = $consoleApp->runAction(
$route,
array_merge( $params, [ 'interactive' => false ] )
);
$result = ob_get_clean();
\Yii::trace( $result, 'console' );
} catch ( \Exception $e ) {
\Yii::warning( $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine(), 'console' );
$exitCode = 1;
}
\Yii::$app = $oldApp;
return $exitCode;
}
} |
Upd: $exitCode = $consoleApp->runAction(
$route,
array_merge( $params, [ 'interactive' => false ] )
); to $exitCode = $consoleApp->runAction(
$route,
array_merge( func_get_arg( 1 ), [ 'interactive' => false, 'color' => false ] )
); since it is collision with variable used in console.php (config from base application), or can use other variable name in the function parameters. Now all working. For future independent behavior (of console.php variables) we can change $route to func_get_arg( 0 ), and $controllerNamespace to func_get_arg( 2 ). |
@mahsa92 please use the forum to ask questions. Or open a new issue to request a feature or report a bug. |
Would be great to have ability to run console commands inside each other if user want to. Example: user designed command and want it to be able also to apply migrations (and then make some decisions on exit-code results), maybe call some other his written command. In Yii1 i was needed in such case, but there is nothing about it for Yii1. I got brief look of Yii2 console commands handling, should not be difficult. But anyway would be great if someone from core developers will handle this one. Proposed syntax
The text was updated successfully, but these errors were encountered: