Skip to content
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

gii command #2789

Closed
wants to merge 26 commits into from
Closed

gii command #2789

wants to merge 26 commits into from

Conversation

schmunk42
Copy link
Contributor

Draft

First off all I had a problem defining gii as a module for the console, it looks like the default controller is recognized as a command, so I had to configure the module as console-gii temporarily.

Sample output

$ ./yii

[...]
- console-gii/default   @author Qiang Xue <qiang.xue@gmail.com>

The command works like this:


$ ./yii gii/generate --generator=module --generate=true \
  --attributes='template:default;moduleClass:app\modules\foobar\Module;moduleID:foobar'
Loading generator 'module'...


Generating code using template "/Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/extensions/gii/generators/module/default"...
 generated modules/foobar/Module.php
 generated modules/foobar/controllers/DefaultController.php
 generated modules/foobar/views/default/index.php
done!

Custom generator:

$ ./yii gii/generate --generator=giiant --generate=true \
  --attributes='template:default;modelClass:app\models\TestBase;searchModelClass:app\models\TestBaseSearch;controllerClass:app\controllers\TestBaseController;providerList:\schmunk42\giiant\crud\providers\EditorProvider,\schmunk42\giiant\crud\providers\SelectProvider'




Loading generator 'giiant'...


Generating code using template "/Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/schmunk42/yii2-giiant/crud/default"...
   skipped controllers/TestBaseController.php
   skipped models/TestBaseSearch.php
 overwrote views/test-base/_form.php
   skipped views/test-base/_search.php
   skipped views/test-base/create.php
   skipped views/test-base/index.php
   skipped views/test-base/update.php
   skipped views/test-base/view.php
done!

Error output:

Errors:
-------
template: A code template must be selected.
modelClass: Model Class cannot be blank.
searchModelClass: Search Model Class cannot be blank.
controllerClass: Controller Class cannot be blank.

It can run the creation process and generate the files. Let me know what you think about the syntax.
If we can run commands from commands there's no need for a config file loader immediately.
I'll also try to implement the interactive fallback, if this approach is OK so far.

@Ragazzo
Copy link
Contributor

Ragazzo commented Mar 18, 2014

I think it will be better to use console output to enable colors and not simple echo .

@schmunk42
Copy link
Contributor Author

@Ragazzo Do you have any examples how to do that, the other core commands also use echos.

@Ragazzo
Copy link
Contributor

Ragazzo commented Mar 18, 2014

Fixture command and yii2-faker extension for example.

@qiangxue
Copy link
Member

That's neat. I'm not quite sure what is the best syntax. How about splitting attributes into individual options?

@schmunk42
Copy link
Contributor Author

Thanks :)

Here's another proposal: #1280 (comment)

I was unsure about the parsing of the same param multiple times. Can Command already deal with it?
I did not want to create an individual action for every model, because that's just unneeded overhead.

@cebe cebe self-assigned this Mar 18, 2014
@schmunk42
Copy link
Contributor Author

@qiangxue Please see #1280 (comment)


