1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\App; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Application; |
14
|
|
|
use Symfony\Component\Console\Command\Command as SCommand; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The App to be run from a script. |
21
|
|
|
*/ |
22
|
|
|
class App extends Application { |
23
|
6 |
|
public function __construct() { |
24
|
6 |
|
parent::__construct(); |
25
|
6 |
|
ini_set('xdebug.max_nesting_level', 200); |
26
|
6 |
|
$this->add_commands(); |
27
|
6 |
|
} |
28
|
|
|
|
29
|
1 |
|
protected function add_commands() { |
30
|
1 |
|
$this->add(new AnalyzeCommand()); |
31
|
|
|
$this->add(new ReportCommand()); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Overwritten from base class to load configs, condfigure the runtime |
36
|
|
|
* and build the DIC, if the command is an command specific to dicto. |
37
|
|
|
* |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
3 |
|
public function doRunCommand(SCommand $command, InputInterface $input, OutputInterface $output) { |
41
|
3 |
|
if ($command instanceof Command) { |
42
|
2 |
|
$command->mergeApplicationDefinition(); |
43
|
2 |
|
if ($command->getDefinition()->hasArgument("configs")) { |
44
|
1 |
|
$input->bind($command->getDefinition()); |
45
|
2 |
|
$configs = $input->getArgument("configs"); |
46
|
1 |
|
$config = $this->load_config($configs); |
47
|
1 |
|
$this->configure_runtime($config); |
48
|
1 |
|
$dic = $this->build_dic($config); |
49
|
1 |
|
$command->pull_deps_from($dic); |
50
|
1 |
|
} |
51
|
2 |
|
} |
52
|
2 |
|
return parent::doRunCommand($command, $input, $output); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Configure php runtime. |
57
|
|
|
* |
58
|
|
|
* @param Config $config |
59
|
|
|
* @return null |
60
|
|
|
*/ |
61
|
2 |
|
protected function configure_runtime(Config $config) { |
62
|
2 |
|
if ($config->runtime_check_assertions()) { |
63
|
1 |
|
assert_options(ASSERT_ACTIVE, true); |
64
|
1 |
|
assert_options(ASSERT_WARNING, true); |
65
|
2 |
|
assert_options(ASSERT_BAIL, false); |
66
|
1 |
|
} |
67
|
|
|
else { |
68
|
1 |
|
assert_options(ASSERT_ACTIVE, false); |
69
|
2 |
|
assert_options(ASSERT_WARNING, false); |
70
|
1 |
|
assert_options(ASSERT_BAIL, false); |
71
|
|
|
} |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Load extra configs from yaml files. |
76
|
|
|
* |
77
|
|
|
* @param array $config_file_paths |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
2 |
|
protected function load_config(array $config_file_paths) { |
81
|
2 |
|
$configs_array = array(); |
82
|
1 |
|
$config_file_path = null; |
83
|
|
|
|
84
|
1 |
|
foreach ($config_file_paths as $config_file) { |
85
|
2 |
|
if (!file_exists($config_file)) { |
86
|
|
|
throw new \RuntimeException("Unknown config-file '$config_file'"); |
87
|
|
|
} |
88
|
1 |
|
if ($config_file_path === null) { |
89
|
1 |
|
$config_file_path = $config_file; |
90
|
1 |
|
} |
91
|
1 |
|
$configs_array[] = Yaml::parse(file_get_contents($config_file)); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
1 |
|
$t = explode("/", $config_file_path); |
95
|
1 |
|
array_pop($t); |
96
|
1 |
|
$config_file_path = implode("/", $t); |
97
|
|
|
|
98
|
2 |
|
return new Config($config_file_path, $configs_array); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Build the dependency injection container. |
103
|
|
|
* |
104
|
|
|
* @param Config |
105
|
|
|
* @return DIC |
106
|
|
|
*/ |
107
|
1 |
|
protected function build_dic(Config $config) { |
108
|
1 |
|
return new DIC($config); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|