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

DeploymentDescriptorParser::getObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * \AppserverIo\Appserver\DependencyInjectionContainer\DeploymentDescriptorParser
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
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\DependencyInjectionContainer;
22
23
use AppserverIo\Psr\Di\ObjectManagerInterface;
24
use AppserverIo\Appserver\Core\Api\Node\DiNode;
25
use AppserverIo\Configuration\Interfaces\NodeInterface;
26
27
/**
28
 * Parser to parse a deployment descriptor for beans.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2015 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/appserver-io/appserver
34
 * @link      http://www.appserver.io
35
 */
36
class DeploymentDescriptorParser extends AbstractDeploymentDescriptorParser
37
{
38
39
    /**
40
     * Parses the bean context's deployment descriptor file for beans
41
     * that has to be registered in the object manager.
42
     *
43
     * @return void
44
     */
45
    public function parse()
46
    {
47
48
        // load the deployment descriptors that has to be parsed
49
        $deploymentDescriptors = $this->loadDeploymentDescriptors();
50
51
        // parse the deployment descriptors from the conf.d and the application's META-INF directory
52
        foreach ($deploymentDescriptors as $deploymentDescriptor) {
53
            // query whether we found epb.xml deployment descriptor file
54
            if (file_exists($deploymentDescriptor) === false) {
55
                continue;
56
            }
57
58
            // validate the passed configuration file
59
            /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
60
            $configurationService = $this->getApplication()->newService('AppserverIo\Appserver\Core\Api\ConfigurationService');
0 ignored issues
show
Bug introduced by
The method newService() 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

60
            $configurationService = $this->getApplication()->/** @scrutinizer ignore-call */ newService('AppserverIo\Appserver\Core\Api\ConfigurationService');
Loading history...
61
            $configurationService->validateFile($deploymentDescriptor, null, true);
62
63
            // prepare and initialize the configuration node
64
            $diNode = new DiNode();
65
            $diNode->initFromFile($deploymentDescriptor);
0 ignored issues
show
Bug introduced by
$deploymentDescriptor of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $filename of AppserverIo\Description\...actNode::initFromFile(). ( Ignorable by Annotation )

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

65
            $diNode->initFromFile(/** @scrutinizer ignore-type */ $deploymentDescriptor);
Loading history...
66
67
            // query whether or not the deployment descriptor contains any preferences
68
            if ($preferences = $diNode->getPreferences()) {
69
                // parse the preferences of the deployment descriptor
70
                /** @var \AppserverIo\Description\Api\Node\PreferenceNode $preferenceNode */
71
                foreach ($preferences as $preferenceNode) {
72
                    $this->processPreferenceNode($preferenceNode);
73
                }
74
            }
75
76
            // query whether or not the deployment descriptor contains any beans
77
            if ($beans = $diNode->getBeans()) {
78
                // parse the beans from the deployment descriptor
79
                /** @var \AppserverIo\Description\Api\Node\BeanNode $beanNode */
80
                foreach ($beans as $beanNode) {
81
                    $this->processBeanNode($beanNode);
82
                }
83
            }
84
        }
85
    }
86
87
    /**
88
     * Creates a new descriptor instance from the data of the passed configuration node
89
     * and add's it to the object manager.
90
     *
91
     * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The node to process
92
     *
93
     * @return void
94
     */
95
    protected function processPreferenceNode(NodeInterface $node)
96
    {
97
98
        // load the object manager instance
99
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
100
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
101
102
        // iterate over all configured descriptors and try to load object description
103
        /** \AppserverIo\Appserver\Core\Api\Node\DescriptorNode $descriptor */
104
        foreach ($this->getDescriptors() as $descriptor) {
105
            try {
106
                // load the descriptor class
107
                $descriptorClass = $descriptor->getNodeValue()->getValue();
108
109
                // load the object descriptor, initialize the servlet mappings and add it to the object manager
110
                /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
111
                if ($objectDescriptor = $descriptorClass::newDescriptorInstance()->fromConfiguration($node)) {
112
                    $objectManager->addPreference($objectDescriptor, true);
113
                }
114
115
                // proceed with the next descriptor
116
                continue;
117
118
                // if class can not be reflected continue with next class
119
            } catch (\Exception $e) {
120
                // log an error message
121
                $this->getApplication()->getInitialContext()->getSystemLogger()->error($e->__toString());
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

121
                $this->getApplication()->/** @scrutinizer ignore-call */ getInitialContext()->getSystemLogger()->error($e->__toString());
Loading history...
122
123
                // proceed with the next descriptor
124
                continue;
125
            }
126
        }
127
    }
128
129
    /**
130
     * Creates a new descriptor instance from the data of the passed configuration node
131
     * and add's it to the object manager.
132
     *
133
     * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The node to process
134
     *
135
     * @return void
136
     */
137
    protected function processBeanNode(NodeInterface $node)
138
    {
139
140
        // load the object manager instance
141
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
142
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
143
144
        // iterate over all configured descriptors and try to load object description
145
        /** \AppserverIo\Appserver\Core\Api\Node\DescriptorNode $descriptor */
146
        foreach ($this->getDescriptors() as $descriptor) {
147
            try {
148
                // load the descriptor class
149
                $descriptorClass = $descriptor->getNodeValue()->getValue();
150
151
                // load the object descriptor, initialize the servlet mappings and add it to the object manager
152
                /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
153
                if ($objectDescriptor = $descriptorClass::newDescriptorInstance()->fromConfiguration($node)) {
154
                    $objectManager->addObjectDescriptor($objectDescriptor, true);
155
                }
156
157
                // proceed with the next descriptor
158
                continue;
159
160
                // if class can not be reflected continue with next class
161
            } catch (\Exception $e) {
162
                // log an error message
163
                $this->getApplication()->getInitialContext()->getSystemLogger()->error($e->__toString());
164
165
                // proceed with the next descriptor
166
                continue;
167
            }
168
        }
169
    }
170
}
171