/**
  * Process the Container to replace aliases with service definitions.
  *
  * @param ehough_iconic_ContainerBuilder $container
  *
  * @throws ehough_iconic_exception_InvalidArgumentException if the service definition does not exist
  */
 public function process(ehough_iconic_ContainerBuilder $container)
 {
     $this->compiler = $container->getCompiler();
     $this->formatter = $this->compiler->getLoggingFormatter();
     foreach ($container->getAliases() as $id => $alias) {
         $aliasId = (string) $alias;
         try {
             $definition = $container->getDefinition($aliasId);
         } catch (ehough_iconic_exception_InvalidArgumentException $e) {
             if (version_compare(PHP_VERSION, '5.3') < 0) {
                 throw new ehough_iconic_exception_InvalidArgumentException(sprintf('Unable to replace alias "%s" with "%s".', $alias, $id), null);
             } else {
                 throw new ehough_iconic_exception_InvalidArgumentException(sprintf('Unable to replace alias "%s" with "%s".', $alias, $id), null, $e);
             }
         }
         if ($definition->isPublic()) {
             continue;
         }
         $definition->setPublic(true);
         $container->setDefinition($id, $definition);
         $container->removeDefinition($aliasId);
         $this->updateReferences($container, $aliasId, $id);
         // we have to restart the process due to concurrent modification of
         // the container
         $this->process($container);
         break;
     }
 }
 /**
  * Removes abstract definitions from the ContainerBuilder
  *
  * @param ehough_iconic_ContainerBuilder $container
  */
 public function process(ehough_iconic_ContainerBuilder $container)
 {
     $compiler = $container->getCompiler();
     $formatter = $compiler->getLoggingFormatter();
     foreach ($container->getDefinitions() as $id => $definition) {
         if ($definition->isAbstract()) {
             $container->removeDefinition($id);
             $compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'abstract'));
         }
     }
 }
예제 #3
0
 /**
  * Processes the ContainerBuilder to remove unused definitions.
  *
  * @param ehough_iconic_ContainerBuilder $container
  */
 public function process(ehough_iconic_ContainerBuilder $container)
 {
     $compiler = $container->getCompiler();
     $formatter = $compiler->getLoggingFormatter();
     $graph = $compiler->getServiceReferenceGraph();
     $hasChanged = false;
     foreach ($container->getDefinitions() as $id => $definition) {
         if ($definition->isPublic()) {
             continue;
         }
         if ($graph->hasNode($id)) {
             $edges = $graph->getNode($id)->getInEdges();
             $referencingAliases = array();
             $sourceIds = array();
             foreach ($edges as $edge) {
                 $node = $edge->getSourceNode();
                 $sourceIds[] = $node->getId();
                 if ($node->isAlias()) {
                     $referencingAliases[] = $node->getValue();
                 }
             }
             $isReferenced = count(array_unique($sourceIds)) - count($referencingAliases) > 0;
         } else {
             $referencingAliases = array();
             $isReferenced = false;
         }
         if (1 === count($referencingAliases) && false === $isReferenced) {
             $container->setDefinition((string) reset($referencingAliases), $definition);
             $definition->setPublic(true);
             $container->removeDefinition($id);
             $compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'replaces alias ' . reset($referencingAliases)));
         } elseif (0 === count($referencingAliases) && false === $isReferenced) {
             $container->removeDefinition($id);
             $compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'unused'));
             $hasChanged = true;
         }
     }
     if ($hasChanged) {
         $this->repeatedPass->setRepeat();
     }
 }