isPublic() public method

Whether this service is public facing.
public isPublic ( ) : boolean
return boolean
 protected function isInlinableDefinition(ContainerBuilder $container, $id, Definition $definition)
 {
     if (!$definition->isShared()) {
         return true;
     }
     if ($definition->isPublic()) {
         return false;
     }
     $references = count(array_keys($container->getAliases(), $id, true));
     foreach ($container->getDefinitions() as $cDefinition) {
         if ($references > 1) {
             break;
         }
         if ($this->isReferencedByArgument($id, $cDefinition->getArguments())) {
             $references += 1;
             continue;
         }
         foreach ($cDefinition->getMethodCalls() as $call) {
             if ($this->isReferencedByArgument($id, $call[1])) {
                 $references += 1;
                 continue 2;
             }
         }
     }
     return $references <= 1;
 }
Example #2
0
 /**
  * Adds a service.
  *
  * @param string     $id
  * @param Definition $definition
  *
  * @return string
  */
 private function addService($id, $definition)
 {
     $code = "    {$id}:\n";
     if ($definition->getClass()) {
         $code .= sprintf("        class: %s\n", $definition->getClass());
     }
     if (!$definition->isPublic()) {
         $code .= "        public: false\n";
     }
     $tagsCode = '';
     foreach ($definition->getTags() as $name => $tags) {
         foreach ($tags as $attributes) {
             $att = array();
             foreach ($attributes as $key => $value) {
                 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
             }
             $att = $att ? ', ' . implode(', ', $att) : '';
             $tagsCode .= sprintf("            - { name: %s%s }\n", $this->dumper->dump($name), $att);
         }
     }
     if ($tagsCode) {
         $code .= "        tags:\n" . $tagsCode;
     }
     if ($definition->getFile()) {
         $code .= sprintf("        file: %s\n", $definition->getFile());
     }
     if ($definition->isSynthetic()) {
         $code .= sprintf("        synthetic: true\n");
     }
     if ($definition->isLazy()) {
         $code .= sprintf("        lazy: true\n");
     }
     if ($definition->getArguments()) {
         $code .= sprintf("        arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
     }
     if ($definition->getProperties()) {
         $code .= sprintf("        properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
     }
     if ($definition->getMethodCalls()) {
         $code .= sprintf("        calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
     }
     if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) {
         $code .= sprintf("        scope: %s\n", $scope);
     }
     if (null !== ($decorated = $definition->getDecoratedService())) {
         list($decorated, $renamedId) = $decorated;
         $code .= sprintf("        decorates: %s\n", $decorated);
         if (null !== $renamedId) {
             $code .= sprintf("        decoration_inner_name: %s\n", $renamedId);
         }
     }
     if ($callable = $definition->getFactory()) {
         $code .= sprintf("        factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
     }
     if ($callable = $definition->getConfigurator()) {
         $code .= sprintf("        configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
     }
     return $code;
 }
 /**
  * Adds a service
  *
  * @param string     $id
  * @param Definition $definition
  *
  * @return string
  */
 private function addService($id, $definition)
 {
     $code = "    {$id}:\n";
     if ($definition->getClass()) {
         $code .= sprintf("        class: %s\n", $definition->getClass());
     }
     if (!$definition->isPublic()) {
         $code .= "        public: false\n";
     }
     $tagsCode = '';
     foreach ($definition->getTags() as $name => $tags) {
         foreach ($tags as $attributes) {
             $att = array();
             foreach ($attributes as $key => $value) {
                 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
             }
             $att = $att ? ', ' . implode(' ', $att) : '';
             $tagsCode .= sprintf("            - { name: %s%s }\n", $this->dumper->dump($name), $att);
         }
     }
     if ($tagsCode) {
         $code .= "        tags:\n" . $tagsCode;
     }
     if ($definition->getFile()) {
         $code .= sprintf("        file: %s\n", $definition->getFile());
     }
     if ($definition->getFactoryMethod()) {
         $code .= sprintf("        factory_method: %s\n", $definition->getFactoryMethod());
     }
     if ($definition->getFactoryService()) {
         $code .= sprintf("        factory_service: %s\n", $definition->getFactoryService());
     }
     if ($definition->getArguments()) {
         $code .= sprintf("        arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
     }
     if ($definition->getProperties()) {
         $code .= sprintf("        properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
     }
     if ($definition->getMethodCalls()) {
         $code .= sprintf("        calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
     }
     if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) {
         $code .= sprintf("        scope: %s\n", $scope);
     }
     if ($callable = $definition->getConfigurator()) {
         if (is_array($callable)) {
             if ($callable[0] instanceof Reference) {
                 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
             } else {
                 $callable = array($callable[0], $callable[1]);
             }
         }
         $code .= sprintf("        configurator: %s\n", $this->dumper->dump($callable, 0));
     }
     return $code;
 }
 public function validate(Definition $definition) : bool
 {
     if (!$definition->isPublic() || $definition->isAbstract()) {
         return false;
     }
     if (null === $definition->getClass() || !class_exists($definition->getClass())) {
         return false;
     }
     return true;
 }
 /**
  * @return bool
  */
 private function canBeAdded(Definition $definition)
 {
     if ($definition->isAbstract()) {
         return false;
     }
     if (!$definition->isPublic()) {
         return false;
     }
     if (!$definition->getClass()) {
         return false;
     }
     return true;
 }
 protected function isInlinableDefinition(ContainerBuilder $container, $id, Definition $definition)
 {
     if (ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
         return true;
     }
     if ($definition->isPublic()) {
         return false;
     }
     if (!$this->graph->hasNode($id)) {
         return true;
     }
     $ids = array();
     foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
         $ids[] = $edge->getSourceNode()->getId();
     }
     return count(array_unique($ids)) <= 1;
 }
Example #7
0
 /**
  * Checks if the definition is valid.
  *
  * @param ContainerBuilder $container  The container.
  * @param Definition       $definition The definition.
  * @param string           $id         The identifier.
  * @param array            $tag        The tag data.
  *
  * @throws DefinitionException If the definition is not valid.
  */
 protected function validate(ContainerBuilder $container, Definition $definition, $id, array $tag)
 {
     if ($definition->isAbstract()) {
         throw DefinitionException::taggedServiceAbstract($id, $this->tag);
         // @codeCoverageIgnore
     }
     if (!$definition->isPublic()) {
         throw DefinitionException::taggedServiceNotPublic($id, $this->tag);
         // @codeCoverageIgnore
     }
     if (null !== $this->parent) {
         $reflection = new ReflectionClass($container->getParameterBag()->resolveValue($definition->getClass()));
         // @codeCoverageIgnoreStart
         if (!$reflection->isSubclassOf($this->parent)) {
             throw DefinitionException::taggedServiceNotSubclass($id, $this->tag, $this->parent);
         }
         // @codeCoverageIgnoreEnd
     }
 }
Example #8
0
    /**
     * Adds a service
     *
     * @param string     $id
     * @param Definition $definition
     *
     * @return string
     */
    private function addService($id, $definition)
    {
        $this->definitionVariables = new \SplObjectStorage();
        $this->referenceVariables = array();
        $this->variableCount = 0;
        $return = array();
        if ($definition->isSynthetic()) {
            $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
        } elseif ($class = $definition->getClass()) {
            $return[] = sprintf("@return %s A %s instance.", 0 === strpos($class, '%') ? 'object' : $class, $class);
        } elseif ($definition->getFactoryClass()) {
            $return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryClass(), $definition->getFactoryMethod());
        } elseif ($definition->getFactoryService()) {
            $return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryService(), $definition->getFactoryMethod());
        }
        $scope = $definition->getScope();
        if (!in_array($scope, array(ContainerInterface::SCOPE_CONTAINER, ContainerInterface::SCOPE_PROTOTYPE))) {
            if ($return && 0 === strpos($return[count($return) - 1], '@return')) {
                $return[] = '';
            }
            $return[] = sprintf("@throws InactiveScopeException when the '%s' service is requested while the '%s' scope is not active", $id, $scope);
        }
        $return = implode("\n     * ", $return);
        $doc = '';
        if (ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
            $doc .= <<<EOF

     *
     * This service is shared.
     * This method always returns the same instance of the service.
EOF;
        }
        if (!$definition->isPublic()) {
            $doc .= <<<EOF

     *
     * This service is private.
     * If you want to be able to request this service from the container directly,
     * make it public, otherwise you might end up with broken code.
EOF;
        }
        if ($definition->isLazy()) {
            $lazyInitialization = '$lazyLoad = true';
            $lazyInitializationDoc = "\n     * @param boolean \$lazyLoad whether to try lazy-loading the service with a proxy\n     *";
        } else {
            $lazyInitialization = '';
            $lazyInitializationDoc = '';
        }
        // with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
        $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
        $visibility = $isProxyCandidate ? 'public' : 'protected';
        $code = <<<EOF

    /**
     * Gets the '{$id}' service.{$doc}
     *{$lazyInitializationDoc}
     * {$return}
     */
    {$visibility} function get{$this->camelize($id)}Service({$lazyInitialization})
    {

EOF;
        $code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id) : '';
        if (!in_array($scope, array(ContainerInterface::SCOPE_CONTAINER, ContainerInterface::SCOPE_PROTOTYPE))) {
            $code .= <<<EOF
        if (!isset(\$this->scopedServices['{$scope}'])) {
            throw new InactiveScopeException('{$id}', '{$scope}');
        }


EOF;
        }
        if ($definition->isSynthetic()) {
            $code .= sprintf("        throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n    }\n", $id);
        } else {
            $code .= $this->addServiceInclude($id, $definition) . $this->addServiceLocalTempVariables($id, $definition) . $this->addServiceInlinedDefinitions($id, $definition) . $this->addServiceInstance($id, $definition) . $this->addServiceInlinedDefinitionsSetup($id, $definition) . $this->addServiceMethodCalls($id, $definition) . $this->addServiceProperties($id, $definition) . $this->addServiceConfigurator($id, $definition) . $this->addServiceReturn($id, $definition);
        }
        $this->definitionVariables = null;
        $this->referenceVariables = null;
        return $code;
    }
Example #9
0
 /**
  * @covers Symfony\Component\DependencyInjection\Definition::setPublic
  * @covers Symfony\Component\DependencyInjection\Definition::isPublic
  */
 public function testSetIsPublic()
 {
     $def = new Definition('stdClass');
     $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
     $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
     $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
 }
Example #10
0
 /**
  * @param Definition $definition
  * @param bool       $omitTags
  *
  * @return array
  */
 private function getContainerDefinitionData(Definition $definition, $omitTags = false)
 {
     $data = array('class' => (string) $definition->getClass(), 'scope' => $definition->getScope(), 'public' => $definition->isPublic(), 'synthetic' => $definition->isSynthetic(), 'file' => $definition->getFile());
     if (!$omitTags) {
         $data['tags'] = array();
         if (count($definition->getTags())) {
             foreach ($definition->getTags() as $tagName => $tagData) {
                 foreach ($tagData as $parameters) {
                     $data['tags'][] = array('name' => $tagName, 'parameters' => $parameters);
                 }
             }
         }
     }
     return $data;
 }
Example #11
0
 /**
  * @param Definition $definition
  * @param bool       $omitTags
  *
  * @return array
  */
 private function getContainerDefinitionData(Definition $definition, $omitTags = false)
 {
     $data = array('class' => (string) $definition->getClass(), 'public' => $definition->isPublic(), 'synthetic' => $definition->isSynthetic(), 'lazy' => $definition->isLazy());
     if (method_exists($definition, 'isShared')) {
         $data['shared'] = $definition->isShared();
     }
     $data['abstract'] = $definition->isAbstract();
     if (method_exists($definition, 'isAutowired')) {
         $data['autowire'] = $definition->isAutowired();
         $data['autowiring_types'] = array();
         foreach ($definition->getAutowiringTypes() as $autowiringType) {
             $data['autowiring_types'][] = $autowiringType;
         }
     }
     $data['file'] = $definition->getFile();
     if ($factory = $definition->getFactory()) {
         if (is_array($factory)) {
             if ($factory[0] instanceof Reference) {
                 $data['factory_service'] = (string) $factory[0];
             } elseif ($factory[0] instanceof Definition) {
                 throw new \InvalidArgumentException('Factory is not describable.');
             } else {
                 $data['factory_class'] = $factory[0];
             }
             $data['factory_method'] = $factory[1];
         } else {
             $data['factory_function'] = $factory;
         }
     }
     $calls = $definition->getMethodCalls();
     if (count($calls) > 0) {
         $data['calls'] = array();
         foreach ($calls as $callData) {
             $data['calls'][] = $callData[0];
         }
     }
     if (!$omitTags) {
         $data['tags'] = array();
         if (count($definition->getTags())) {
             foreach ($definition->getTags() as $tagName => $tagData) {
                 foreach ($tagData as $parameters) {
                     $data['tags'][] = array('name' => $tagName, 'parameters' => $parameters);
                 }
             }
         }
     }
     return $data;
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 protected function describeContainerDefinition(Definition $definition, array $options = array())
 {
     $description = isset($options['id']) ? array($this->formatSection('container', sprintf('Information for service <info>%s</info>', $options['id']))) : array();
     $description[] = sprintf('<comment>Service Id</comment>       %s', isset($options['id']) ? $options['id'] : '-');
     $description[] = sprintf('<comment>Class</comment>            %s', $definition->getClass() ?: "-");
     $tags = $definition->getTags();
     if (count($tags)) {
         $description[] = '<comment>Tags</comment>';
         foreach ($tags as $tagName => $tagData) {
             foreach ($tagData as $parameters) {
                 $description[] = sprintf('    - %-30s (%s)', $tagName, implode(', ', array_map(function ($key, $value) {
                     return sprintf('<info>%s</info>: %s', $key, $value);
                 }, array_keys($parameters), array_values($parameters))));
             }
         }
     } else {
         $description[] = '<comment>Tags</comment>             -';
     }
     $description[] = sprintf('<comment>Scope</comment>            %s', $definition->getScope());
     $description[] = sprintf('<comment>Public</comment>           %s', $definition->isPublic() ? 'yes' : 'no');
     $description[] = sprintf('<comment>Synthetic</comment>        %s', $definition->isSynthetic() ? 'yes' : 'no');
     $description[] = sprintf('<comment>Lazy</comment>             %s', $definition->isLazy() ? 'yes' : 'no');
     $description[] = sprintf('<comment>Synchronized</comment>     %s', $definition->isSynchronized() ? 'yes' : 'no');
     $description[] = sprintf('<comment>Abstract</comment>         %s', $definition->isAbstract() ? 'yes' : 'no');
     if ($definition->getFile()) {
         $description[] = sprintf('<comment>Required File</comment>    %s', $definition->getFile() ? $definition->getFile() : '-');
     }
     if ($definition->getFactoryClass()) {
         $description[] = sprintf('<comment>Factory Class</comment>    %s', $definition->getFactoryClass());
     }
     if ($definition->getFactoryService()) {
         $description[] = sprintf('<comment>Factory Service</comment>  %s', $definition->getFactoryService());
     }
     if ($definition->getFactoryMethod()) {
         $description[] = sprintf('<comment>Factory Method</comment>   %s', $definition->getFactoryMethod());
     }
     if ($factory = $definition->getFactory()) {
         if (is_array($factory)) {
             if ($factory[0] instanceof Reference) {
                 $description[] = sprintf('<comment>Factory Service</comment>  %s', $factory[0]);
             } elseif ($factory[0] instanceof Definition) {
                 throw new \InvalidArgumentException('Factory is not describable.');
             } else {
                 $description[] = sprintf('<comment>Factory Class</comment>    %s', $factory[0]);
             }
             $description[] = sprintf('<comment>Factory Method</comment>   %s', $factory[1]);
         } else {
             $description[] = sprintf('<comment>Factory Function</comment>    %s', $factory);
         }
     }
     $this->writeText(implode("\n", $description) . "\n", $options);
 }
Example #13
0
 /**
  * @param Definition  $definition
  * @param string|null $id
  * @param bool        $omitTags
  *
  * @return \DOMDocument
  */
 private function getContainerDefinitionDocument(Definition $definition, $id = null, $omitTags = false)
 {
     $dom = new \DOMDocument('1.0', 'UTF-8');
     $dom->appendChild($serviceXML = $dom->createElement('definition'));
     if ($id) {
         $serviceXML->setAttribute('id', $id);
     }
     $serviceXML->setAttribute('class', $definition->getClass());
     if ($definition->getFactoryClass()) {
         $serviceXML->setAttribute('factory-class', $definition->getFactoryClass());
     }
     if ($definition->getFactoryService()) {
         $serviceXML->setAttribute('factory-service', $definition->getFactoryService());
     }
     if ($definition->getFactoryMethod()) {
         $serviceXML->setAttribute('factory-method', $definition->getFactoryMethod());
     }
     $serviceXML->setAttribute('scope', $definition->getScope());
     $serviceXML->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
     $serviceXML->setAttribute('synthetic', $definition->isSynthetic() ? 'true' : 'false');
     $serviceXML->setAttribute('lazy', $definition->isLazy() ? 'true' : 'false');
     $serviceXML->setAttribute('synchronized', $definition->isSynchronized() ? 'true' : 'false');
     $serviceXML->setAttribute('abstract', $definition->isAbstract() ? 'true' : 'false');
     $serviceXML->setAttribute('file', $definition->getFile());
     if (!$omitTags) {
         $tags = $definition->getTags();
         if (count($tags) > 0) {
             $serviceXML->appendChild($tagsXML = $dom->createElement('tags'));
             foreach ($tags as $tagName => $tagData) {
                 foreach ($tagData as $parameters) {
                     $tagsXML->appendChild($tagXML = $dom->createElement('tag'));
                     $tagXML->setAttribute('name', $tagName);
                     foreach ($parameters as $name => $value) {
                         $tagXML->appendChild($parameterXML = $dom->createElement('parameter'));
                         $parameterXML->setAttribute('name', $name);
                         $parameterXML->appendChild(new \DOMText($this->formatParameter($value)));
                     }
                 }
             }
         }
     }
     return $dom;
 }
 /**
  * Checks if the definition is inlineable.
  *
  * @param ContainerBuilder $container
  * @param string           $id
  * @param Definition       $definition
  *
  * @return bool If the definition is inlineable
  */
 private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
 {
     if (ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
         return true;
     }
     if ($definition->isPublic() || $definition->isLazy()) {
         return false;
     }
     if (!$this->graph->hasNode($id)) {
         return true;
     }
     if ($this->currentId == $id) {
         return false;
     }
     $ids = array();
     foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
         $ids[] = $edge->getSourceNode()->getId();
     }
     if (count(array_unique($ids)) > 1) {
         return false;
     }
     if (count($ids) > 1 && $definition->getFactoryService()) {
         return false;
     }
     return $container->getDefinition(reset($ids))->getScope() === $definition->getScope();
 }
 /**
  * Gets a service definition as PHP array.
  *
  * @param \Symfony\Component\DependencyInjection\Definition $definition
  *   The definition to process.
  *
  * @return array
  *   The service definition as PHP array.
  */
 protected function getServiceDefinition($definition)
 {
     $service = array();
     if ($definition->getClass()) {
         $service['class'] = $definition->getClass();
     }
     if (!$definition->isPublic()) {
         $service['public'] = FALSE;
     }
     /*
         $tagsCode = '';
         foreach ($definition->getTags() as $name => $tags) {
           foreach ($tags as $attributes) {
             $att = array();
             foreach ($attributes as $key => $value) {
               $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
             }
             $att = $att ? ', '.implode(', ', $att) : '';
     
             $tagsCode .= sprintf("      - { name: %s%s }\n", $this->dumper->dump($name), $att);
           }
         }
         if ($tagsCode) {
           $code .= "    tags:\n".$tagsCode;
         }
     */
     if ($definition->getFile()) {
         $service['file'] = $definition->getFile();
     }
     if ($definition->isSynthetic()) {
         $service['synthetic'] = TRUE;
     }
     if ($definition->isLazy()) {
         $service['lazy'] = TRUE;
     }
     if ($definition->getArguments()) {
         $service['arguments'] = $this->dumpValue($definition->getArguments());
     }
     if ($definition->getProperties()) {
         $service['properties'] = $this->dumpValue($definition->getProperties());
     }
     if ($definition->getMethodCalls()) {
         $service['calls'] = $this->dumpValue($definition->getMethodCalls());
     }
     if (($scope = $definition->getScope()) !== ContainerInterface::SCOPE_CONTAINER) {
         $service['scope'] = $scope;
     }
     if (($decorated = $definition->getDecoratedService()) !== NULL) {
         $service['decorates'] = $decorated;
     }
     if ($callable = $definition->getFactory()) {
         $service['factory'] = $this->dumpCallable($callable);
     }
     if ($callable = $definition->getConfigurator()) {
         $service['configurator'] = $this->dumpCallable($callable);
     }
     return $service;
 }
Example #16
0
 /**
  * Adds a service.
  *
  * @param string     $id
  * @param Definition $definition
  *
  * @return string
  */
 private function addService($id, $definition)
 {
     $code = "    {$id}:\n";
     if ($class = $definition->getClass()) {
         if ('\\' === substr($class, 0, 1)) {
             $class = substr($class, 1);
         }
         $code .= sprintf("        class: %s\n", $this->dumper->dump($class));
     }
     if (!$definition->isPublic()) {
         $code .= "        public: false\n";
     }
     $tagsCode = '';
     foreach ($definition->getTags() as $name => $tags) {
         foreach ($tags as $attributes) {
             $att = array();
             foreach ($attributes as $key => $value) {
                 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
             }
             $att = $att ? ', ' . implode(', ', $att) : '';
             $tagsCode .= sprintf("            - { name: %s%s }\n", $this->dumper->dump($name), $att);
         }
     }
     if ($tagsCode) {
         $code .= "        tags:\n" . $tagsCode;
     }
     if ($definition->getFile()) {
         $code .= sprintf("        file: %s\n", $this->dumper->dump($definition->getFile()));
     }
     if ($definition->isSynthetic()) {
         $code .= sprintf("        synthetic: true\n");
     }
     if ($definition->isSynchronized(false)) {
         $code .= sprintf("        synchronized: true\n");
     }
     if ($definition->isDeprecated()) {
         $code .= sprintf("        deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
     }
     if ($definition->isAutowired()) {
         $code .= "        autowire: true\n";
     }
     $autowiringTypesCode = '';
     foreach ($definition->getAutowiringTypes() as $autowiringType) {
         $autowiringTypesCode .= sprintf("            - %s\n", $this->dumper->dump($autowiringType));
     }
     if ($autowiringTypesCode) {
         $code .= sprintf("        autowiring_types:\n%s", $autowiringTypesCode);
     }
     if ($definition->getFactoryClass(false)) {
         $code .= sprintf("        factory_class: %s\n", $this->dumper->dump($definition->getFactoryClass(false)));
     }
     if ($definition->isLazy()) {
         $code .= sprintf("        lazy: true\n");
     }
     if ($definition->getFactoryMethod(false)) {
         $code .= sprintf("        factory_method: %s\n", $this->dumper->dump($definition->getFactoryMethod(false)));
     }
     if ($definition->getFactoryService(false)) {
         $code .= sprintf("        factory_service: %s\n", $this->dumper->dump($definition->getFactoryService(false)));
     }
     if ($definition->getArguments()) {
         $code .= sprintf("        arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
     }
     if ($definition->getProperties()) {
         $code .= sprintf("        properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
     }
     if ($definition->getMethodCalls()) {
         $code .= sprintf("        calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
     }
     if (!$definition->isShared()) {
         $code .= "        shared: false\n";
     }
     if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope(false))) {
         $code .= sprintf("        scope: %s\n", $this->dumper->dump($scope));
     }
     if (null !== ($decorated = $definition->getDecoratedService())) {
         list($decorated, $renamedId, $priority) = $decorated;
         $code .= sprintf("        decorates: %s\n", $decorated);
         if (null !== $renamedId) {
             $code .= sprintf("        decoration_inner_name: %s\n", $renamedId);
         }
         if (0 !== $priority) {
             $code .= sprintf("        decoration_priority: %s\n", $priority);
         }
     }
     if ($callable = $definition->getFactory()) {
         $code .= sprintf("        factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
     }
     if ($callable = $definition->getConfigurator()) {
         $code .= sprintf("        configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
     }
     return $code;
 }
Example #17
0
 /**
  * Adds a service.
  *
  * @param Definition  $definition
  * @param string      $id
  * @param \DOMElement $parent
  */
 private function addService($definition, $id, \DOMElement $parent)
 {
     $service = $this->document->createElement('service');
     if (null !== $id) {
         $service->setAttribute('id', $id);
     }
     if ($class = $definition->getClass()) {
         if ('\\' === substr($class, 0, 1)) {
             $class = substr($class, 1);
         }
         $service->setAttribute('class', $class);
     }
     if (!$definition->isShared()) {
         $service->setAttribute('shared', 'false');
     }
     if (!$definition->isPublic()) {
         $service->setAttribute('public', 'false');
     }
     if ($definition->isSynthetic()) {
         $service->setAttribute('synthetic', 'true');
     }
     if ($definition->isLazy()) {
         $service->setAttribute('lazy', 'true');
     }
     if (null !== ($decorated = $definition->getDecoratedService())) {
         list($decorated, $renamedId, $priority) = $decorated;
         $service->setAttribute('decorates', $decorated);
         if (null !== $renamedId) {
             $service->setAttribute('decoration-inner-name', $renamedId);
         }
         if (0 !== $priority) {
             $service->setAttribute('decoration-priority', $priority);
         }
     }
     foreach ($definition->getTags() as $name => $tags) {
         foreach ($tags as $attributes) {
             $tag = $this->document->createElement('tag');
             $tag->setAttribute('name', $name);
             foreach ($attributes as $key => $value) {
                 $tag->setAttribute($key, $value);
             }
             $service->appendChild($tag);
         }
     }
     if ($definition->getFile()) {
         $file = $this->document->createElement('file');
         $file->appendChild($this->document->createTextNode($definition->getFile()));
         $service->appendChild($file);
     }
     if ($parameters = $definition->getArguments()) {
         $this->convertParameters($parameters, 'argument', $service);
     }
     if ($parameters = $definition->getProperties()) {
         $this->convertParameters($parameters, 'property', $service, 'name');
     }
     $this->addMethodCalls($definition->getMethodCalls(), $service);
     if ($callable = $definition->getFactory()) {
         $factory = $this->document->createElement('factory');
         if (is_array($callable) && $callable[0] instanceof Definition) {
             $this->addService($callable[0], null, $factory);
             $factory->setAttribute('method', $callable[1]);
         } elseif (is_array($callable)) {
             $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
             $factory->setAttribute('method', $callable[1]);
         } else {
             $factory->setAttribute('function', $callable);
         }
         $service->appendChild($factory);
     }
     if ($definition->isDeprecated()) {
         $deprecated = $this->document->createElement('deprecated');
         $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
         $service->appendChild($deprecated);
     }
     if ($definition->isAutowired()) {
         $service->setAttribute('autowire', 'true');
     }
     foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
         $autowiringType = $this->document->createElement('autowiring-type');
         $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
         $service->appendChild($autowiringType);
     }
     if ($callable = $definition->getConfigurator()) {
         $configurator = $this->document->createElement('configurator');
         if (is_array($callable) && $callable[0] instanceof Definition) {
             $this->addService($callable[0], null, $configurator);
             $configurator->setAttribute('method', $callable[1]);
         } elseif (is_array($callable)) {
             $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
             $configurator->setAttribute('method', $callable[1]);
         } else {
             $configurator->setAttribute('function', $callable);
         }
         $service->appendChild($configurator);
     }
     $parent->appendChild($service);
 }
Example #18
0
 /**
  * Adds a service.
  *
  * @param Definition  $definition
  * @param string      $id
  * @param \DOMElement $parent
  */
 private function addService($definition, $id, \DOMElement $parent)
 {
     $service = $this->document->createElement('service');
     if (null !== $id) {
         $service->setAttribute('id', $id);
     }
     if ($class = $definition->getClass()) {
         if ('\\' === substr($class, 0, 1)) {
             $class = substr($class, 1);
         }
         $service->setAttribute('class', $class);
     }
     if ($definition->getFactoryMethod(false)) {
         $service->setAttribute('factory-method', $definition->getFactoryMethod(false));
     }
     if ($definition->getFactoryClass(false)) {
         $service->setAttribute('factory-class', $definition->getFactoryClass(false));
     }
     if ($definition->getFactoryService(false)) {
         $service->setAttribute('factory-service', $definition->getFactoryService(false));
     }
     if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) {
         $service->setAttribute('scope', $scope);
     }
     if (!$definition->isPublic()) {
         $service->setAttribute('public', 'false');
     }
     if ($definition->isSynthetic()) {
         $service->setAttribute('synthetic', 'true');
     }
     if ($definition->isSynchronized(false)) {
         $service->setAttribute('synchronized', 'true');
     }
     if ($definition->isLazy()) {
         $service->setAttribute('lazy', 'true');
     }
     if (null !== ($decorated = $definition->getDecoratedService())) {
         list($decorated, $renamedId) = $decorated;
         $service->setAttribute('decorates', $decorated);
         if (null !== $renamedId) {
             $service->setAttribute('decoration-inner-name', $renamedId);
         }
     }
     foreach ($definition->getTags() as $name => $tags) {
         foreach ($tags as $attributes) {
             $tag = $this->document->createElement('tag');
             $tag->setAttribute('name', $name);
             foreach ($attributes as $key => $value) {
                 $tag->setAttribute($key, $value);
             }
             $service->appendChild($tag);
         }
     }
     if ($definition->getFile()) {
         $file = $this->document->createElement('file');
         $file->appendChild($this->document->createTextNode($definition->getFile()));
         $service->appendChild($file);
     }
     if ($parameters = $definition->getArguments()) {
         $this->convertParameters($parameters, 'argument', $service);
     }
     if ($parameters = $definition->getProperties()) {
         $this->convertParameters($parameters, 'property', $service, 'name');
     }
     $this->addMethodCalls($definition->getMethodCalls(), $service);
     if ($callable = $definition->getFactory()) {
         $factory = $this->document->createElement('factory');
         if (is_array($callable) && $callable[0] instanceof Definition) {
             $this->addService($callable[0], null, $factory);
             $factory->setAttribute('method', $callable[1]);
         } elseif (is_array($callable)) {
             $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
             $factory->setAttribute('method', $callable[1]);
         } else {
             $factory->setAttribute('function', $callable);
         }
         $service->appendChild($factory);
     }
     if ($callable = $definition->getConfigurator()) {
         $configurator = $this->document->createElement('configurator');
         if (is_array($callable) && $callable[0] instanceof Definition) {
             $this->addService($callable[0], null, $configurator);
             $configurator->setAttribute('method', $callable[1]);
         } elseif (is_array($callable)) {
             $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
             $configurator->setAttribute('method', $callable[1]);
         } else {
             $configurator->setAttribute('function', $callable);
         }
         $service->appendChild($configurator);
     }
     $parent->appendChild($service);
 }
 /**
  * Validates the service definition.
  *
  * An exception is thrown if the following conditions are not met:
  *
  * - The service is public.
  * - The service is not abstract.
  * - The service implements an interface or is a subclass.
  *
  * @param ContainerBuilder $container  The container builder.
  * @param Definition       $definition The service definition.
  * @param string           $class      The FQIN or FQCN.
  * @param string           $id         The service identifier.
  *
  * @throws ServiceException If the definition is not valid.
  */
 private function validateDefinition(ContainerBuilder $container, Definition $definition, $class, $id)
 {
     if ($definition->isAbstract()) {
         throw new ServiceException("The service definition \"{$id}\" is abstract.");
     }
     if (!$definition->isPublic()) {
         throw new ServiceException("The service definition \"{$id}\" is private.");
     }
     $reflection = new ReflectionClass($container->getParameterBag()->resolveValue($definition->getClass()));
     if ($reflection->isInterface() || !$reflection->isSubclassOf($class)) {
         $expected = new ReflectionClass($class);
         if ($expected->isInterface() || $class !== $reflection->getName()) {
             throw new ServiceException(sprintf('The service definition "%s" does not implement or extend "%s".', $id, $class));
         }
     }
 }
 /**
  * Checks if the definition is inlineable.
  *
  * @param string     $id
  * @param Definition $definition
  *
  * @return bool If the definition is inlineable
  */
 private function isInlineableDefinition($id, Definition $definition)
 {
     if (!$definition->isShared()) {
         return true;
     }
     if ($definition->isPublic() || $definition->isLazy()) {
         return false;
     }
     if (!$this->graph->hasNode($id)) {
         return true;
     }
     if ($this->currentId == $id) {
         return false;
     }
     $ids = array();
     foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
         $ids[] = $edge->getSourceNode()->getId();
     }
     if (count(array_unique($ids)) > 1) {
         return false;
     }
     if (count($ids) > 1 && is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
         return false;
     }
     return true;
 }
Example #21
0
    /**
     * Adds a service.
     *
     * @param string     $id
     * @param Definition $definition
     *
     * @return string
     */
    private function addService($id, $definition)
    {
        $this->definitionVariables = new \SplObjectStorage();
        $this->referenceVariables = array();
        $this->variableCount = 0;

        $return = array();

        if ($definition->isSynthetic()) {
            $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
        } elseif ($class = $definition->getClass()) {
            $return[] = sprintf('@return %s A %s instance.', 0 === strpos($class, '%') ? 'object' : '\\'.ltrim($class, '\\'), ltrim($class, '\\'));
        } elseif ($definition->getFactory()) {
            $factory = $definition->getFactory();
            if (is_string($factory)) {
                $return[] = sprintf('@return object An instance returned by %s().', $factory);
            } elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
                if (is_string($factory[0]) || $factory[0] instanceof Reference) {
                    $return[] = sprintf('@return object An instance returned by %s::%s().', (string) $factory[0], $factory[1]);
                } elseif ($factory[0] instanceof Definition) {
                    $return[] = sprintf('@return object An instance returned by %s::%s().', $factory[0]->getClass(), $factory[1]);
                }
            }
        }

        $return = implode("\n     * ", $return);

        $doc = '';
        if ($definition->isShared()) {
            $doc .= <<<EOF

     *
     * This service is shared.
     * This method always returns the same instance of the service.
EOF;
        }

        if (!$definition->isPublic()) {
            $doc .= <<<EOF

     *
     * This service is private.
     * If you want to be able to request this service from the container directly,
     * make it public, otherwise you might end up with broken code.
EOF;
        }

        if ($definition->isLazy()) {
            $lazyInitialization = '$lazyLoad = true';
            $lazyInitializationDoc = "\n     * @param bool    \$lazyLoad whether to try lazy-loading the service with a proxy\n     *";
        } else {
            $lazyInitialization = '';
            $lazyInitializationDoc = '';
        }

        // with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
        $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
        $visibility = $isProxyCandidate ? 'public' : 'protected';
        $code = <<<EOF

    /**
     * Gets the '$id' service.$doc
     *$lazyInitializationDoc
     * $return
     */
    {$visibility} function get{$this->camelize($id)}Service($lazyInitialization)
    {

EOF;

        $code .= $isProxyCandidate ? $this->getProxyDumper()->getProxyFactoryCode($definition, $id) : '';

        if ($definition->isSynthetic()) {
            $code .= sprintf("        throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n    }\n", $id);
        } else {
            $code .=
                $this->addServiceInclude($id, $definition).
                $this->addServiceLocalTempVariables($id, $definition).
                $this->addServiceInlinedDefinitions($id, $definition).
                $this->addServiceInstance($id, $definition).
                $this->addServiceInlinedDefinitionsSetup($id, $definition).
                $this->addServiceMethodCalls($id, $definition).
                $this->addServiceProperties($id, $definition).
                $this->addServiceConfigurator($id, $definition).
                $this->addServiceReturn($id, $definition)
            ;
        }

        $this->definitionVariables = null;
        $this->referenceVariables = null;

        return $code;
    }
Example #22
0
 /**
  * @param Definition $definition
  * @param bool       $omitTags
  *
  * @return array
  */
 private function getContainerDefinitionData(Definition $definition, $omitTags = false)
 {
     $data = array('class' => (string) $definition->getClass(), 'scope' => $definition->getScope(), 'public' => $definition->isPublic(), 'synthetic' => $definition->isSynthetic(), 'lazy' => $definition->isLazy(), 'synchronized' => $definition->isSynchronized(), 'abstract' => $definition->isAbstract(), 'file' => $definition->getFile());
     if ($definition->getFactoryClass()) {
         $data['factory_class'] = $definition->getFactoryClass();
     }
     if ($definition->getFactoryService()) {
         $data['factory_service'] = $definition->getFactoryService();
     }
     if ($definition->getFactoryMethod()) {
         $data['factory_method'] = $definition->getFactoryMethod();
     }
     if ($factory = $definition->getFactory()) {
         if (is_array($factory)) {
             if ($factory[0] instanceof Reference) {
                 $data['factory_service'] = (string) $factory[0];
             } elseif ($factory[0] instanceof Definition) {
                 throw new \InvalidArgumentException('Factory is not describable.');
             } else {
                 $data['factory_class'] = $factory[0];
             }
             $data['factory_method'] = $factory[1];
         } else {
             $data['factory_function'] = $factory;
         }
     }
     if (!$omitTags) {
         $data['tags'] = array();
         if (count($definition->getTags())) {
             foreach ($definition->getTags() as $tagName => $tagData) {
                 foreach ($tagData as $parameters) {
                     $data['tags'][] = array('name' => $tagName, 'parameters' => $parameters);
                 }
             }
         }
     }
     return $data;
 }
Example #23
0
 /**
  * @param Definition  $definition
  * @param string|null $id
  * @param bool        $omitTags
  *
  * @return \DOMDocument
  */
 private function getContainerDefinitionDocument(Definition $definition, $id = null, $omitTags = false)
 {
     $dom = new \DOMDocument('1.0', 'UTF-8');
     $dom->appendChild($serviceXML = $dom->createElement('definition'));
     if ($id) {
         $serviceXML->setAttribute('id', $id);
     }
     $serviceXML->setAttribute('class', $definition->getClass());
     if ($definition->getFactoryClass()) {
         $serviceXML->setAttribute('factory-class', $definition->getFactoryClass());
     }
     if ($definition->getFactoryService()) {
         $serviceXML->setAttribute('factory-service', $definition->getFactoryService());
     }
     if ($definition->getFactoryMethod()) {
         $serviceXML->setAttribute('factory-method', $definition->getFactoryMethod());
     }
     if ($factory = $definition->getFactory()) {
         $serviceXML->appendChild($factoryXML = $dom->createElement('factory'));
         if (is_array($factory)) {
             if ($factory[0] instanceof Reference) {
                 $factoryXML->setAttribute('service', (string) $factory[0]);
             } elseif ($factory[0] instanceof Definition) {
                 throw new \InvalidArgumentException('Factory is not describable.');
             } else {
                 $factoryXML->setAttribute('class', $factory[0]);
             }
             $factoryXML->setAttribute('method', $factory[1]);
         } else {
             $factoryXML->setAttribute('function', $factory);
         }
     }
     $serviceXML->setAttribute('scope', $definition->getScope());
     $serviceXML->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
     $serviceXML->setAttribute('synthetic', $definition->isSynthetic() ? 'true' : 'false');
     $serviceXML->setAttribute('lazy', $definition->isLazy() ? 'true' : 'false');
     $serviceXML->setAttribute('synchronized', $definition->isSynchronized() ? 'true' : 'false');
     $serviceXML->setAttribute('abstract', $definition->isAbstract() ? 'true' : 'false');
     $serviceXML->setAttribute('file', $definition->getFile());
     if (!$omitTags) {
         $tags = $definition->getTags();
         if (count($tags) > 0) {
             $serviceXML->appendChild($tagsXML = $dom->createElement('tags'));
             foreach ($tags as $tagName => $tagData) {
                 foreach ($tagData as $parameters) {
                     $tagsXML->appendChild($tagXML = $dom->createElement('tag'));
                     $tagXML->setAttribute('name', $tagName);
                     foreach ($parameters as $name => $value) {
                         $tagXML->appendChild($parameterXML = $dom->createElement('parameter'));
                         $parameterXML->setAttribute('name', $name);
                         $parameterXML->appendChild(new \DOMText($this->formatParameter($value)));
                     }
                 }
             }
         }
     }
     return $dom;
 }
 /**
  * {@inheritdoc}
  */
 protected function describeContainerDefinition(Definition $definition, array $options = array())
 {
     $description = isset($options['id']) ? array($this->formatSection('container', sprintf('Information for service <info>%s</info>', $options['id']))) : array();
     $description[] = sprintf('<comment>Service Id</comment>       %s', isset($options['id']) ? $options['id'] : '-');
     $description[] = sprintf('<comment>Class</comment>            %s', $definition->getClass() ?: "-");
     $tags = $definition->getTags();
     if (count($tags)) {
         $description[] = '<comment>Tags</comment>';
         foreach ($tags as $tagName => $tagData) {
             foreach ($tagData as $parameters) {
                 $description[] = sprintf('    - %-30s (%s)', $tagName, implode(', ', array_map(function ($key, $value) {
                     return sprintf('<info>%s</info>: %s', $key, $value);
                 }, array_keys($parameters), array_values($parameters))));
             }
         }
     } else {
         $description[] = '<comment>Tags</comment>             -';
     }
     $description[] = sprintf('<comment>Scope</comment>            %s', $definition->getScope());
     $description[] = sprintf('<comment>Public</comment>           %s', $definition->isPublic() ? 'yes' : 'no');
     $description[] = sprintf('<comment>Synthetic</comment>        %s', $definition->isSynthetic() ? 'yes' : 'no');
     $description[] = sprintf('<comment>Required File</comment>    %s', $definition->getFile() ? $definition->getFile() : '-');
     $this->writeText(implode("\n", $description) . "\n", $options);
 }
Example #25
0
 /**
  * {@inheritdoc}
  */
 protected function describeContainerDefinition(Definition $definition, array $options = array())
 {
     if (isset($options['id'])) {
         $options['output']->title(sprintf('Information for Service "<info>%s</info>"', $options['id']));
     }
     $tableHeaders = array('Option', 'Value');
     $tableRows[] = array('Service ID', isset($options['id']) ? $options['id'] : '-');
     $tableRows[] = array('Class', $definition->getClass() ?: '-');
     $tags = $definition->getTags();
     if (count($tags)) {
         $tagInformation = '';
         foreach ($tags as $tagName => $tagData) {
             foreach ($tagData as $tagParameters) {
                 $parameters = array_map(function ($key, $value) {
                     return sprintf('<info>%s</info>: %s', $key, $value);
                 }, array_keys($tagParameters), array_values($tagParameters));
                 $parameters = implode(', ', $parameters);
                 if ('' === $parameters) {
                     $tagInformation .= sprintf('%s', $tagName);
                 } else {
                     $tagInformation .= sprintf('%s (%s)', $tagName, $parameters);
                 }
             }
         }
     } else {
         $tagInformation = '-';
     }
     $tableRows[] = array('Tags', $tagInformation);
     $tableRows[] = array('Scope', $definition->getScope(false));
     $tableRows[] = array('Public', $definition->isPublic() ? 'yes' : 'no');
     $tableRows[] = array('Synthetic', $definition->isSynthetic() ? 'yes' : 'no');
     $tableRows[] = array('Lazy', $definition->isLazy() ? 'yes' : 'no');
     if (method_exists($definition, 'isSynchronized')) {
         $tableRows[] = array('Synchronized', $definition->isSynchronized(false) ? 'yes' : 'no');
     }
     $tableRows[] = array('Abstract', $definition->isAbstract() ? 'yes' : 'no');
     if (method_exists($definition, 'isAutowired')) {
         $tableRows[] = array('Autowired', $definition->isAutowired() ? 'yes' : 'no');
         $autowiringTypes = $definition->getAutowiringTypes();
         if (count($autowiringTypes)) {
             $autowiringTypesInformation = implode(', ', $autowiringTypes);
         } else {
             $autowiringTypesInformation = '-';
         }
         $tableRows[] = array('Autowiring Types', $autowiringTypesInformation);
     }
     if ($definition->getFile()) {
         $tableRows[] = array('Required File', $definition->getFile() ? $definition->getFile() : '-');
     }
     if ($definition->getFactoryClass(false)) {
         $tableRows[] = array('Factory Class', $definition->getFactoryClass(false));
     }
     if ($definition->getFactoryService(false)) {
         $tableRows[] = array('Factory Service', $definition->getFactoryService(false));
     }
     if ($definition->getFactoryMethod(false)) {
         $tableRows[] = array('Factory Method', $definition->getFactoryMethod(false));
     }
     if ($factory = $definition->getFactory()) {
         if (is_array($factory)) {
             if ($factory[0] instanceof Reference) {
                 $tableRows[] = array('Factory Service', $factory[0]);
             } elseif ($factory[0] instanceof Definition) {
                 throw new \InvalidArgumentException('Factory is not describable.');
             } else {
                 $tableRows[] = array('Factory Class', $factory[0]);
             }
             $tableRows[] = array('Factory Method', $factory[1]);
         } else {
             $tableRows[] = array('Factory Function', $factory);
         }
     }
     $options['output']->table($tableHeaders, $tableRows);
 }
Example #26
0
    /**
     * Adds a service
     *
     * @param string     $id
     * @param Definition $definition
     *
     * @return string
     */
    private function addService($id, $definition)
    {
        $name = Container::camelize($id);
        $this->definitionVariables = new \SplObjectStorage();
        $this->referenceVariables = array();
        $this->variableCount = 0;
        $return = '';
        if ($definition->isSynthetic()) {
            $return = sprintf('@throws RuntimeException always since this service is expected to be injected dynamically');
        } elseif ($class = $definition->getClass()) {
            $return = sprintf("@return %s A %s instance.", 0 === strpos($class, '%') ? 'Object' : $class, $class);
        } elseif ($definition->getFactoryClass()) {
            $return = sprintf('@return Object An instance returned by %s::%s().', $definition->getFactoryClass(), $definition->getFactoryMethod());
        } elseif ($definition->getFactoryService()) {
            $return = sprintf('@return Object An instance returned by %s::%s().', $definition->getFactoryService(), $definition->getFactoryMethod());
        }
        $doc = '';
        if (ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope()) {
            $doc .= <<<EOF

     *
     * This service is shared.
     * This method always returns the same instance of the service.
EOF;
        }
        if (!$definition->isPublic()) {
            $doc .= <<<EOF

     *
     * This service is private.
     * If you want to be able to request this service from the container directly,
     * make it public, otherwise you might end up with broken code.
EOF;
        }
        $code = <<<EOF

    /**
     * Gets the '{$id}' service.{$doc}
     *
     * {$return}
     */
    protected function get{$name}Service()
    {

EOF;
        $scope = $definition->getScope();
        if (ContainerInterface::SCOPE_CONTAINER !== $scope && ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
            $code .= <<<EOF
        if (!isset(\$this->scopedServices['{$scope}'])) {
            throw new InactiveScopeException('{$id}', '{$scope}');
        }


EOF;
        }
        if ($definition->isSynthetic()) {
            $code .= sprintf("        throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n    }\n", $id);
        } else {
            $code .= $this->addServiceInclude($id, $definition) . $this->addServiceLocalTempVariables($id, $definition) . $this->addServiceInlinedDefinitions($id, $definition) . $this->addServiceInstance($id, $definition) . $this->addServiceInlinedDefinitionsSetup($id, $definition) . $this->addServiceMethodCalls($id, $definition) . $this->addServiceProperties($id, $definition) . $this->addServiceConfigurator($id, $definition) . $this->addServiceReturn($id, $definition);
        }
        $this->definitionVariables = null;
        $this->referenceVariables = null;
        return $code;
    }
 /**
  * {@inheritdoc}
  */
 protected function describeContainerDefinition(Definition $definition, array $options = array())
 {
     $output = '- Class: `' . $definition->getClass() . '`' . "\n" . '- Scope: `' . $definition->getScope() . '`' . "\n" . '- Public: ' . ($definition->isPublic() ? 'yes' : 'no') . "\n" . '- Synthetic: ' . ($definition->isSynthetic() ? 'yes' : 'no');
     if ($definition->getFile()) {
         $output .= "\n" . '- File: `' . $definition->getFile() . '`';
     }
     if (!(isset($options['omit_tags']) && $options['omit_tags'])) {
         foreach ($definition->getTags() as $tagName => $tagData) {
             foreach ($tagData as $parameters) {
                 $output .= "\n" . '- Tag: `' . $tagName . '`';
                 foreach ($parameters as $name => $value) {
                     $output .= "\n" . '    - ' . ucfirst($name) . ': ' . $value;
                 }
             }
         }
     }
     $this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
 }
 /**
  * Gets a service definition as PHP array.
  *
  * @param \Symfony\Component\DependencyInjection\Definition $definition
  *   The definition to process.
  *
  * @return array
  *   The service definition as PHP array.
  *
  * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  *   Thrown when the definition is marked as decorated, or with an explicit
  *   scope different from SCOPE_CONTAINER and SCOPE_PROTOTYPE.
  */
 protected function getServiceDefinition(Definition $definition)
 {
     $service = array();
     if ($definition->getClass()) {
         $service['class'] = $definition->getClass();
     }
     if (!$definition->isPublic()) {
         $service['public'] = FALSE;
     }
     if ($definition->getFile()) {
         $service['file'] = $definition->getFile();
     }
     if ($definition->isSynthetic()) {
         $service['synthetic'] = TRUE;
     }
     if ($definition->isLazy()) {
         $service['lazy'] = TRUE;
     }
     if ($definition->getArguments()) {
         $arguments = $definition->getArguments();
         $service['arguments'] = $this->dumpCollection($arguments);
         $service['arguments_count'] = count($arguments);
     } else {
         $service['arguments_count'] = 0;
     }
     if ($definition->getProperties()) {
         $service['properties'] = $this->dumpCollection($definition->getProperties());
     }
     if ($definition->getMethodCalls()) {
         $service['calls'] = $this->dumpMethodCalls($definition->getMethodCalls());
     }
     if (($scope = $definition->getScope()) !== ContainerInterface::SCOPE_CONTAINER) {
         if ($scope === ContainerInterface::SCOPE_PROTOTYPE) {
             // Scope prototype has been replaced with 'shared' => FALSE.
             // This is a Symfony 2.8 forward compatibility fix.
             // Reference: https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md#dependencyinjection
             $service['shared'] = FALSE;
         } else {
             throw new InvalidArgumentException("The 'scope' definition is deprecated in Symfony 3.0 and not supported by Drupal 8.");
         }
     }
     // By default services are shared, so just provide the flag, when needed.
     if ($definition->isShared() === FALSE) {
         $service['shared'] = $definition->isShared();
     }
     if (($decorated = $definition->getDecoratedService()) !== NULL) {
         throw new InvalidArgumentException("The 'decorated' definition is not supported by the Drupal 8 run-time container. The Container Builder should have resolved that during the DecoratorServicePass compiler pass.");
     }
     if ($callable = $definition->getFactory()) {
         $service['factory'] = $this->dumpCallable($callable);
     }
     if ($callable = $definition->getConfigurator()) {
         $service['configurator'] = $this->dumpCallable($callable);
     }
     return $service;
 }
Example #29
0
 /**
  * Adds a service.
  *
  * @param Definition  $definition
  * @param string      $id
  * @param \DOMElement $parent
  */
 private function addService($definition, $id, \DOMElement $parent)
 {
     $service = $this->document->createElement('service');
     if (null !== $id) {
         $service->setAttribute('id', $id);
     }
     if ($definition->getClass()) {
         $service->setAttribute('class', $definition->getClass());
     }
     if ($definition->getFactoryMethod()) {
         $service->setAttribute('factory-method', $definition->getFactoryMethod());
     }
     if ($definition->getFactoryService()) {
         $service->setAttribute('factory-service', $definition->getFactoryService());
     }
     if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) {
         $service->setAttribute('scope', $scope);
     }
     if (!$definition->isPublic()) {
         $service->setAttribute('public', 'false');
     }
     foreach ($definition->getTags() as $name => $tags) {
         foreach ($tags as $attributes) {
             $tag = $this->document->createElement('tag');
             $tag->setAttribute('name', $name);
             foreach ($attributes as $key => $value) {
                 $tag->setAttribute($key, $value);
             }
             $service->appendChild($tag);
         }
     }
     if ($definition->getFile()) {
         $file = $this->document->createElement('file');
         $file->appendChild($this->document->createTextNode($definition->getFile()));
         $service->appendChild($file);
     }
     if ($parameters = $definition->getArguments()) {
         $this->convertParameters($parameters, 'argument', $service);
     }
     if ($parameters = $definition->getProperties()) {
         $this->convertParameters($parameters, 'property', $service, 'name');
     }
     $this->addMethodCalls($definition->getMethodCalls(), $service);
     if ($callable = $definition->getConfigurator()) {
         $configurator = $this->document->createElement('configurator');
         if (is_array($callable)) {
             $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
             $configurator->setAttribute('method', $callable[1]);
         } else {
             $configurator->setAttribute('function', $callable);
         }
         $service->appendChild($configurator);
     }
     $parent->appendChild($service);
 }
Example #30
0
 /**
  * {@inheritdoc}
  */
 protected function describeContainerDefinition(Definition $definition, array $options = array())
 {
     $output = '- Class: `' . $definition->getClass() . '`' . "\n" . '- Scope: `' . $definition->getScope() . '`' . "\n" . '- Public: ' . ($definition->isPublic() ? 'yes' : 'no') . "\n" . '- Synthetic: ' . ($definition->isSynthetic() ? 'yes' : 'no') . "\n" . '- Lazy: ' . ($definition->isLazy() ? 'yes' : 'no') . "\n" . '- Synchronized: ' . ($definition->isSynchronized() ? 'yes' : 'no') . "\n" . '- Abstract: ' . ($definition->isAbstract() ? 'yes' : 'no');
     if ($definition->getFile()) {
         $output .= "\n" . '- File: `' . $definition->getFile() . '`';
     }
     if ($definition->getFactoryClass()) {
         $output .= "\n" . '- Factory Class: `' . $definition->getFactoryClass() . '`';
     }
     if ($definition->getFactoryService()) {
         $output .= "\n" . '- Factory Service: `' . $definition->getFactoryService() . '`';
     }
     if ($definition->getFactoryMethod()) {
         $output .= "\n" . '- Factory Method: `' . $definition->getFactoryMethod() . '`';
     }
     if ($factory = $definition->getFactory()) {
         if (is_array($factory)) {
             if ($factory[0] instanceof Reference) {
                 $output .= "\n" . '- Factory Service: `' . $factory[0] . '`';
             } elseif ($factory[0] instanceof Definition) {
                 throw new \InvalidArgumentException('Factory is not describable.');
             } else {
                 $output .= "\n" . '- Factory Class: `' . $factory[0] . '`';
             }
             $output .= "\n" . '- Factory Method: `' . $factory[1] . '`';
         } else {
             $output .= "\n" . '- Factory Function: `' . $factory . '`';
         }
     }
     if (!(isset($options['omit_tags']) && $options['omit_tags'])) {
         foreach ($definition->getTags() as $tagName => $tagData) {
             foreach ($tagData as $parameters) {
                 $output .= "\n" . '- Tag: `' . $tagName . '`';
                 foreach ($parameters as $name => $value) {
                     $output .= "\n" . '    - ' . ucfirst($name) . ': ' . $value;
                 }
             }
         }
     }
     $this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
 }