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

[DependencyInjection] ActionBundle integration: introduce _instanceof#21357

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
dunglas wants to merge5 commits intosymfony:masterfromdunglas:instanceof

Conversation

@dunglas
Copy link
Member

@dunglasdunglas commentedJan 20, 2017
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?no
Fixed ticketsn/a
LicenseMIT
Doc PRtodo

There is some work being done to include features ofDunglasActionBundle in the core of Symfony. The goal of all those PRs is to improve the developper experience of the framework, allow to develop faster while preserving all benefits of using Symfony (strictness, modularity, extensibility...) and make it easier to learn for newcomers.

This PR implements the tagging feature of ActionBundle in a more generic way. It will help to get rid ofAppBundle in the the standard edition and to register automatically some classes including commands.

Here is an example of config (that can be embedded in the standard edition) to enable those features:

# config/services.ymlservices:_defaults:autowire:['get*', 'set*']# Enable constructor, getter and setter autowiring for all services defined in this file_instanceof:Symfony\Component\Console\Command:# Add the console.command tag to all services defined in this file having this typetags:['console.command']# Set tags but also other settings like "public", "autowire" or "shared" hereTwig_ExtensionInterface:tags:['twig.extension']Symfony\Component\EventDispatcher\EventSubscriberInterface:tags:['kernel.event_subscriber']App\:# Register all classes in the src/Controller directory as servicespsr4:../src/{Controller,Command,Twig,EventSubscriber}

It's part of our 0 config initiative: controllers and commands will be automatically registered as services and "autowired", allowing the user to create and inject new services without having to write a single line of YAML or XML.
When refactoring changes are also automatically updated and don't require to update config files. It's a big win for rapid application development and prototyping.

Of course, this is fully compatible with the actual way of defining services and it's possible to switch (or mix) approaches very easily. It's even possible to start prototyping using 0config features then switch to explicit services definitions when the project becomes mature.

Thanks a lot to@nicolas-grekas for defining the_instanceof syntax and giving hints for the implementation.

  • Remove some code duplication

theofidry, Simperfit, Cydonia7, nicolas-grekas, GuilhemN, ogizanagi, mvrhov, blazarecki, and TomasVotruba reacted with thumbs up emojijvasseur, mnapoli, juuuuuu, conishiwa, and philippecarle reacted with confused emoji
$s->setAttribute('parent',$parentId);
}
// TODO: move the ID generation or maybe the whole block in the parent class
$parentId =md5("$file.$type.$id");
Copy link
MemberAuthor

@dunglasdunglasJan 20, 2017
edited
Loading

Choose a reason for hiding this comment

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

Including the full file name is a bad idea... It makes the md5 related to the absolute path of the file. It will create deployment problems and it's why test fails. I'll change that.

Copy link
Contributor

Choose a reason for hiding this comment

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

you can still have something like:

$parentId =$baseParentId =md5("$type.$id");$i =0;while ($this->container->has($parentId)) {    ++$i;$parentId =$baseParentId.$i;}

}

if ($parentId) {
$s->setAttribute('parent',$parentId);
Copy link
Contributor

@GuilhemNGuilhemNJan 21, 2017
edited
Loading

Choose a reason for hiding this comment

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

shouldn't it throw an exception if there is already a parent?

$s->setAttribute('parent',$parentId);
}
// TODO: move the ID generation or maybe the whole block in the parent class
$parentId =md5("$file.$type.$id");
Copy link
Contributor

Choose a reason for hiding this comment

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

you can still have something like:

$parentId =$baseParentId =md5("$type.$id");$i =0;while ($this->container->has($parentId)) {    ++$i;$parentId =$baseParentId.$i;}

@dunglas
Copy link
MemberAuthor

Status: needs review

}

if ($parentId) {
$s->setAttribute('parent',$parentId);
Copy link
Member

Choose a reason for hiding this comment

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

There is an issue here. This is altering the DOM node, which means it will impact the next services too (which will break things if they are not ChildDefinition themselves)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Fixed.

return$definition;
}

$className =$definition->getClass() ?:$id;
Copy link
Member

Choose a reason for hiding this comment

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

this is broken when the class is a parameter (IIRC, this is still supported in 3.x, even though Symfony itself does not use the feature anymore)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

IIUC, it cannot be fixed in the loader directly because all parameters may have not been set at this time...

Copy link
MemberAuthor

@dunglasdunglasJan 24, 2017
edited
Loading

Choose a reason for hiding this comment

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

