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