Skip to content

Commit

Permalink
feature #18547 DX: better error message if factory class is empty (dbu)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.1-dev branch.

Discussion
----------

DX: better error message if factory class is empty

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

i was trying to debug this service configuration:

```xml
        <service id="repository.user" class="Doctrine\ORM\EntityRepository">
            <argument>%user_entity_class%</argument>
            <factory id="doctrine.orm.default_entity_manager" method="getRepository"/>
        </service>
```

Turns out i should have used `<factory service=...` instead of `<factory id=...` but the error message does not even tell which service is wrong. The same happens when only specifying the `method=` attribute. The current exception message tells:

    Cannot dump definition because of invalid class name ('')

With this change, the message at least tells the context, hopefully helping the developer debug the issue:

    Empty class for factory of service repository.user and no factory service specified

Commits
-------

0999326 better error message if factory class is empty
  • Loading branch information
nicolas-grekas committed Apr 18, 2016
2 parents 8aec0bb + 0999326 commit 2824db3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -330,7 +330,7 @@ private function addServiceInlinedDefinitions($id, $definition)
throw new ServiceCircularReferenceException($id, array($id));
}

$code .= $this->addNewInstance($sDefinition, '$'.$name, ' = ');
$code .= $this->addNewInstance($sDefinition, '$'.$name, ' = ', $id);

if (!$this->hasReference($id, $sDefinition->getMethodCalls(), true) && !$this->hasReference($id, $sDefinition->getProperties(), true)) {
$code .= $this->addServiceMethodCalls(null, $sDefinition, $name);
Expand Down Expand Up @@ -404,7 +404,7 @@ private function addServiceInstance($id, $definition)
$instantiation .= ' = ';
}

$code = $this->addNewInstance($definition, $return, $instantiation);
$code = $this->addNewInstance($definition, $return, $instantiation, $id);

if (!$simple) {
$code .= "\n";
Expand Down Expand Up @@ -692,7 +692,7 @@ private function addServices()
return $publicServices.$privateServices;
}

private function addNewInstance(Definition $definition, $return, $instantiation)
private function addNewInstance(Definition $definition, $return, $instantiation, $id)
{
$class = $this->dumpValue($definition->getClass());

Expand All @@ -716,6 +716,10 @@ private function addNewInstance(Definition $definition, $return, $instantiation)
$class = $this->dumpValue($callable[0]);
// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
if ("''" === $class) {
throw new RuntimeException(sprintf('Cannot dump definition: The "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id));
}

return sprintf(" $return{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
}

Expand Down

0 comments on commit 2824db3

Please sign in to comment.