Passed
Pull Request — master (#1108)
by Tim
06:37
created

ObjectManager::getDirectories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * \AppserverIo\Appserver\DependencyInjectionContainer\ObjectManager
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @author    Bernhard Wick <[email protected]>
16
 * @copyright 2015 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/appserver-io/appserver
19
 * @link      http://www.appserver.io
20
 */
21
22
namespace AppserverIo\Appserver\DependencyInjectionContainer;
23
24
use AppserverIo\Storage\StorageInterface;
25
use AppserverIo\Appserver\Core\AbstractManager;
26
use AppserverIo\Psr\Di\ObjectManagerInterface;
27
use AppserverIo\Psr\Di\UnknownObjectDescriptorException;
28
use AppserverIo\Psr\Deployment\DescriptorInterface;
29
use AppserverIo\Psr\Application\ApplicationInterface;
30
31
/**
32
 * The object manager is necessary to load and provides information about all
33
 * objects related with the application itself.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @author    Bernhard Wick <[email protected]>
37
 * @copyright 2015 TechDivision GmbH <[email protected]>
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      https://github.com/appserver-io/appserver
40
 * @link      http://www.appserver.io
41
 *
42
 * @property \AppserverIo\Storage\StorageInterface $objectDescriptors Storage for our collected object descriptors
43
 */
44
class ObjectManager extends AbstractManager implements ObjectManagerInterface
45
{
46
47
    /**
48
     * Inject the storage for the object descriptors.
49
     *
50
     * @param \AppserverIo\Storage\StorageInterface $objectDescriptors The storage for the object descriptors
51
     *
52
     * @return void
53
     */
54
    public function injectObjectDescriptors(StorageInterface $objectDescriptors)
55
    {
56
        $this->objectDescriptors = $objectDescriptors;
57
    }
58
59
    /**
60
     * Returns the storage with the object descriptors.
61
     *
62
     * @return \AppserverIo\Storage\StorageInterface The storage with the object descriptors
63
     */
64
    public function getObjectDescriptors()
65
    {
66
        return $this->objectDescriptors;
67
    }
68
69
    /**
70
     * Has been automatically invoked by the container after the application
71
     * instance has been created.
72
     *
73
     * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
74
     *
75
     * @return void
76
     * @see \AppserverIo\Psr\Application\ManagerInterface::initialize()
77
     */
78
    public function initialize(ApplicationInterface $application)
79
    {
80
81
        // parse the object descriptors
82
        $this->parseObjectDescriptors();
83
    }
84
85
    /**
86
     * Adds the passed object descriptor to the object manager. If the merge flag is TRUE, then
87
     * we check if already an object descriptor for the class exists before they will be merged.
88
     *
89
     * When we merge object descriptors this means, that the values of the passed descriptor
90
     * will override the existing ones.
91
     *
92
     * @param \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor The object descriptor to add
93
     * @param boolean                                         $merge            TRUE if we want to merge with an existing object descriptor
94
     *
95
     * @return void
96
     */
97
    public function addPreference(DescriptorInterface $objectDescriptor, $merge = false)
98
    {
99
100
        // query whether or not an existing preference has to be overwritten
101
        if ($this->hasAttribute($interface = $objectDescriptor->getInterface()) && !$merge) {
0 ignored issues
show
Bug introduced by
The method getInterface() does not exist on AppserverIo\Psr\Deployment\DescriptorInterface. It seems like you code against a sub-type of AppserverIo\Psr\Deployment\DescriptorInterface such as AppserverIo\Description\PreferenceDescriptor. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        if ($this->hasAttribute($interface = $objectDescriptor->/** @scrutinizer ignore-call */ getInterface()) && !$merge) {
Loading history...
102
            return;
103
        }
104
105
        // add the new preference
106
        $this->setAttribute($interface, $objectDescriptor->getClassName());
0 ignored issues
show
Bug introduced by
The method getClassName() does not exist on AppserverIo\Psr\Deployment\DescriptorInterface. It seems like you code against a sub-type of AppserverIo\Psr\Deployment\DescriptorInterface such as AppserverIo\Description\PreferenceDescriptor or AppserverIo\Description\FactoryDescriptor or AppserverIo\Description\BeanDescriptor or AppserverIo\Description\EnterpriseBeanDescriptor or AppserverIo\Description\ServletDescriptor or AppserverIo\Description\PersistenceUnitDescriptor. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        $this->setAttribute($interface, $objectDescriptor->/** @scrutinizer ignore-call */ getClassName());
Loading history...
107
    }
108
109
    /**
110
     * Return's the preference for the passed class name.
111
     *
112
     * @param string $className The class name to return the preference for
113
     *
114
     * @return string The preference or the original class name
115
     */
116
    public function getPreference($className)
117
    {
118
119
        // query whether or not we've a preference
120
        if ($this->hasAttribute($className)) {
121
            return $this->getAttribute($className);
122
        }
123
124
        // if not, return the original class name
125
        return $className;
126
    }
127
128
    /**
129
     * Adds the passed object descriptor to the object manager. If the merge flag is TRUE, then
130
     * we check if already an object descriptor for the class exists before they will be merged.
131
     *
132
     * When we merge object descriptors this means, that the values of the passed descriptor
133
     * will override the existing ones.
134
     *
135
     * @param \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor The object descriptor to add
136
     * @param boolean                                         $merge            TRUE if we want to merge with an existing object descriptor
137
     *
138
     * @return void
139
     */
140
    public function addObjectDescriptor(DescriptorInterface $objectDescriptor, $merge = false)
141
    {
142
143
        // query whether we've to merge the configuration found in annotations
144
        if ($this->hasObjectDescriptor($objectDescriptor->getName()) && $merge) {
0 ignored issues
show
Bug introduced by
The method getName() does not exist on AppserverIo\Psr\Deployment\DescriptorInterface. It seems like you code against a sub-type of AppserverIo\Psr\Deployment\DescriptorInterface such as AppserverIo\RemoteMethodInvocation\LocalProxyTest or AppserverIo\Description\...ractNameAwareDescriptor. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        if ($this->hasObjectDescriptor($objectDescriptor->/** @scrutinizer ignore-call */ getName()) && $merge) {
Loading history...
145
            // load the existing descriptor
146
            $existingDescriptor = $this->getObjectDescriptor($objectDescriptor->getName());
147
            $existingDescriptorType = get_class($existingDescriptor);
148
            // log on info level to make overwriting more obvious
149
            $this->getApplication()->getInitialContext()->getSystemLogger()->debug(
0 ignored issues
show
Bug introduced by
The method getInitialContext() does not exist on AppserverIo\Psr\Application\ApplicationInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ApplicationInterface such as AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
            $this->getApplication()->/** @scrutinizer ignore-call */ getInitialContext()->getSystemLogger()->debug(
Loading history...
150
                sprintf(
151
                    'Overriding descriptor "%s" of webapp "%s" from XML configuration.',
152
                    $existingDescriptor->getName(),
153
                    $this->getApplication()->getName()
154
                )
155
            );
156
157
            // Merge the descriptors. XML configuration overrides values from annotation but we have to make sure
158
            // that the descriptors are of the same type. If not we have to take the XML descriptor as our new base descriptor
159
            // and merge the annotations (but XML still has to be dominant)
160
            $mergedDescriptor = null;
161
            if (!$objectDescriptor instanceof $existingDescriptorType) {
162
                // create a temporary clone of the object (XML) descriptor to merge the existing descriptor into an instance with the same type
163
                $mergedDescriptor = clone $objectDescriptor;
164
                $mergedDescriptor->merge($existingDescriptor);
0 ignored issues
show
Bug introduced by
The method merge() does not exist on AppserverIo\Psr\Deployment\DescriptorInterface. It seems like you code against a sub-type of said class. However, the method does not exist in AppserverIo\RemoteMethodInvocation\LocalProxyTest or AppserverIo\Description\AbstractDescriptor or AppserverIo\Psr\Deployment\DescriptorWrapper or AppserverIo\Description\...ractNameAwareDescriptor or AppserverIo\Description\...ractReferenceDescriptor. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
                $mergedDescriptor->/** @scrutinizer ignore-call */ 
165
                                   merge($existingDescriptor);
Loading history...
165
                $mergedDescriptor->merge($objectDescriptor);
166
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
167
            } else {
168
                // merge the object descriptor into the existing one
169
                $mergedDescriptor = $existingDescriptor;
170
                $mergedDescriptor->merge($objectDescriptor);
0 ignored issues
show
Bug introduced by
The method merge() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
                $mergedDescriptor->/** @scrutinizer ignore-call */ 
171
                                   merge($objectDescriptor);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
            }
172
173
            // re-register the merged descriptor
174
            $this->setObjectDescriptor($mergedDescriptor);
0 ignored issues
show
Bug introduced by
It seems like $mergedDescriptor can also be of type null; however, parameter $objectDescriptor of AppserverIo\Appserver\De...::setObjectDescriptor() does only seem to accept AppserverIo\Psr\Deployment\DescriptorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
            $this->setObjectDescriptor(/** @scrutinizer ignore-type */ $mergedDescriptor);
Loading history...
175
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
176
        } else {
177
            // register the object descriptor
178
            $this->setObjectDescriptor($objectDescriptor);
179
        }
180
    }
181
182
    /**
183
     * Registers the passed object descriptor under its class name.
184
     *
185
     * @param \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor The object descriptor to set
186
     *
187
     * @return void
188
     */
189
    public function setObjectDescriptor(DescriptorInterface $objectDescriptor)
190
    {
191
        $this->objectDescriptors->set($objectDescriptor->getName(), $objectDescriptor);
192
    }
193
194
    /**
195
     * Query if we've an object descriptor for the passed name.
196
     *
197
     * @param string $name The name we query for a object descriptor
198
     *
199
     * @return boolean TRUE if an object descriptor has been registered, else FALSE
200
     */
201
    public function hasObjectDescriptor($name)
202
    {
203
        return $this->objectDescriptors->has($name);
204
    }
205
206
    /**
207
     * Returns the object descriptor if we've registered it.
208
     *
209
     * @param string $name The name we want to return the object descriptor for
210
     *
211
     * @return \AppserverIo\Psr\Deployment\DescriptorInterface|null The requested object descriptor instance
212
     * @throws \AppserverIo\Psr\Di\UnknownObjectDescriptorException Is thrown if someone tries to access an unknown object desciptor
213
     */
214
    public function getObjectDescriptor($name)
215
    {
216
217
        // query if we've an object descriptor registered
218
        if ($this->hasObjectDescriptor($name)) {
219
            // return the object descriptor
220
            return $this->objectDescriptors->get($name);
221
        }
222
223
        // throw an exception is object descriptor has not been registered
224
        throw new UnknownObjectDescriptorException(sprintf('Object Descriptor with name "%s" has not been registered', $name));
225
    }
226
227
    /**
228
     * Returns the identifier for the object manager instance.
229
     *
230
     * @return string
231
     * @see \AppserverIo\Psr\Application\ManagerInterface::getIdentifier()
232
     */
233
    public function getIdentifier()
234
    {
235
        return ObjectManagerInterface::IDENTIFIER;
236
    }
237
}
238