-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👮 Prevent feed providers with duplicate source
The feed provider source should be unique for each class that extends the FeedProvider interface. In this commit we add a compiler pass who guards against violations of this rule. The container won't compile if a developer picks a source name that is already taken by another implementation of the feed provider interface.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...ed/Infrastructure/Framework/CompilerPass/FeedProviderSourceShouldBeUniqueCompilerPass.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,38 @@ | ||
<?php | ||
|
||
namespace App\Feed\Infrastructure\Framework\CompilerPass; | ||
|
||
use App\Feed\Application\Service\FeedProvider\FeedProvider; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
final class FeedProviderSourceShouldBeUniqueCompilerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$sources = []; | ||
|
||
$services = $container->findTaggedServiceIds(FeedProvider::class); | ||
|
||
foreach ($services as $service => $attributes) { | ||
if (class_exists($service) === false) { | ||
throw new \Exception(sprintf('Couldn\'t derive existing FQCN from service %s, the service is probably aliased', $service)); | ||
} | ||
|
||
if (method_exists($service, 'getSource') === false) { | ||
throw new \Exception('%s must implement static method `getSource`.'); | ||
} | ||
|
||
$source = call_user_func([$service, 'getSource']); | ||
|
||
if (in_array($source, $sources)) { | ||
throw new \Exception(sprintf( | ||
'The \'source\' on a FeedProvider should be unique. Duplicate source found for %s.', | ||
$service, | ||
)); | ||
} | ||
|
||
$sources[] = $source; | ||
} | ||
} | ||
} |
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