Skip to content

[Console] command as service #3620

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

Closed

Conversation

gnugat
Copy link

@gnugat gnugat commented Feb 28, 2014

Q A
Doc fix? yes
New docs? no
Applies to 2.4+
Fixed tickets N/A

Command as a service can be useful to give access to services and
configuration parameters in the configure method.

A simple use case: you want to allow the user to set an option's default
value in the app/config/parameters.yml file. Or the default value
needs to be computed by a service (database retrieval for instance).

With a ContainerAwareCommand, this wouldn't be possible because the
configure method is called from the constructor, so the container
isn't set yet.

nixilla and others added 3 commits February 12, 2014 16:28
Otherwise you'll get:
```
ContextErrorException: Runtime Notice: call_user_func() expects parameter 1 to be a valid callback, non-static method Vendor\Package\Validator::validate() should not be called statically in Symfony/Component/Validator/Constraints/CallbackValidator.php
```
…be static (nixilla)

This PR was merged into the 2.4 branch.

Discussion
----------

Callback: [Validator, validate] expects validate to be static

Otherwise you'll get:
```
ContextErrorException: Runtime Notice: call_user_func() expects parameter 1 to be a valid callback, non-static method Vendor\Package\Validator::validate() should not be called statically in Symfony/Component/Validator/Constraints/CallbackValidator.php
```

Commits
-------

027bc97 Callback: [Validator, validate] expects validate to be static
| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | 2.4+
| Fixed tickets | N/A

Command as a service can be useful to give access to services and
configuration parameters in the `configure` method.

A simple use case: you want to allow the user to set an option's default
value in the `app/config/parameters.yml` file. Or the default value
needs to be computed by  a service (database retrieval for instance).

With a `ContainerAwareCommand`, this wouldn't be possible because the
`configure` method is called from the constructor, so the container
isn't set yet.
bundles and automatically register your commands. For the ones implementing
the ``ContainerAwareCommand`` interface, Symfony will even inject the container.

If you wan to, you can instead register them as services in the container using
Copy link
Member

Choose a reason for hiding this comment

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

Typo: wan

@wouterj
Copy link
Member

wouterj commented Mar 1, 2014

Thank you for the addition!

However, it seems like you based your PR on another branch than the branch itself... This should also be merged into the 2.3 branch

@gnugat
Copy link
Author

gnugat commented Mar 1, 2014

Hi! I based my branch on 2.4, because command as service have been introduced in this version. How can I fix this?

@wouterj
Copy link
Member

wouterj commented Mar 1, 2014

Ah, my bad :)

Well, you can leave this PR open. Since your branch is based on 2.4, it's easy to merge into the 2.4 branch. If you really want to do it the correct way, you have to close this one and open it again against the 2.4 branch (you can't change the base branch of a PR on github).

@gnugat
Copy link
Author

gnugat commented Mar 1, 2014

Ok I'll do that with the fixes. Cheers!

@gnugat gnugat closed this Mar 1, 2014
``configure`` method

For example, Imagine you want to provide a default value for the ``name``
option. You could hard code a string and pass it as the 4th argument of
Copy link
Member

Choose a reason for hiding this comment

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

pass it [...] to

@gnugat
Copy link
Author

gnugat commented Mar 9, 2014

@xabbuh: this PR has been closed, the new one's here: #3621

weaverryan added a commit that referenced this pull request Mar 24, 2014
This PR was merged into the 2.4 branch.

Discussion
----------

[Console] Command as service

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | 2.4+
| Fixed tickets | N/A

Command as a service can be useful to give access to services and
configuration parameters in the `configure` method.

A simple use case: you want to allow the user to set an option's default
value in the `app/config/parameters.yml` file. Or the default value
needs to be computed by  a service (database retrieval for instance).

With a `ContainerAwareCommand`, this wouldn't be possible because the
`configure` method is called from the constructor, so the container
isn't set yet.

Original PR: #3620

Commits
-------

c8fe610 Moved back and listened to @weaverryan
e8b3320 Took into account @cordoval's feedback
11bfe50 Added warning about performances
6a7a25f Fixed @wouterj's feedback
e137951 Created a new article
cdd534a Added @lsmith77 and @dbu use cases
a7b916e Fixed typos spotted by @cordoval
a055140 Took @wouterj's advices into account
947ad92 [Console] Adding use cases to command as service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants