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

DirectoryParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parseDirectory() 0 50 7
A parse() 0 7 2
1
<?php
2
3
/**
4
 * \AppserverIo\Appserver\DependencyInjectionContainer\DirectoryParser
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\Utilities\DirectoryKeys;
25
26
/**
27
 * Generic parser to parse a directory for annotated beans.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/appserver-io/appserver
33
 * @link      http://www.appserver.io
34
 */
35
class DirectoryParser extends AbstractParser
36
{
37
38
    /**
39
     * Parses the bean context's web application base directory for beans
40
     * that has to be registered in the object manager.
41
     *
42
     * @return void
43
     */
44
    public function parse()
45
    {
46
47
        // parse the directories from the servlet managers configuration
48
        foreach ($this->getDirectories() as $directory) {
49
            // parse the directories for annotated servlets
50
            $this->parseDirectory(DirectoryKeys::realpath($directory));
51
        }
52
    }
53
54
    /**
55
     * Parses the passed directory for classes and instances that has to be registered
56
     * in the object manager.
57
     *
58
     * @param string $directory The directory to parse
59
     *
60
     * @return void
61
     */
62
    protected function parseDirectory($directory)
63
    {
64
65
        // check if we've found a valid directory
66
        if (is_dir($directory) === false) {
67
            return;
68
        }
69
70
        // load the object manager instance
71
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
72
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
73
74
        // check directory for classes we want to register
75
        /** @var \AppserverIo\Appserver\Core\Api\DeploymentService $service */
76
        $service = $this->getApplication()->newService('AppserverIo\Appserver\Core\Api\DeploymentService');
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

76
        $service = $this->getApplication()->/** @scrutinizer ignore-call */ newService('AppserverIo\Appserver\Core\Api\DeploymentService');
Loading history...
77
        $phpFiles = $service->globDir($directory . DIRECTORY_SEPARATOR . '*.php');
78
79
        // iterate all php files
80
        foreach ($phpFiles as $phpFile) {
81
            // iterate over all configured descriptors and try to load object description
82
            foreach ($this->getDescriptors() as $descriptor) {
83
                try {
84
                    // cut off the META-INF directory and replace OS specific directory separators
85
                    $relativePathToPhpFile = str_replace(DIRECTORY_SEPARATOR, '\\', str_replace($directory, '', $phpFile));
86
87
                    // now cut off the .php extension
88
                    $className = substr($relativePathToPhpFile, 0, -4);
89
90
                    // we need a reflection class to read the annotations
91
                    /** \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass */
92
                    $reflectionClass = $objectManager->getReflectionClass($className);
0 ignored issues
show
Bug introduced by
The method getReflectionClass() does not exist on AppserverIo\Psr\Di\ObjectManagerInterface. It seems like you code against a sub-type of AppserverIo\Psr\Di\ObjectManagerInterface such as AppserverIo\Appserver\De...Container\ObjectManager. ( Ignorable by Annotation )

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

92
                    /** @scrutinizer ignore-call */ 
93
                    $reflectionClass = $objectManager->getReflectionClass($className);
Loading history...
93
94
                    // load the descriptor class
95
                    $descriptorClass = $descriptor->getNodeValue()->getValue();
96
97
                    // load the object descriptor and add it to the object manager
98
                    /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
99
                    if (class_exists($descriptorClass)) {
100
                        if ($objectDescriptor = $descriptorClass::newDescriptorInstance()->fromReflectionClass($reflectionClass)) {
101
                            $objectManager->addObjectDescriptor($objectDescriptor);
102
                        }
103
                    }
104
105
                // if class can not be reflected continue with next class
106
                } catch (\Exception $e) {
107
                    // log an error message
108
                    $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

108
                    $this->getApplication()->/** @scrutinizer ignore-call */ getInitialContext()->getSystemLogger()->error($e->__toString());
Loading history...
109
110
                    // proceed with the next bean
111
                    continue;
112
                }
113
            }
114
        }
115
    }
116
}
117