Passed
Pull Request — 1.1 (#1122)
by Tim
07:39
created

DeploymentDescriptorParser::parse()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 37
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

61
            $configurationService = $this->getApplication()->/** @scrutinizer ignore-call */ newService('AppserverIo\Appserver\Core\Api\ConfigurationService');
Loading history...
62
            $configurationService->validateFile($deploymentDescriptor, null, true);
63
64
            // prepare and initialize the configuration node
65
            $ldapNode = new LdapNode();
66
            $ldapNode->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

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

122
                $this->getApplication()->/** @scrutinizer ignore-call */ getInitialContext()->getSystemLogger()->error($e->__toString());
Loading history...
123
124
                // proceed with the next descriptor
125
                continue;
126
            }
127
        }
128
    }
129
}
130