Application::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 3
crap 6
1
<?php
2
3
namespace Comrade42\PhpBBParser;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\Console\Application as BaseApplication;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
9
use Symfony\Component\Finder\Finder;
10
11
/**
12
 * Class Application
13
 * @package Comrade42\PhpBBParser
14
 */
15
class Application extends BaseApplication
16
{
17
    /**
18
     * @var ContainerBuilder
19
     */
20
    protected $container;
21
22
    /**
23
     * @param string $rootPath
24
     * @param string $name The name of the application
25
     * @param string $version The version of the application
26
     * @throws \InvalidArgumentException
27
     */
28
    public function __construct($rootPath, $name = 'UNKNOWN', $version = 'UNKNOWN')
29
    {
30
        if (!is_dir($rootPath)) {
31
            throw new \InvalidArgumentException("Wrong root path '{$rootPath}' specified!");
32
        }
33
34
        $this->container = new ContainerBuilder();
35
        $this->container->setParameter('root_path', realpath($rootPath));
36
37
        $loader = new YamlFileLoader($this->container, new FileLocator(array($rootPath . '/app/config')));
38
        $loader->load('config.yml');
39
40
        $this->container->compile();
41
42
        parent::__construct($name, $version);
43
    }
44
45
    /**
46
     * @return ContainerBuilder
47
     */
48
    public function getContainer()
49
    {
50
        return $this->container;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getRootPath()
57
    {
58
        return $this->container->getParameter('root_path');
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getDefaultCommands()
65
    {
66
        $commands = parent::getDefaultCommands();
67
68
        $finder = new Finder();
69
        $finder->files()->name('*Command.php')->in($this->getRootPath() . '/src');
70
71
        foreach ($finder as $file)
72
        {
73
            /** @var \Symfony\Component\Finder\SplFileInfo $file */
74
            $namespace = strtr($file->getRelativePath(), '/', '\\');
75
            $class = new \ReflectionClass($namespace . '\\' . $file->getBasename('.php'));
76
77
            if ($class->isSubclassOf('Comrade42\\PhpBBParser\\Command\\ContainerAwareCommand') && !$class->isAbstract())
78
            {
79
                $commands[] = $class->newInstance($this->container);
80
            }
81
        }
82
83
        return $commands;
84
    }
85
}
86