We have 2 options here:

  1. Don't support parameters as id when using _instanceof and document this behavior
  2. Store the something like the following and the container and create child definitions in a compiler pass
['/foo/bar.yaml' => ['services' => ['id1','id2'],'instanceof' => ['type1' =>newDefinition(/*...*/),'type2' =>newDefinition(/*...*/)]]]

For the sake of simplicity, I'm for the solution 1. Solution 2 will create new edges cases (when using inline definitions for instance).
Or maybe someone have a better idea?

}

if (isset($underscoreParam['alias']) ||isset($underscoreParam['class']) ||isset($underscoreParam['factory'])) {
@trigger_error(sprintf('Giving a service the "%s" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.',$name),E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

should we deprecate using_ as the first char of a service id entirely to open the door for future reserved names ?

All shortcuts we are adding in 3.3 will make it almost impossible to implement such detection in 3.4+ (class is now optional)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

👍 to deprecate all services starting by_.

Copy link
Member

@nicolas-grekasnicolas-grekasJan 24, 2017
edited
Loading

Choose a reason for hiding this comment

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

I'm mixed on this deprecation at the general service id level.
But I'm 👍 for deprecating such idswhen using the YamlFileLoader

dunglas reacted with thumbs up emoji
}

if (isset($underscoreParam['alias']) ||isset($underscoreParam['class']) ||isset($underscoreParam['factory'])) {
@trigger_error(sprintf('Giving a service the "%s" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.',$name),E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

Btw, we have an inconsistency here:_defaults and_instanceof are allowed as service ids in other loaders.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Will be fixed if we deprecate all services starting with a_ as you suggested.

@stof
Copy link
Member

status: needs work

*/
protectedfunctiongenerateInstanceofDefinitionId($id,$type,$file)
{
returnsprintf('%s_%s_%s',$id,$type,hash('sha256',$file));

Choose a reason for hiding this comment

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

generated ids should start with a number so that we don't confuse them with classes (maybe not applicable here, but still a good default practice to me).

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Likesprintf('0%s_%s_%s', $id, $type, hash('sha256', $file));?

Choose a reason for hiding this comment

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

yes

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

done

@nicolas-grekas
Copy link
Member

Continued in#21530

fabpot added a commit that referenced this pull requestFeb 17, 2017
…al interface-defined configs (nicolas-grekas, dunglas)This PR was merged into the 3.3-dev branch.Discussion----------[DependencyInjection] Add "instanceof" section for local interface-defined configs| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -This is a direction follow up of#21357 on which we're working together with@dunglas. From the description posted there:There is some work being done to include features of [DunglasActionBundle](https://github.com/dunglas/DunglasActionBundle) in the core of Symfony. The goal of all those PRs is to improve the developper experience of the framework, allow to develop faster while preserving all benefits of using Symfony (strictness, modularity, extensibility...) and make it easier to learn for newcomers.This PR implements the tagging feature of ActionBundle in a more generic way. It will help to get rid of `AppBundle` in the the standard edition and to register automatically some classes including commands.Here is an example of config (that can be embedded in the standard edition) to enable those features:```yaml# config/services.ymlservices:    _defaults:        autowire: ['get*', 'set*'] # Enable constructor, getter and setter autowiring for all services defined in this file    _instanceof:        Symfony\Component\Console\Command: # Add the console.command tag to all services defined in this file having this type            tags: ['console.command']            # Set tags but also other settings like "public", "autowire" or "shared" here        Twig_ExtensionInterface:            tags: ['twig.extension']        Symfony\Component\EventDispatcher\EventSubscriberInterface:            tags: ['kernel.event_subscriber']    App\: # Register all classes in the src/Controller directory as services        psr4: ../src/{Controller,Command,Twig,EventSubscriber}```It's part of our 0 config initiative: controllers and commands will be automatically registered as services and "autowired", allowing the user to create and inject new services without having to write a single line of YAML or XML.When refactoring changes are also automatically updated and don't require to update config files. It's a big win for rapid application development and prototyping.Of course, this is fully compatible with the actual way of defining services and it's possible to switch (or mix) approaches very easily. It's even possible to start prototyping using 0config features then switch to explicit services definitions when the project becomes mature.Commits-------773eca7 [DependencyInjection] Tests + refacto for "instanceof" definitions2fb6019 [DependencyInjection] Add "instanceof" section for local interface-defined configs
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@stofstofstof requested changes

+1 more reviewer

@GuilhemNGuilhemNGuilhemN left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

5 participants

@dunglas@stof@nicolas-grekas@GuilhemN@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp