Completed
Push — dev ( b3da26...a54b70 )
by Zach
03:06
created

Yarak::getKernel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak;
4
5
use Phalcon\Di\FactoryDefault;
6
use Yarak\Exceptions\FileNotFound;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Output\NullOutput;
9
10
class Yarak
11
{
12
    /**
13
     * Call a Yarak console command.
14
     *
15
     * @param string         $command
16
     * @param array          $arguments Argument array.
17
     * @param FactoryDefault $di        DI, may be necessary for php 5.6.
18
     */
19
    public static function call($command, array $arguments = [], FactoryDefault $di = null)
20
    {
21
        $kernel = self::getKernel($di);
22
23
        $arguments = ['command' => $command] + $arguments;
24
25
        $input = new ArrayInput($arguments);
26
27
        $output = new NullOutput();
28
29
        $kernel->handle($input, $output);
30
    }
31
32
    /**
33
     * Resolve Yarak kernel from di.
34
     *
35
     * @param FactoryDefault|null $di
36
     *
37
     * @throws FileNotFound
38
     *
39
     * @return Kernel
40
     */
41
    protected static function getKernel($di)
42
    {
43
        if ($di === null) {
44
            $di = self::getDI();
45
        }
46
47
        return $di->get('yarak');
48
    }
49
50
    protected static function getDI()
51
    {
52
        $di = new FactoryDefault();
53
54
        $servicesPath = __DIR__.'/../../../../app/config/services.php';
55
56
        if (!realpath($servicesPath)) {
57
            $servicesPath = __DIR__.'/../app/config/services.php';
58
        }
59
60
        try {
61
            include $servicesPath;
62
        } catch (\Exception $e) {
63
            throw FileNotFound::servicesFileNotFound(
64
                'Try passing the Yarak config array as the third argument '.
65
                'to Yarak::call.'
66
            );
67
        }
68
69
        return $di;
70
    }
71
}
72