WindowsLocatorFactory::createFromPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
namespace Nubs\Which\LocatorFactory;
3
4
use Habitat\Environment\Environment;
5
use Nubs\Which\Locator;
6
use Nubs\Which\PathBuilder\WindowsPathBuilder;
7
8
/**
9
 * Locator factory for Windows systems.
10
 */
11
class WindowsLocatorFactory implements LocatorFactoryInterface
12
{
13
    /**
14
     * Create a locator from the PATH and PATHEXT environment variables.
15
     *
16
     * @api
17
     * @param \Habitat\Environment\Environment $environment The environment
18
     *     variable wrapper.  Defaults to null which just uses PHP's built-in
19
     *     getenv.
20
     * @return \Nubs\Which\Locator The locator.
21
     */
22
    public function create(Environment $environment = null)
23
    {
24
        return $this->createFromPath(
25
            $environment ? $environment->getenv('PATH') : getenv('PATH'),
26
            $environment ? $environment->getenv('PATHEXT') : getenv('PATHEXT')
27
        );
28
    }
29
30
    /**
31
     * Create a locator using semicolon-separated PATH and PATHEXT strings.
32
     *
33
     * The semicolon is a hard separator.  This means that command paths cannot
34
     * have a semicolon in the name.
35
     *
36
     * The current working directory is always prepended to the list of paths.
37
     *
38
     * @api
39
     * @param string $path The semicolon-separated PATH string.
40
     * @param string $pathext The semicolon-separated PATHEXT string.
41
     * @return \Nubs\Which\Locator The locator.
42
     */
43
    public function createFromPath($path, $pathext)
44
    {
45
        $paths = array_merge(['.'], array_filter(explode(';', $path)));
46
        $extensions = array_merge([''], array_filter(explode(';', $pathext)));
47
        return new Locator(new WindowsPathBuilder($paths, $extensions));
48
    }
49
}
50