Application::getDefaultCommands()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 9.0534
cc 4
eloc 10
nc 3
nop 0
crap 20
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