Completed
Pull Request — master (#1105)
by Tim
42:55
created

AppEnvironmentHelper::globDir()   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 3
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * \AppserverIo\Appserver\Core\Utilities\AppEnvironmentHelper
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    Bernhard Wick <[email protected]>
15
 * @author    Hans Höchtl <[email protected]>
16
 * @copyright 2016 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\Core\Utilities;
23
24
use AppserverIo\Properties\Properties;
25
26
/**
27
 * Helper which provides static methods for handling different application environment settings
28
 *
29
 * @author    Bernhard Wick <[email protected]>
30
 * @author    Hans Höchtl <[email protected]>
31
 * @copyright 2016 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 AppEnvironmentHelper
37
{
38
39
    /**
40
     * Returns the name of the configuration file
41
     *
42
     * @var string CONFIGURATION_FILE
43
     */
44
    const CONFIGURATION_FILE = 'build.properties';
45
46
    /**
47
     * Cached configuration properties
48
     *
49
     * @var \AppserverIo\Properties\Properties $cachedProperties
50
     */
51
    protected static $cachedProperties;
52
53
    /**
54
     * Get the environment modifier (if any) which helps to switch the configuration environment
55
     *
56
     * @param string $appBase The base of the application we are dealing with
57
     *
58
     * @return string
59
     *
60
     * @throws \AppserverIo\Properties\PropertyFileNotFoundException
61
     * @throws \AppserverIo\Properties\PropertyFileParseException
62
     */
63
    public static function getEnvironmentModifier($appBase)
64
    {
65 7
        // check if we got the properties cached already, if not load them anew
66
        $properties = null;
67
        if (!is_null(self::$cachedProperties)) {
68 7
            $properties = self::$cachedProperties;
69 7
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
70 7
        } else {
71
            // load the properties from file
72 7
            $propertiesFile = DirectoryKeys::realpath(
73
                sprintf('%s/%s', $appBase, self::CONFIGURATION_FILE)
74
            );
75
76
            // load the properties from the configuration file
77
            if (file_exists($propertiesFile)) {
78
                $properties = Properties::create()->load($propertiesFile);
0 ignored issues
show
Bug introduced by
$propertiesFile of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $file of AppserverIo\Properties\Properties::load(). ( Ignorable by Annotation )

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

78
                $properties = Properties::create()->load(/** @scrutinizer ignore-type */ $propertiesFile);
Loading history...
79
            }
80
        }
81
82
        // load the properties from the configuration file
83
        $result = '';
84
        // get the actual property if it exists
85 7
        if (!is_null($properties) && $properties->exists(ConfigurationKeys::APP_ENVIRONMENT)) {
86
            $result = $properties->get(ConfigurationKeys::APP_ENVIRONMENT);
87 7
        }
88 7
        // ENV variable always wins
89 7
        if (defined(ConfigurationKeys::APP_ENVIRONMENT)) {
90
            $result = getenv(ConfigurationKeys::APP_ENVIRONMENT);
91 7
        }
92
93
        return $result;
94
    }
95 7
96
    /**
97
     * Will take a segmented path to a file (which might contain glob type wildcards) and return it fixed to the currently active environment modifier.
98
     * E.g.
99
     * AppEnvironmentHelper::getEnvironmentAwareFilePath('webapps/example', 'META-INF/*-ds') => 'webapps/example/META-INF/*-ds.dev.xml'
100
     *
101
     * @param string  $appBase       The base file path to the application
102
     * @param string  $fileGlob      The intermediate path (or glob pattern) from app base path to file extension
103
     * @param integer $flags         The flags passed to the glob function
104
     * @param string  $fileExtension The extension of the file, will default to 'xml'
105
     *
106
     * @return string
107
     */
108
    public static function getEnvironmentAwareGlobPattern($appBase, $fileGlob, $flags = 0, $fileExtension = 'xml')
109
    {
110 7
        // get the file path modifier
111
        $modifier = static::getEnvironmentModifier($appBase);
112
113 7
        // as we default to a not modified path we have to be careful about the "two dots" schema .$modifier.$extension
114
        $defaultFilePath = $appBase . DIRECTORY_SEPARATOR . $fileGlob . '.' . $fileExtension;
115
        if (empty($modifier)) {
116 7
            // if we do not have a modifier we do not need to act upon anything, so we return the default
117 7
            return $defaultFilePath;
118
        } else {
119 2
            // we got a modifier we have to check if there is something reachable under the modified path, if not we will also return the default
120
            $modifiedPath = $appBase . DIRECTORY_SEPARATOR . $fileGlob . '.' . $modifier . '.' . $fileExtension;
121
            $potentialFiles = static::globDir($modifiedPath, $flags);
122 5
            if (!empty($potentialFiles)) {
123 5
                return $modifiedPath;
124 5
            }
125 2
            return $defaultFilePath;
126
        }
127 3
    }
128
129
    /**
130
     * Parses and returns the directories and files that matches
131
     * the passed glob pattern in a recursive way (if wanted).
132
     *
133
     * @param string  $pattern   The glob pattern used to parse the directories
134
     * @param integer $flags     The flags passed to the glob function
135
     * @param boolean $recursive Whether or not to parse directories recursively
136
     *
137
     * @return array The directories matches the passed glob pattern
138
     * @link http://php.net/glob
139
     */
140
    protected static function globDir($pattern, $flags = 0, $recursive = true)
141
    {
142
        return FileSystem::globDir($pattern, $flags, $recursive);
143
    }
144
}
145