1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the XabbuhPandaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xabbuh\PandaBundle\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
21
|
|
|
use Xabbuh\PandaClient\Exception\PandaException; |
22
|
|
|
|
23
|
1 |
|
if (class_exists(ContainerAwareCommand::class)) { |
24
|
|
|
/** |
25
|
|
|
* Base class of all commands which act on panda clouds. |
26
|
|
|
* |
27
|
|
|
* The cloud name can be specified on the command-line. If no cloud is |
28
|
|
|
* given the configured default cloud is used. |
29
|
|
|
* |
30
|
|
|
* @author Christian Flothmann <[email protected]> |
31
|
|
|
* |
32
|
|
|
* @internal since 1.5 |
33
|
|
|
*/ |
34
|
|
|
abstract class CloudCommand extends ContainerAwareCommand |
35
|
|
|
{ |
36
|
|
|
use CloudCommandTrait; |
37
|
|
|
} |
38
|
|
|
} else { |
39
|
|
|
/** |
40
|
|
|
* Base class of all commands which act on panda clouds. |
41
|
|
|
* |
42
|
|
|
* The cloud name can be specified on the command-line. If no cloud is |
43
|
|
|
* given the configured default cloud is used. |
44
|
|
|
* |
45
|
|
|
* @author Christian Flothmann <[email protected]> |
46
|
|
|
* |
47
|
|
|
* @internal since 1.5 |
48
|
|
|
*/ |
49
|
|
|
abstract class CloudCommand extends Command implements ContainerAwareInterface |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
use CloudCommandTrait; |
52
|
|
|
|
53
|
|
|
private $container; |
54
|
|
|
|
55
|
85 |
|
public function setContainer(ContainerInterface $container = null) |
56
|
|
|
{ |
57
|
85 |
|
$this->container = $container; |
58
|
85 |
|
} |
59
|
|
|
|
60
|
67 |
|
public function getContainer() |
61
|
|
|
{ |
62
|
67 |
|
return $this->container; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
trait CloudCommandTrait |
68
|
|
|
{ |
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
85 |
|
protected function configure() |
73
|
|
|
{ |
74
|
85 |
|
$this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically |
|
|
|
|
75
|
85 |
|
$this->addOption( |
|
|
|
|
76
|
85 |
|
'cloud', |
77
|
85 |
|
'-c', |
78
|
85 |
|
InputOption::VALUE_REQUIRED, |
79
|
85 |
|
'Cloud on which the command is executed.' |
80
|
|
|
); |
81
|
85 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \Xabbuh\PandaClient\Api\CloudManager |
85
|
|
|
*/ |
86
|
67 |
|
protected function getCloudManager() |
87
|
|
|
{ |
88
|
67 |
|
return $this->getContainer()->get('xabbuh_panda.cloud_manager'); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the cloud to work on. |
93
|
|
|
* |
94
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
95
|
|
|
* |
96
|
|
|
* @return \Xabbuh\PandaClient\Api\CloudInterface |
97
|
|
|
*/ |
98
|
67 |
|
protected function getCloud(InputInterface $input) |
99
|
|
|
{ |
100
|
67 |
|
if (null === $input->getOption('cloud')) { |
101
|
67 |
|
return $this->getCloudManager()->getDefaultCloud(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->getCloudManager()->getCloud($input->getOption('cloud')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Executes the actual command (to be implemented by subclasses, will be called automatically). |
109
|
|
|
* |
110
|
|
|
* @param InputInterface $input |
111
|
|
|
* @param OutputInterface $output |
112
|
|
|
*/ |
113
|
|
|
abstract protected function doExecuteCommand(InputInterface $input, OutputInterface $output); |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
69 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
119
|
|
|
{ |
120
|
|
|
try { |
121
|
69 |
|
$this->doExecuteCommand($input, $output); |
122
|
|
|
|
123
|
40 |
|
return 0; |
124
|
29 |
|
} catch (PandaException $e) { |
125
|
29 |
|
$output->writeln(sprintf('<error>An error occurred: %s</error>', $e->getMessage())); |
126
|
|
|
|
127
|
29 |
|
return 1; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks for classes that have been defined more than once in the same file.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.