Completed
Push — master ( f1eedc...1b618f )
by Adam
05:55
created

BundleLocator::getComposerInstalledBundleClasses()   A

↳ Parent: BundleLocator

Complexity

Conditions 3
Paths 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 16
Code Lines 9

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
namespace WellCommerce\Bundle\CoreBundle\Locator;
4
5
use Composer\Composer;
6
use Composer\Factory;
7
use Composer\IO\NullIO;
8
use Composer\Package\PackageInterface;
9
use Symfony\Component\HttpKernel\KernelInterface;
10
11
/**
12
 * Class BundleLoader
13
 *
14
 * @author  Adam Piotrowski <[email protected]>
15
 */
16
final class BundleLocator
17
{
18
    /**
19
     * @var KernelInterface
20
     */
21
    private $kernel;
22
    
23
    public function __construct(KernelInterface $kernel)
24
    {
25
        $this->kernel = $kernel;
26
    }
27
    
28
    public function getBundleClasses(): array
29
    {
30
        $composerInstalledBundleClasses = $this->getComposerInstalledBundleClasses();
31
        $localBundles                   = [];
32
        
33
        return array_merge($composerInstalledBundleClasses, $localBundles);
34
    }
35
    
36
    private function getComposerInstalledBundleClasses(): array
37
    {
38
        $bundles         = [];
39
        $composer        = $this->getComposer();
40
        $localRepository = $composer->getRepositoryManager()->getLocalRepository();
41
        
42
        /** @var PackageInterface $package */
43
        foreach ($localRepository->getPackages() as $package) {
44
            $bundleClasses = $package->getExtra()['register-bundles'] ?? [];
45
            foreach ($bundleClasses as $bundleClass) {
46
                $bundles[$bundleClass] = $bundleClass;
47
            }
48
        }
49
        
50
        return $bundles;
51
    }
52
    
53
    private function getComposer(): Composer
54
    {
55
        return Factory::create(new NullIO());
56
    }
57
}
58