// TODO: is there are better way, needed for `./yii help gii`
public function getUniqueId(){
return 'gii/'.$this->generatorName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to override this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ ./yii help gii

DESCRIPTION

Allows you to run Gii from the command line.
Example command:
$ ./yii gii/<generator> --property1=foo --property2=bar --generate=true


SUB-COMMANDS

- gii/controllerPHP Fatal error:  Call to a member function getUniqueId() on a non-object in /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/Action.php on line 69
PHP Stack trace:
PHP   1. {main}() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/yii:0
PHP   2. yii\base\Application->run() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/yii:23
PHP   3. yii\console\Application->handleRequest() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/Application.php:302
PHP   4. yii\console\Application->runAction() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/console/Application.php:140
PHP   5. yii\base\Module->runAction() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/console/Application.php:173
PHP   6. yii\console\Controller->runAction() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/Module.php:581
PHP   7. yii\base\Controller->runAction() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/console/Controller.php:83
PHP   8. yii\base\InlineAction->runWithParams() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/Controller.php:127
PHP   9. call_user_func_array:{/Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/InlineAction.php:54}() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/InlineAction.php:54
PHP  10. yii\console\controllers\HelpController->actionIndex() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/InlineAction.php:54
PHP  11. yii\console\controllers\HelpController->getControllerHelp() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/console/controllers/HelpController.php:66
PHP  12. yii\console\controllers\HelpController->getActionSummary() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/console/controllers/HelpController.php:222
PHP  13. yii\base\Action->getUniqueId() /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/console/controllers/HelpController.php:254
exception 'yii\base\ErrorException' with message 'Call to a member function getUniqueId() on a non-object' in /Users/tobias/hrzg/gitlab/phundament/app-local-v4/vendor/yiisoft/yii2-dev/framework/base/Action.php:69

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs some debugging. It means Action::controller is null, which shouldn't happen normally.

@schmunk42 schmunk42 mentioned this pull request Mar 24, 2014
1 task
@qiangxue qiangxue added this to the 2.0 RC milestone Apr 16, 2014
@samdark
Copy link
Member

samdark commented Apr 18, 2014

@schmunk42 what's left in order for this cool feature to be merged?

@schmunk42
Copy link
Contributor Author

@samdark One problem at the moment is, that Yii 2 falsely detects module (web)-controllers as commands. So, in order to run the Gii command I had to configure Gii for the console as console-gii.
I was unsure how and where to fix it, because as far as I remember currently a command is detected just by checking if a file name ends with Controller.

The other thing is the help section, see also #1280 (comment)

But the functionality should be complete ... I'll review the code again in the next days.

@qiangxue
Copy link
Member

@schmunk42 Thanks. You may focus on the help section. I will look into the controller resolution issue.

@cebe
Copy link
Member

cebe commented Apr 18, 2014

We should separate console and web controllers. they can not reside in the same dir. had the same problem when starting to implement this but had no time to finally handle it.
Should be done as we already have it in basic app. console controllers should default to command dir while web controllers should be in controllers.

…de the correct source class for generating the help texts
@schmunk42
Copy link
Contributor Author

@qiangxue @cebe @samdark So, I got a "working" solution, but it needs some more refactoring.
As stated here #1280 (comment) you spoke about moving the generation of the help text logic to the base controller.

While I could still do this, I'd like to take a look at the current implementation (work in progress) where I just return the correct class to the help command for parsing the doc blocks. There are just some tiny changes to the help command instead of blowing up the base controller with code which is only needed for the help command. Maybe we could use an interface here to detect which controller/action has to be treated in a special way ... or we may add something like getHelpDocClass() to the base controller.
Would be happy about some advice here.

I'll also take care about moving the command to extensions and use the bootstrap method to register the command.

@schmunk42
Copy link
Contributor Author

Current Status

  • Help command displays correct information, see below
  • I refactored Action instead of console\Controller because I found no nice way to implement the required functionality into Controller
  • Open bug with falsely detected controllers from modules (console-gii)

Current Output

$ ./yii help    

The following commands are available:

- asset                 Allows you to combine and compress your JavaScript and CSS files.
- cache                 Allows you to flush cache.
- console-gii/default   @author Qiang Xue <qiang.xue@gmail.com>
- fixture               Manages loading and unloading fixtures.
- gii                   Allows you to run Gii from the command line.
- help                  Provides help information about console commands.
- message               Extracts messages to be translated from source files.
- migrate               Manages application migrations.
- packaii/default       
- prefab                This command echoes the first argument that you have entered.
- sakila-batch          @author Tobias Munk <schmunk@usrbin.de>
- templay/template      

To see the help of each command, enter:

  yii help <command-name>
$ ./yii help gii

DESCRIPTION

Allows you to run Gii from the command line.
Example command:
$ ./yii gii/<generator> --property1=foo --property2=bar --generate=true


SUB-COMMANDS

- gii/controller: This generator will generate a controller and one or a few ...
- gii/crud: Generates CRUD
- gii/extension: This generator will generate the skeleton files needed by an...
- gii/form: This generator will generate an action view file based on the spe...
- gii/giiant-crud
- gii/giiant-model: This generator will generate one or multiple ActiveRecord...
- gii/model: This generator will generate one or multiple ActiveRecord classe...
- gii/module: This generator will generate the skeleton code needed by a modu...

To see the detailed information about individual sub-commands, enter:

  yii help <sub-command>
$ ./yii help gii/crud

USAGE

yii gii/crud [...options...]


OPTIONS

--baseControllerClass: string (defaults to 'yii\\web\\Controller')

--controllerClass

--enableI18N: boolean (defaults to 0)
  whether the strings will be generated using `Yii::t()` or normal strings.

--indexWidgetType: string (defaults to 'grid')

--messageCategory: string (defaults to 'app')
  the message category used by `Yii::t()` when `$enableI18N` is `true`.
  Defaults to `app`.

--modelClass

--moduleID

--searchModelClass

--template: string
  the name of the code template that the user has selected.
  The value of this property is internally managed by this class.

--templates: array
  a list of available code templates. The array keys are the template names,
  and the array values are the corresponding template paths or path aliases.

@schmunk42
Copy link
Contributor Author

@samdark @qiangxue @cebe I'd really like to get this one merged and out of my head, could you have a look again, see my last post for the latest status.

@samdark
Copy link
Member

samdark commented May 31, 2014

Overall it looks good. @cebe will you handle it? I can take it if you don't currently have enough time.

@qiangxue
Copy link
Member

I think the code about help documentation still needs to be reworked (as described in #1280 (comment)). The newly added methods in the base action class do not make sense for general actions.

@schmunk42
Copy link
Contributor Author

Just let me know, if I can still assist you with this (via github, Skype, etc...).

@cebe
Copy link
Member

cebe commented May 31, 2014

I am going to look into this, just will take some more time.

Conflicts:
	extensions/gii/Module.php
@schmunk42
Copy link
Contributor Author

I've merged the latest master.

This is my current console config, in $gii I store my gii config with custom providers.
Defining the command as giic is just a temporary workaround, because of the falsely detected controllers.

    'controllerMap'       => [
        'giic' => [
            'class' => 'yii\gii\commands\GiiController'
        ],
    ],
    'modules'             => [
        'console-gii' =>  $gii, // defined in common/config/main
        'gii' => $gii // defined in common/config/main
    ],

@schmunk42
Copy link
Contributor Author

I was able to remove the 'gii-console' hack and the command it now works just by using bootstrapping gii on console, like you do it in backend.

Regarding 607294a I still see 'gii' in the command list, when it is registered as a module in the console config; therefore the giic configuration.

@schmunk42
Copy link
Contributor Author

Could we please finish this one, I had to close #3273 because of too many changes. It would be a pity, if this one would also become unmergeable.

@cebe
Copy link
Member

cebe commented Jul 3, 2014

will try to check it tomorrow.

@schmunk42
Copy link
Contributor Author

I moved this into an extension: https://github.com/dmstr/yii2-gii-command

@schmunk42 schmunk42 closed this Aug 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants