Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[FrameworkBundle] Multiple services on one Command class#19305

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@SenseException
Copy link
Contributor

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#19001
LicenseMIT
Doc PR

Based on my opened issue#19001, I created this PR to introduce the possibility of using multiple command-services with one command class. This should make Commands much more equal to other services in Symfony.

mcfedr reacted with thumbs up emoji
This way, a Command class can be used in more than just oneservice without breaking registerCommands of HttpKernelcomponent.
This way, a Command class can be used in more than just oneservice without breaking registerCommands of HttpKernelcomponent.
…issue_19001Conflicts:src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConsoleCommandPass.phpsrc/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php
$container->addCompilerPass(newAddConsoleCommandPass());
$container->setParameter('my-command.class','Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');

$definition1 =newDefinition('%my-command.class%');
Copy link
Contributor

Choose a reason for hiding this comment

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

Just put this in a variable, no need to use parameters to define class names

@linaori
Copy link
Contributor

linaori commentedJul 7, 2016
edited
Loading

So now that you can register the same command service twice, how will you solve the conflicting name problem?

@ogizanagi
Copy link
Contributor

@iltar: I guess it's up to you to configure a different name according to a constructor argument or whatever.

@linaori
Copy link
Contributor

This feature is closely related to another; Lazy command initialization. Ideally I'd configure the name via the tag.

ogizanagi and SenseException reacted with thumbs up emoji

@SenseException
Copy link
ContributorAuthor

Yes,Command allows to inject the command's name in the constructor. I think injecting it should be a best practice for a future major Symfony release, but I also like@iltar's idea with using the name in the tag.

@iltar: I'll adapt the test. Is there a link to that other feature you mentioned?

@linaori
Copy link
Contributor

No, but it's come up quite a few times. The current system lets Commands define their own name, which works. However, this means the command has to be initialized. So if you have 20 commands, every single command has to be instantiated in order to get the name.

The problem arises when you start using command as a service (my preference). It means that every single command will be initialized with all their dependencies. Hence there should be some kind of support of defining and retrieving their names without initializing them.

@linaori
Copy link
Contributor

Now that I think of it, Commands could be decorated. In a compiler pass you could do something that would eventually trigger this:

new LazyCommand($container, $commandServiceId, $name);getName(): $namerun($input, $output) {    $this->container->get($commandServiceId)->run($input, $output);}

}
$container->setAlias($serviceId ='console.command.'.strtolower(str_replace('\\','_',$class)),$id);
$serviceId ='console.command.'.strtolower(str_replace('\\','_',$class));
if ($container->hasAlias($serviceId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be replaced by something likethis to work for any number of commands.

linaori reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

You prefer to enumerate the alias for services of the same command class? What do we gain by adding another loop into the foreach and using numbers?

Copy link
Contributor

Choose a reason for hiding this comment

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

The actual proposition doesn't fix the current limitation but only reduce its impact.
For example still won't be able to have 3 commands with the same class.
If it is decided to fix this limitation it should be fully fixed.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

It uses the service name in the alias. For working services, the$id has to be unique, which is given in the alias too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh indeed sorry i thought it was a number... looks good to me then.

SenseException reacted with hooray emoji
Copy link
Contributor

@mcfedrmcfedrJul 26, 2016
edited
Loading

Choose a reason for hiding this comment

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

This little section can also be rearraged so that the check for$definition->isPublic() comes first, and avoid making$serviceId
Also, the check for existing alias could be avoided byalways making$serviceId with..._$id - there is no harm in doing this Seem that would break the automatic registration of Commands

@SenseException
Copy link
ContributorAuthor

It would be nice if Symfony would handle its commands like any other class in a service.

@mcfedr
Copy link
Contributor

Are there any work arounds for the moment? I guess I just need to create subclasses for each service.

@mcfedr
Copy link
Contributor

Also, I would call this a bug fix not a feature.

@SenseException
Copy link
ContributorAuthor

@mcfedr Subclasses are my current workaround. I marked the PR as feature because commands as a service were introduced in Symfony 2.4 and we're looking on legacy-code from the time before that. It just didn't grow with the rest.

@SenseException
Copy link
ContributorAuthor

Ping.

@nicolas-grekasnicolas-grekas added this to the3.x milestoneDec 6, 2016
@fabpot
Copy link
Member

👍 Can you rebase to fix conflicts and get rid of the merge commit? Thanks.

@fabpot
Copy link
Member

I've done the work in#22120

@fabpotfabpot closed thisMar 22, 2017
fabpot added a commit that referenced this pull requestMar 22, 2017
…ss (SenseException)This PR was squashed before being merged into the 3.3-dev branch (closes#22120).Discussion----------[FrameworkBundle] Multiple services on one Command classrebased version of#19305Commits-------2b82fcb [FrameworkBundle] Multiple services on one Command class
@nicolas-grekasnicolas-grekas modified the milestones:3.x,3.3Mar 24, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

8 participants

@SenseException@linaori@ogizanagi@mcfedr@fabpot@GuilhemN@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp