Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
made the DI config validation more strict to catch errors early
- Loading branch information
1 parent
4408cbc
commit f29a5f7
Showing
7 changed files
with
86 additions
and
11 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* This pass validates each definition individually only taking the information | ||
* into account which is contained in the definition itself. | ||
* | ||
* Later passes can rely on the following, and specifically do not need to | ||
* perform these checks themself: | ||
* | ||
* - non synthetic services always have a class set | ||
* - synthetic services are always public | ||
* - synthetic services are always of non-prototype scope | ||
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
class CheckDefinitionValidityPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
// synthetic service is public | ||
if ($definition->isSynthetic() && !$definition->isPublic()) { | ||
throw new \RuntimeException(sprintf( | ||
'A synthetic service ("%s") must be public.', | ||
$id | ||
)); | ||
} | ||
|
||
// synthetic service has non-prototype scope | ||
if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) { | ||
throw new \RuntimeException(sprintf( | ||
'A synthetic service ("%s") cannot be of scope "prototype".', | ||
$id | ||
)); | ||
} | ||
|
||
// non-synthetic service has class | ||
if (!$definition->isSynthetic() && !$definition->getClass()) { | ||
if ($definition->getFactoryService()) { | ||
throw new \RuntimeException(sprintf( | ||
'Please add the class to service "%s" even if it is constructed ' | ||
.'by a factory since we might need to add method calls based on ' | ||
.'interface injection, or other compile-time checks.', | ||
$id | ||
)); | ||
} | ||
|
||
throw new \RuntimeException(sprintf( | ||
'The definition for "%s" has no class. If you intend to inject ' | ||
.'this service dynamically at runtime, please mark it as synthetic=true, ' | ||
.'otherwise specify a class to get rid of this error.', | ||
$id | ||
)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters