Skip to content

Introduction of syntax/shortcut aware Definition #15800

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
wants to merge 1 commit into from

Conversation

weaverryan
Copy link
Member

Hi guys!

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR not yet...

Similar to what #15778 does for routes, this aims to make creating Definitions in PHP easier. This is already quite easy, but this PR has 2 additional goals:

  1. Allow the YAML @service_name syntax to be used with Definitions. Subjectively, this makes building services in PHP much more appearling:
// currently
$container->register('pizza_chef', 'Acme\PizzaChef')
    ->addArgument(new Reference('fire_oven'))
    ->addMethodCall('setRecipeFactory', [new Reference('recipe_factory')]);

// after
$container->register('pizza_chef', 'Acme\PizzaChef')
    ->addArgument('@fire_oven')
    ->addMethodCall('setRecipeFactory', ['@recipe_factory']);

Also, this makes the PHP syntax very very close to the YAML syntax, make it easy to learn/teach if this were done more often, e.g in some micro/RAD edition.

  1. Accomplish (1) while still having the fluid ContainerBuilder::register() interface.

Thanks!

…ntaxAwareContainerBuilder.

The purpose is to allow for YAML-like @service_name shortcuts through PHP. The
SyntaxAwareDefinition is the most important part of this, but the SyntaxAwareContainerBuilder is also important as it allows you to use the ContainerBuilder::register() fluent interface
for adding these types of Definitions.

The SyntaxAwareParameterBag completes the last use of the shortcuts - expression shortcuts
inside parameters.
@fabpot
Copy link
Member

fabpot commented Sep 15, 2015

👎 from me. I still don't understand why creating a Reference would be complex or difficult for anyone using Symfony. Moreover, the @, ?and some other random characters we are using in YAML seems overly complex and very difficult to understand, at least for me. Last, but not the least, and IIRC, the YAML syntax does not allow to express everything.

I'm all for making things simpler, but this one does not seem to go in the right direction.

If we really want to make things "easier", and if a use statement or creating an object seems too complex, I would prefer to introduce a reference() function.

@Tobion
Copy link
Contributor

Tobion commented Sep 15, 2015

👎 We do not want to introduce magic again. The syntax for YAML only exists because there is no way to make it explicit as in PHP.

private function resolveServices($value)
{
if (is_array($value)) {
$value = array_map(array('self', 'resolveServices'), $value);
Copy link
Member

Choose a reason for hiding this comment

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

I would use a return here directly, which would allow to use if rather than elseif for next cases

@stof
Copy link
Member

stof commented Sep 15, 2015

-1 from me as well. Your SyntaxAwareDefinition violates SRP, which would break any compiler pass dealing with definitions later if it does not take care of it ($def->setArguments($def->getArguments()) does not work for your class)

@weaverryan
Copy link
Member Author

Thanks for the review - I'm convinced there's no solid technical way to accomplish this, even if we wanted to. Closing.

@weaverryan weaverryan closed this Sep 16, 2015
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