forked from symfony/symfony
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DI] Add compiler pass which check extistence of service class and
factory_class name (using class_exists() ) if that is possible - definition is not syntetic or is not definition via factory method.
- Loading branch information
Showing
9 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
src/Symfony/Component/DependencyInjection/Compiler/CheckServiceClassPass.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,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\BadServiceClassException; | ||
|
||
/** | ||
* Checks your service exists by checking its definition "class" and "factory_class" keys | ||
* | ||
* @author Leszek "l3l0" Prabucki <leszek.prabucki@gmail.com> | ||
*/ | ||
class CheckServiceClassPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* Checks if ContainerBuilder services exists | ||
* | ||
* @param ContainerBuilder $container The ContainerBuilder instances | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$parameterBag = $container->getParameterBag(); | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ($this->allowsToCheckClassExistenceForClass($definition) && !class_exists($parameterBag->resolveValue($definition->getClass()))) { | ||
throw new BadServiceClassException($id, $definition->getClass(), 'class'); | ||
} | ||
if ($this->allowsToCheckClassExistenceForFactoryClass($definition) && !class_exists($parameterBag->resolveValue($definition->getFactoryClass()))) { | ||
throw new BadServiceClassException($id, $definition->getFactoryClass(), 'factory_class'); | ||
} | ||
} | ||
} | ||
|
||
private function allowsToCheckClassExistenceForClass(Definition $definition) | ||
{ | ||
return $definition->getClass() && !$this->isFactoryDefinition($definition) && !$definition->isSynthetic(); | ||
} | ||
|
||
private function allowsToCheckClassExistenceForFactoryClass(Definition $definition) | ||
{ | ||
return $definition->getFactoryClass() && !$definition->isSynthetic(); | ||
} | ||
|
||
private function isFactoryDefinition(Definition $definition) | ||
{ | ||
return $definition->getFactoryClass() || $definition->getFactoryService(); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/DependencyInjection/Exception/BadServiceClassException.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,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Exception; | ||
|
||
class BadServiceClassException extends RuntimeException | ||
{ | ||
public function __construct($id, $className, $key) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'Class "%s" not found. Check the spelling on the "%s" configuration for your "%s" service.', | ||
$className, | ||
$key, | ||
$id | ||
) | ||
); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckServiceClassPassTest.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,106 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CheckServiceClassPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class CheckServiceClassPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\BadServiceClassException | ||
* @expectedExceptionMessage Class "\InvalidName\TestClass" not found. Check the spelling on the "class" configuration for your "a" service. | ||
*/ | ||
public function testThrowsBadNameExceptionWhenServiceHasInvalidClassName() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('a', '\InvalidName\TestClass'); | ||
|
||
$this->process($container); | ||
} | ||
|
||
public function testDoesNotThrowExceptionIfClassExists() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('a', '\Symfony\Component\DependencyInjection\Compiler\CheckServiceClassPass'); | ||
$container->register('b', '\stdClass'); | ||
$container->register('c', '\Symfony\Component\DependencyInjection\Tests\Compiler\MyTestService'); | ||
$container | ||
->register('d', '\stdClass') | ||
->setFactoryService('\Symfony\Component\DependencyInjection\Tests\Compiler\MyTestService') | ||
->setFactoryMethod('factoryMethod') | ||
; | ||
|
||
$this->process($container); | ||
} | ||
|
||
public function testSynteticServicesClassNamesAreNotChecked() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container | ||
->register('a', '\InvalidName\TestClass') | ||
->setSynthetic(true) | ||
; | ||
|
||
$this->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\BadServiceClassException | ||
* @expectedExceptionMessage Class "\InvalidName\TestClass" not found. Check the spelling on the "factory_class" configuration for your "a" service. | ||
*/ | ||
public function testFactoryMethodServicesClassNamesAreNotChecked() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container | ||
->register('a', '\stdClass') | ||
->setFactoryClass('\InvalidName\TestClass') | ||
->setFactoryMethod('factoryMethod') | ||
; | ||
|
||
$this->process($container); | ||
} | ||
|
||
public function testServicesWithoutNameAreNotChecked() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container | ||
->register('a') | ||
; | ||
|
||
$this->process($container); | ||
} | ||
|
||
public function testSynteticServicesNameAreNotChecked() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container | ||
->register('a', '\InvalidName\TestClass') | ||
->setSynthetic(true) | ||
; | ||
|
||
$this->process($container); | ||
} | ||
|
||
protected function process(ContainerBuilder $container) | ||
{ | ||
$pass = new CheckServiceClassPass(); | ||
$pass->process($container); | ||
} | ||
} | ||
|
||
class MyTestService | ||
{ | ||
public function factoryMethod() | ||
{ | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.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
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