Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI] Deprecate XML services without ID #22903

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

Merged
merged 1 commit into from
May 25, 2017
Merged

[DI] Deprecate XML services without ID #22903

merged 1 commit into from
May 25, 2017

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented May 25, 2017

Q A
Branch? 3.4
Bug fix? no, confusing though
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #...
License MIT
Doc PR symfony/symfony-docs#...

On slack someone had a issue with class named services;

So, probably should have done this sooner, I stepped through with a debugger and it looks like \Symfony\Component\DependencyInjection\Loader\XmlFileLoader::processAnonymousServices assigns a sha256 to services that don't have any IDs
When my manually wired service is registered, it has an ID that looks like 1_344b468f6069ffe8c32092409d99c59abc218f41071ce4c4230c198876129bc0, so it doesn't override the auto-loaded one
I swear I read that IDs default to the class name now...

The fix was easy; doing <service id="ClassName"/> instead of <service class="ClassName"/>. However the thing is... i made the exact same mistake trying to reproduce 😅

I think given the recent developments (dropping type based autowiring and class named services) it makes sense to force XML service to specify an ID attribute (the top level ones). This would be consistent with YAML and PHP as well.

Fixing deprecations is also easy, just change class attribute to id like i've done for the frameworkbundle in this PR.

Any thoughts?

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍 makes sense to me

++$num;
}
if ($num) {
@trigger_error(sprintf('Anonymous services (%d) in file %s are deprecated since 3.4 and wont be supported in 4.0', $num, $file), 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.

imho, no need for $num. To be a bit more explicit, I propose:
Anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %s.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it will allow for missing deprecations more easy that way, but that's more or less related to loose comparison of those.

Updated :)

@ro0NL
Copy link
Contributor Author

ro0NL commented May 25, 2017

failures unrelated.

@@ -412,6 +412,10 @@ private function processAnonymousServices(\DOMDocument $xml, $file, $defaults)

// anonymous services "in the wild"
if (false !== $nodes = $xpath->query('//container:services/container:service[not(@id)]')) {
if ($nodes->length) {
@trigger_error(sprintf('Anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %s.', $file), E_USER_DEPRECATED);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas worth mentioning this only applies to outlined anon. services (top level, non-inlined... how should we call that?)

Copy link
Member

Choose a reason for hiding this comment

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

Top-level anonymous services... then?

@nicolas-grekas nicolas-grekas changed the base branch from master to 3.4 May 25, 2017 08:02
UPGRADE-3.4.md Outdated
@@ -11,5 +16,5 @@ Finder
Validator
---------

* not setting the `strict` option of the `Choice` constraint to `true` is
* Not setting the `strict` option of the `Choice` constraint to `true` is
Copy link
Member

Choose a reason for hiding this comment

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

should IMO be reverted as the sentence does not end with a dot

@xabbuh
Copy link
Member

xabbuh commented May 25, 2017

Do we still have a test which ensures that deprecations are not triggered for nested anonymous definitions?

@ro0NL
Copy link
Contributor Author

ro0NL commented May 25, 2017

Yep :) https://github.com/symfony/symfony/pull/22903/files#diff-25d0b562ad1c177bac8f4f9d0247b2acR7

it's tricky though due loose comparison of deprecations; if it triggered we wouldnt catch it (before it did by including the no. of occurences)

@xabbuh
Copy link
Member

xabbuh commented May 25, 2017

@ro0NL But this one is only tested in a @legacy test. So we would never notice when it accidentally triggered a deprecation.

@@ -230,6 +230,17 @@ public function testLoadAnonymousServices()
$this->assertSame($fooArgs[0], $barArgs[0]);
}

/**
* @group legacy
* @expectedDeprecation Top-level anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %sservices_without_id.xml.
Copy link
Member

@xabbuh xabbuh May 25, 2017

Choose a reason for hiding this comment

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

the %s seems to be a left-over from the sprintf pattern

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it includes an absolute path, so this deals with phpunit's assertStringMatchesFormat

@@ -412,6 +412,10 @@ private function processAnonymousServices(\DOMDocument $xml, $file, $defaults)

// anonymous services "in the wild"
if (false !== $nodes = $xpath->query('//container:services/container:service[not(@id)]')) {
if ($nodes->length) {
@trigger_error(sprintf('Top-level anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %s.', $file), 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.

maybe we should include the class in the deprecation message to make it easier to find the place

Copy link
Contributor Author

Choose a reason for hiding this comment

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

currently it triggers one deprecation per file, kinda like that; it goes away after you solved everything. Also class can be empty... not helping :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ro0NL
Copy link
Contributor Author

ro0NL commented May 25, 2017

@xabbuh extra test added

@fabpot
Copy link
Member

fabpot commented May 25, 2017

Thank you @ro0NL.

@fabpot fabpot merged commit b8c68da into symfony:3.4 May 25, 2017
fabpot added a commit that referenced this pull request May 25, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Deprecate XML services without ID

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no, confusing though
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

On slack someone had a issue with class named services;

> So, probably should have done this sooner, I stepped through with a debugger and it looks like \Symfony\Component\DependencyInjection\Loader\XmlFileLoader::processAnonymousServices assigns a sha256 to services that don't have any IDs
> When my manually wired service is registered, it has an ID that looks like 1_344b468f6069ffe8c32092409d99c59abc218f41071ce4c4230c198876129bc0, so it doesn't override the auto-loaded one
> I swear I read that IDs default to the class name now...

The fix was easy; doing `<service id="ClassName"/>` instead of `<service class="ClassName"/>`. However the thing is... i made the exact same mistake trying to reproduce 😅

I think given the recent developments (dropping type based autowiring and class named services) it makes sense to force XML service to specify an ID attribute (the top level ones). This would be consistent with YAML and PHP as well.

Fixing deprecations is also easy, just change `class` attribute to `id` like i've done for the frameworkbundle in this PR.

Any thoughts?

Commits
-------

b8c68da [DI] Deprecate XML services without ID
@ro0NL ro0NL deleted the di/xml-service branch May 26, 2017 07:13
@backbone87
Copy link
Contributor

Is it possible to allow unnamed toplevel services, which will be implicitly named after the class name?
So that <service class="Foo"> will generate a service of class Foo named Foo?
Imho the current behavior is quite counter intuitive since one expect with an unnamed toplevel service to be implicitly assigned the class name. If there are multiple unnamed top level services an error should be thrown during compilation.

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.

7 participants