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