Skip to content

[DependencyInjection] Prevent service:method factory notation in PHP config #24732

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
Nov 17, 2017

Conversation

vudaltsov
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #24704
License MIT
Doc PR

Started working on fixing #24704.

@nicolas-grekas, am I on the right way? If yes I will look at the tests and try to add this case.

@vudaltsov
Copy link
Contributor Author

Should I move the string splitting logic to the AbstractConfigurator::processValue?

@ro0NL
Copy link
Contributor

ro0NL commented Oct 29, 2017

See YamlFileLoader::parseCallable; we need to support configurator as well i guess. Also account for :: before splitting by : (see setFactory).

Im not sure why we dont do this directly in Definition::setFactory & co; right now XML is missing this feature as well. (e.g. function="service:method").

Otherwise perhaps go with static::processValue(static::processCallable()) or so.

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Oct 30, 2017
@nicolas-grekas
Copy link
Member

I know I suggested this one, but I've mixed feelings about it :)
What about throwing instead? Providing a single way to reference services would make teaching easier.
The message should suggest to use [ref('foo'), 'bar'] when foo:bar is provided.
WDYT?

@ogizanagi
Copy link
Contributor

I'd also prefer the exception suggested by @nicolas-grekas instead of this.

@vudaltsov
Copy link
Contributor Author

You know, I would also agree :)
When trying to implement it I realized that it looks unnatural.
[ref(), ''] definitely makes much more sense.

@nicolas-grekas
Copy link
Member

@vudaltsov let's throw here then instead?

@vudaltsov vudaltsov force-pushed the di-php-string-factory-fix branch from c83ae1f to 81ead82 Compare November 16, 2017 21:58
@vudaltsov
Copy link
Contributor Author

@nicolas-grekas, done

@vudaltsov vudaltsov changed the title [DependencyInjection] Add service:method notation in PHP config [DependencyInjection] Prevent service:method factory notation in PHP config Nov 16, 2017
@@ -22,6 +24,10 @@
*/
final public function factory($factory)
{
if (is_string($factory) && 1 === substr_count($factory, ':')) {
throw new InvalidArgumentException('The short factory notation is not supported by the PHP-based DI configuration.');
Copy link
Contributor

Choose a reason for hiding this comment

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

In addition, it should also suggest to use [ref('service'), 'method'] instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am also considering to replace short factory notation with service:method notation. WDYT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the final exception will look like The service:method factory notation is not supported by the PHP-based DI configuration. Use [ref('service'), 'method'] instead.

Copy link
Member

Choose a reason for hiding this comment

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

LGTM, with double quotes around, and resolved/actual id+method, eg:

Invalid factory "%s:%s": the `service:method` notation is not available when using PHP-based DI configuration. Use "[ref('%s'), '%s']" instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Static is still allowed using factory('class::method')? Or should that notation also be explicit; thus factory('class', 'method').

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

(could you please squash, and update the commit message meanwhile please)

@vudaltsov vudaltsov force-pushed the di-php-string-factory-fix branch from be6fa70 to 49fc677 Compare November 17, 2017 09:52
@vudaltsov
Copy link
Contributor Author

@nicolas-grekas, squashed, reworded and rebased

@nicolas-grekas
Copy link
Member

Thank you @vudaltsov.

@nicolas-grekas nicolas-grekas merged commit 49fc677 into symfony:3.4 Nov 17, 2017
nicolas-grekas added a commit that referenced this pull request Nov 17, 2017
…ion in PHP config (vudaltsov)

This PR was merged into the 3.4 branch.

Discussion
----------

[DependencyInjection] Prevent service:method factory notation in PHP config

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24704
| License       | MIT
| Doc PR        |

Started working on fixing #24704.

@nicolas-grekas, am I on the right way? If yes I will look at the tests and try to add this case.

Commits
-------

49fc677 Throw on service:method factory notation in PHP-based DI configuration
@vudaltsov vudaltsov deleted the di-php-string-factory-fix branch November 17, 2017 10:02
This was referenced Nov 21, 2017
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