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

DeploymentDescriptorParser::getApplication()   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 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * \AppserverIo\Appserver\ServletEngine\DependencyInjection\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\ServletEngine\DependencyInjection;
22
23
use AppserverIo\Psr\Di\ObjectManagerInterface;
24
use AppserverIo\Appserver\Core\Api\Node\WebAppNode;
25
use AppserverIo\Appserver\DependencyInjectionContainer\AbstractDeploymentDescriptorParser;
26
27
/**
28
 * Parser implementation to parse a web application deployment descriptor (WEB-INF/web.xml).
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 servlet context's deployment descriptor file for servlets
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
            // load the object manager instance
64
            /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
65
            $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
66
67
            // prepare and initialize the configuration node
68
            $webAppNode = new WebAppNode();
69
            $webAppNode->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

69
            $webAppNode->initFromFile(/** @scrutinizer ignore-type */ $deploymentDescriptor);
Loading history...
70
71
            /** @var \AppserverIo\Appserver\Core\Api\Node\ServletNode $servletNode */
72
            foreach ($webAppNode->getServlets() as $servletNode) {
73
                // iterate over all configured descriptors and try to load object description
74
                /** \AppserverIo\Appserver\Core\Api\Node\DescriptorNode $descriptor */
75
                foreach ($this->getDescriptors() as $descriptor) {
76
                    try {
77
                        // load the descriptor class
78
                        $descriptorClass = $descriptor->getNodeValue()->getValue();
79
80
                        // load the object descriptor, initialize the servlet mappings and add it to the object manager
81
                        /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
82
                        if ($objectDescriptor = $descriptorClass::newDescriptorInstance()->fromConfiguration($servletNode)) {
83
                            /** @var \AppserverIo\Appserver\Core\Api\Node\ServletMappingNode $servletMappingNode */
84
                            foreach ($webAppNode->getServletMappings() as $servletMappingNode) {
85
                                // query whether or not we've to add the URL pattern for the servlet
86
                                if ((string) $servletNode->getServletName() === (string) $servletMappingNode->getServletName()) {
87
                                    $objectDescriptor->addUrlPattern((string) $servletMappingNode->getUrlPattern());
88
                                }
89
                            }
90
91
                            // add the object descriptor for the servlet
92
                            $objectManager->addObjectDescriptor($objectDescriptor, true);
93
                        }
94
95
                        // proceed with the next descriptor
96
                        continue;
97
98
                    // if class can not be reflected continue with next class
99
                    } catch (\Exception $e) {
100
                        // log an error message
101
                        $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

101
                        $this->getApplication()->/** @scrutinizer ignore-call */ getInitialContext()->getSystemLogger()->error($e->__toString());
Loading history...
102
103
                        // proceed with the next descriptor
104
                        continue;
105
                    }
106
                }
107
            }
108
109
            // initialize the session configuration if available
110
            /** @var \AppserverIo\Appserver\Core\Api\Node\SessionConfigNode $sessionConfig */
111
            if ($sessionConfig = $webAppNode->getSessionConfig()) {
112
                foreach ($sessionConfig->toArray() as $key => $value) {
113
                    $this->getManager()->addSessionParameter($key, $value);
0 ignored issues
show
Bug introduced by
The method addSessionParameter() does not exist on AppserverIo\Psr\Application\ManagerInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ManagerInterface such as AppserverIo\Appserver\ServletEngine\ServletManager. ( Ignorable by Annotation )

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

113
                    $this->getManager()->/** @scrutinizer ignore-call */ addSessionParameter($key, $value);
Loading history...
114
                }
115
            }
116
117
            // initialize the error page configuration if available
118
            /** @var \AppserverIo\Appserver\Core\Api\Node\ErrorPageNode $errorPageNode */
119
            foreach ($webAppNode->getErrorPages() as $errorPageNode) {
120
                $this->getManager()->addErrorPage((string) $errorPageNode->getErrorCodePattern(), (string) $errorPageNode->getErrorLocation());
0 ignored issues
show
Bug introduced by
The method addErrorPage() does not exist on AppserverIo\Psr\Application\ManagerInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ManagerInterface such as AppserverIo\Appserver\ServletEngine\ServletManager. ( Ignorable by Annotation )

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

120
                $this->getManager()->/** @scrutinizer ignore-call */ addErrorPage((string) $errorPageNode->getErrorCodePattern(), (string) $errorPageNode->getErrorLocation());
Loading history...
121
            }
122
123
            // initialize the context with the context parameters
124
            /** @var \AppserverIo\Appserver\Core\Api\Node\ContextParamNode $contextParamNode */
125
            foreach ($webAppNode->getContextParams() as $contextParamNode) {
126
                $this->getManager()->addInitParameter((string) $contextParamNode->getParamName(), (string) $contextParamNode->getParamValue());
0 ignored issues
show
Bug introduced by
The method addInitParameter() does not exist on AppserverIo\Psr\Application\ManagerInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ManagerInterface such as AppserverIo\Appserver\ServletEngine\ServletManager. ( Ignorable by Annotation )

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

126
                $this->getManager()->/** @scrutinizer ignore-call */ addInitParameter((string) $contextParamNode->getParamName(), (string) $contextParamNode->getParamValue());
Loading history...
127
            }
128
        }
129
    }
130
}
131