Skip to content

[DependencyInjection] Tweaked factories #12065

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
Oct 3, 2014

Conversation

unkind
Copy link
Contributor

@unkind unkind commented Sep 27, 2014

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

There are some issues with new service factories:

  • ContainerBuilder cannot instantiate service from factory (i.e. currently it works for dumped code only)
  • Dumped code sometimes is invalid (anonymous services as factories, factories without arguments)

@unkind unkind changed the title [DependencyInjection] Tweak factories as callables feature [DependencyInjection] Tweaked factories as callables feature Sep 27, 2014
@unkind unkind force-pushed the bugfix-service-factory-tweaks branch 2 times, most recently from 562b870 to 4264436 Compare September 27, 2014 16:39
@@ -618,8 +617,7 @@ private function addService($id, $definition)
$this->addServiceMethodCalls($id, $definition).
$this->addServiceProperties($id, $definition).
$this->addServiceConfigurator($id, $definition).
$this->addServiceReturn($id, $definition)
;
$this->addServiceReturn($id, $definition);
Copy link
Member

Choose a reason for hiding this comment

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

Sorry about that but PHP-CS-Fixer is buggy here. The previous code was "better". Can you revert this one and the other one as well. Thanks.

@unkind unkind force-pushed the bugfix-service-factory-tweaks branch from 4264436 to cf905b7 Compare September 28, 2014 17:54
@unkind
Copy link
Contributor Author

unkind commented Sep 28, 2014

PhpDumper->addService:

        if ($definition->isSynthetic()) {
            $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
        } elseif ($class = $definition->getClass()) {
            $return[] = sprintf("@return %s A %s instance.", 0 === strpos($class, '%') ? 'object' : "\\".$class, $class);
        } elseif ($definition->getFactory()) {
            $factory = $definition->getFactory();
            if (is_string($factory)) {
                $return[] = sprintf('@return object An instance returned by %s().', $factory);
            } elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
                if (is_string($factory[0]) || $factory[0] instanceof Reference) {
                    $return[] = sprintf('@return object An instance returned by %s::%s().', (string) $factory[0], $factory[1]);
                } elseif ($factory[0] instanceof Definition) {
                    $return[] = sprintf('@return object An instance returned by %s::%s().', $factory[0]->getClass(), $factory[1]);
                }
            }
        } elseif ($definition->getFactoryClass()) {
            $return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryClass(), $definition->getFactoryMethod());
        } elseif ($definition->getFactoryService()) {
            $return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryService(), $definition->getFactoryMethod());
        }

I thought it is not allowed to define service without class, any real case for this code?

@unkind
Copy link
Contributor Author

unkind commented Sep 28, 2014

I'll fix tests later.

@nicolas-grekas
Copy link
Member

👍 once tests are fixed

@@ -955,6 +955,18 @@ public function createService(Definition $definition, $id, $tryProxy = true)
}

$service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
} elseif (null !== $definition->getFactory()) {
Copy link
Member

Choose a reason for hiding this comment

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

the factory should win over the factory method, to be consistent with the code generated for dumped containers

@stof
Copy link
Member

stof commented Sep 29, 2014

Please also add a test instantiating a service with a factory in the ContainerBuilder (well, several to cover the different cases)

@unkind unkind force-pushed the bugfix-service-factory-tweaks branch 8 times, most recently from b6ee1f8 to d9f2720 Compare October 1, 2014 20:25
@unkind
Copy link
Contributor Author

unkind commented Oct 1, 2014

Sorry for delay, tests have been updated.

@unkind unkind force-pushed the bugfix-service-factory-tweaks branch from d9f2720 to ee82392 Compare October 1, 2014 21:49
@unkind unkind changed the title [DependencyInjection] Tweaked factories as callables feature [DependencyInjection] Tweaked factories Oct 1, 2014
@unkind
Copy link
Contributor Author

unkind commented Oct 2, 2014

@fabpot @stof please, check it, I think it is possible to finish #11968 for 2.6.

@fabpot
Copy link
Member

fabpot commented Oct 3, 2014

Looks good to me @unkind. This is merged now. Thanks.

@fabpot fabpot merged commit ee82392 into symfony:master Oct 3, 2014
fabpot added a commit that referenced this pull request Oct 3, 2014
This PR was merged into the 2.6-dev branch.

Discussion
----------

[DependencyInjection] Tweaked factories

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12008
| License       | MIT
| Doc PR        | n/a

There are some issues with new service factories:
 * `ContainerBuilder` cannot instantiate service from factory (i.e. currently it works for dumped code only)
 * Dumped code sometimes is invalid (anonymous services as factories, factories without arguments)

Commits
-------

ee82392 [DependencyInjection] Tweaked factories
@unkind unkind deleted the bugfix-service-factory-tweaks branch October 3, 2014 07:48
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