|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
4
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
11
|
|
|
use Symfony\Bundle\WebServerBundle\WebServerBundle; |
|
12
|
|
|
use Translation\Bundle\TranslationBundle; |
|
13
|
|
|
use AppBundle\AppBundle; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Application. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Fabien Potencier <[email protected]> |
|
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Application extends BaseApplication |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
private $kernel; |
|
24
|
|
|
private $commandsRegistered = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param KernelInterface $kernel A KernelInterface instance |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(KernelInterface $kernel) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->kernel = $kernel; |
|
34
|
|
|
|
|
35
|
|
|
parent::__construct('Translation', '@package_version@'); |
|
36
|
|
|
|
|
37
|
|
|
$this->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The environment name', $kernel->getEnvironment())); |
|
38
|
|
|
$this->getDefinition()->addOption(new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Gets the Kernel associated with this Console. |
|
43
|
|
|
* |
|
44
|
|
|
* @return KernelInterface A KernelInterface instance |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getKernel() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->kernel; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Runs the current application. |
|
53
|
|
|
* |
|
54
|
|
|
* @param InputInterface $input An Input instance |
|
55
|
|
|
* @param OutputInterface $output An Output instance |
|
56
|
|
|
* |
|
57
|
|
|
* @return int 0 if everything went fine, or an error code |
|
58
|
|
|
*/ |
|
59
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->kernel->boot(); |
|
62
|
|
|
|
|
63
|
|
|
$container = $this->kernel->getContainer(); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($this->all() as $command) { |
|
66
|
|
|
if ($command instanceof ContainerAwareInterface) { |
|
67
|
|
|
$command->setContainer($container); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->setDispatcher($container->get('event_dispatcher')); |
|
72
|
|
|
|
|
73
|
|
|
return parent::doRun($input, $output); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function find($name) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->registerCommands(); |
|
82
|
|
|
|
|
83
|
|
|
return parent::find($name); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function get($name) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->registerCommands(); |
|
92
|
|
|
|
|
93
|
|
|
return parent::get($name); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function all($namespace = null) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->registerCommands(); |
|
102
|
|
|
|
|
103
|
|
|
return parent::all($namespace); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getLongVersion() |
|
110
|
|
|
{ |
|
111
|
|
|
return parent::getLongVersion().sprintf(' (kernel: <comment>%s</>, env: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getName(), $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function add(Command $command) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->registerCommands(); |
|
117
|
|
|
|
|
118
|
|
|
$name = $command->getName(); |
|
119
|
|
|
if (substr($name, 0, 12) === 'translation:') { |
|
120
|
|
|
$command->setName(substr($name, 12)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return parent::add($command); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function registerCommands() |
|
127
|
|
|
{ |
|
128
|
|
|
if ($this->commandsRegistered) { |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->commandsRegistered = true; |
|
133
|
|
|
|
|
134
|
|
|
$this->kernel->boot(); |
|
135
|
|
|
|
|
136
|
|
|
$container = $this->kernel->getContainer(); |
|
137
|
|
|
|
|
138
|
|
|
foreach ($this->kernel->getBundles() as $bundle) { |
|
139
|
|
|
if ($bundle instanceof WebServerBundle || $bundle instanceof TranslationBundle || $bundle instanceof AppBundle) { |
|
140
|
|
|
$bundle->registerCommands($this); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($container->hasParameter('console.command.ids')) { |
|
145
|
|
|
foreach ($container->getParameter('console.command.ids') as $id) { |
|
146
|
|
|
$this->add($container->get($id)); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.