for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Psi\Bundle\ContentType\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
abstract class AbstractRegistryPass implements CompilerPassInterface
{
private $registryId;
private $serviceTag;
private $registerByAlias;
public function __construct(
$registryId,
$serviceTag,
$registerByAlias = true
) {
$this->registryId = $registryId;
$this->serviceTag = $serviceTag;
$this->registerByAlias = $registerByAlias;
}
public function process(ContainerBuilder $container)
if (!$container->has($this->registryId)) {
return;
$taggedIds = $container->findTaggedServiceIds($this->serviceTag);
$registryDef = $container->getDefinition($this->registryId);
foreach ($taggedIds as $serviceId => $attributes) {
$attributes = $attributes[0];
if ($this->registerByAlias) {
if (!isset($attributes['alias'])) {
throw new InvalidArgumentException(sprintf(
$this->context . ' "%s" has no "alias" attribute in its tag',
context
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
$serviceId
));
$alias = $attributes['alias'];
} else {
$alias = $container->getDefinition($serviceId)->getClass();
$registryDef->addMethodCall('register', [$alias, new Reference($serviceId)]);
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: