Completed
Pull Request — master (#243)
by Guilh
02:34
created

Metadata/MetadataConverter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\DiExtraBundle\Metadata;
20
21
use JMS\DiExtraBundle\Exception\InvalidAnnotationException;
22
use Symfony\Component\DependencyInjection\Reference;
23
use Symfony\Component\DependencyInjection\DefinitionDecorator;
24
use Symfony\Component\DependencyInjection\Definition;
25
use Metadata\ClassHierarchyMetadata;
26
27
class MetadataConverter
28
{
29
    /**
30
     * Converts class hierarchy metadata to definition instances.
31
     *
32
     * @param ClassHierarchyMetadata $metadata
33
     * @return array an array of Definition instances
34
     */
35
    public function convert(ClassHierarchyMetadata $metadata)
36
    {
37
        static $count = 0;
38
        $definitions = array();
39
40
        $previous = null;
41
        foreach ($metadata->classMetadata as $classMetadata) {
42
            if (null === $previous && null === $classMetadata->parent) {
43
                $definition = new Definition();
44
            } else {
45
                $definition = new DefinitionDecorator(
46
                    $classMetadata->parent ?: $previous->id
47
                );
48
            }
49
50
            $definition->setClass($classMetadata->name);
51
            if (null !== $classMetadata->scope) {
52
                $definition->setScope($classMetadata->scope);
53
            }
54
            if (null !== $classMetadata->public) {
55
                $definition->setPublic($classMetadata->public);
56
            }
57
            if (null !== $classMetadata->abstract) {
58
                $definition->setAbstract($classMetadata->abstract);
59
            }
60
            if (null !== $classMetadata->arguments) {
61
                $definition->setArguments($classMetadata->arguments);
62
            }
63
64
            $definition->setMethodCalls($classMetadata->methodCalls);
65
            $definition->setTags($classMetadata->tags);
66
            $definition->setProperties($classMetadata->properties);
67
68
            if (null !== $classMetadata->decorates) {
69
                if (!method_exists($definition, 'setDecoratedService')) {
70
                    throw new InvalidAnnotationException(
71
                        sprintf(
72
                            "decorations require symfony >=2.8 on class %s",
73
                            $classMetadata->name
74
                        )
75
                    );
76
                }
77
78
                $definition->setDecoratedService($classMetadata->decorates, $classMetadata->decoration_inner_name);
79
            }
80
81
            if (null !== $classMetadata->deprecated && method_exists($definition, 'setDeprecated')) {
82
                $definition->setDeprecated(true, $classMetadata->deprecated);
83
            }
84
85
            if (null === $classMetadata->id) {
86
                $classMetadata->id = '_jms_di_extra.unnamed.service_'.$count++;
87
            }
88
89
            if (0 !== count($classMetadata->initMethods)) {
90
                foreach ($classMetadata->initMethods as $initMethod) {
91
                    $definition->addMethodCall($initMethod);
92
                }
93
            } elseif (null !== $classMetadata->initMethod) {
94
                @trigger_error('ClassMetadata::$initMethod is deprecated since version 1.7 and will be removed in 2.0. Use ClassMetadata::$initMethods instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
                $definition->addMethodCall($classMetadata->initMethod);
96
            }
97
98
            $definitions[$classMetadata->id] = $definition;
99
            $previous = $classMetadata;
100
        }
101
102
        return $definitions;
103
    }
104
}
105