Completed
Pull Request — master (#25)
by Alexander
02:46
created

ComposerLocator::locateClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Parser Reflection API
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\ParserReflection\Locator;
12
13
use Composer\Autoload\ClassLoader;
14
use Go\ParserReflection\LocatorInterface;
15
use Go\ParserReflection\ReflectionException;
16
17
/**
18
 * Locator, that can find a file for the given class name by asking composer
19
 */
20
class ComposerLocator implements LocatorInterface
21
{
22
    /**
23
     * @var ClassLoader
24
     */
25
    private $loader;
26
27
    public function __construct(ClassLoader $loader = null)
28
    {
29
        if (!$loader) {
30
            $loaders = spl_autoload_functions();
31
            foreach ($loaders as $loader) {
32
                if (is_array($loader) && $loader[0] instanceof ClassLoader) {
33
                    $loader = $loader[0];
34
                    break;
35
                }
36
            }
37
            if (!$loader) {
38
                throw new ReflectionException("Can not found a correct composer loader");
39
            }
40
        }
41
        $this->loader = $loader;
42
    }
43
44
    /**
45
     * Returns a path to the file for given class name
46
     *
47
     * @param string $className Name of the class
48
     *
49
     * @return string|false Path to the file with given class or false if not found
50
     */
51
    public function locateClass($className)
52
    {
53
        return $this->loader->findFile($className);
54
    }
55
}
56