1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Pomm's Cli package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Cli\Command; |
11
|
|
|
|
12
|
|
|
use PommProject\Cli\Exception\GeneratorException; |
13
|
|
|
use PommProject\Foundation\Inspector\InspectorPooler; |
14
|
|
|
use PommProject\Foundation\Session\Session; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SessionAwareCommand |
22
|
|
|
* |
23
|
|
|
* Base command for Cli commands that need a session. |
24
|
|
|
* |
25
|
|
|
* @package Cli |
26
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
27
|
|
|
* @author Grégoire HUBERT |
28
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
29
|
|
|
* @see Command |
30
|
|
|
*/ |
31
|
|
|
class SessionAwareCommand extends PommAwareCommand |
32
|
|
|
{ |
33
|
|
|
private $session; |
34
|
|
|
|
35
|
|
|
protected $config_name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* execute |
39
|
|
|
* |
40
|
|
|
* Set pomm dependent variables. |
41
|
|
|
* |
42
|
|
|
* @see Command |
43
|
|
|
*/ |
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
parent::execute($input, $output); |
47
|
|
|
$this->config_name = $input->getArgument('config-name'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* configureRequiredArguments |
52
|
|
|
* |
53
|
|
|
* In order to keep the same argument order for all commands, it is |
54
|
|
|
* necessary to be able to declare base required fields before subcommands. |
55
|
|
|
* |
56
|
|
|
* @access protected |
57
|
|
|
* @return PommAwareCommand $this |
58
|
|
|
*/ |
59
|
|
|
protected function configureRequiredArguments() |
60
|
|
|
{ |
61
|
|
|
$this |
62
|
|
|
->addArgument( |
63
|
|
|
'config-name', |
64
|
|
|
InputArgument::REQUIRED, |
65
|
|
|
'Database configuration name to open a session.' |
66
|
|
|
) |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* getSession |
74
|
|
|
* |
75
|
|
|
* Return a session. |
76
|
|
|
* |
77
|
|
|
* @access protected |
78
|
|
|
* @return Session |
79
|
|
|
*/ |
80
|
|
|
protected function getSession() |
81
|
|
|
{ |
82
|
|
|
if ($this->session === null) { |
83
|
|
|
$this->session = $this |
84
|
|
|
->getPomm() |
85
|
|
|
->getSession($this->config_name) |
|
|
|
|
86
|
|
|
->registerClientPooler(new InspectorPooler()) |
87
|
|
|
; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->session; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* mustBeModelManagerSession |
95
|
|
|
* |
96
|
|
|
* Check if a session is a \PommProject\ModelManager\Session. |
97
|
|
|
* |
98
|
|
|
* @access protected |
99
|
|
|
* @param Session $session |
100
|
|
|
* @throws GeneratorException |
101
|
|
|
* @return Session |
102
|
|
|
*/ |
103
|
|
|
protected function mustBeModelManagerSession(Session $session) |
104
|
|
|
{ |
105
|
|
|
if (!$session instanceof \PommProject\ModelManager\Session) { |
106
|
|
|
throw new GeneratorException( |
107
|
|
|
sprintf( |
108
|
|
|
"To generate models, you should use a '\PommProject\ModelManager\Session session' ('%s' used).", |
109
|
|
|
get_class($session) |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $session; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* setSession |
119
|
|
|
* |
120
|
|
|
* When testing, it is useful to provide directly the session to be used. |
121
|
|
|
* |
122
|
|
|
* @access public |
123
|
|
|
* @param Session $session |
124
|
|
|
* @return PommAwareCommand |
125
|
|
|
*/ |
126
|
|
|
public function setSession(Session $session) |
127
|
|
|
{ |
128
|
|
|
$this->session = $session; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.