1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-21 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Chapi\Commands; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Chapi\Component\Command\CommandUtils; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
20
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
21
|
|
|
|
22
|
|
|
abstract class AbstractCommand extends Command |
23
|
|
|
{ |
24
|
|
|
const FOLDER_RESOURCES = '/../../app/Resources/config/'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var InputInterface |
28
|
|
|
*/ |
29
|
|
|
protected $oInput; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var OutputInterface |
33
|
|
|
*/ |
34
|
|
|
protected $oOutput; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ContainerBuilder |
38
|
|
|
*/ |
39
|
|
|
private static $oContainer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private static $sHomeDir = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Executes the current command. |
48
|
|
|
* |
49
|
|
|
* This method is not abstract because you can use this class |
50
|
|
|
* as a concrete class. In this case, instead of defining the |
51
|
|
|
* execute() method, you set the code to execute by passing |
52
|
|
|
* a Closure to the setCode() method. |
53
|
|
|
* |
54
|
|
|
* @param InputInterface $oInput An InputInterface instance |
55
|
|
|
* @param OutputInterface $oOutput An OutputInterface instance |
56
|
|
|
* |
57
|
|
|
* @return integer null or 0 if everything went fine, or an error code |
58
|
|
|
* |
59
|
|
|
* @throws \LogicException When this abstract method is not implemented |
60
|
|
|
* |
61
|
|
|
* @see setCode() |
62
|
|
|
*/ |
63
|
10 |
|
protected function execute(InputInterface $oInput, OutputInterface $oOutput) |
64
|
|
|
{ |
65
|
10 |
|
$this->oInput = $oInput; |
66
|
10 |
|
$this->oOutput = $oOutput; |
67
|
|
|
|
68
|
10 |
|
if (!$this->isAppRunable()) |
69
|
10 |
|
{ |
70
|
|
|
return 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// set output for verbosity handling |
74
|
|
|
/** @var \Symfony\Bridge\Monolog\Handler\ConsoleHandler $_oConsoleHandler */ |
75
|
10 |
|
$_oConsoleHandler = $this->getContainer()->get('ConsoleHandler'); |
76
|
10 |
|
$_oConsoleHandler->setOutput($this->oOutput); |
77
|
|
|
|
78
|
10 |
|
return $this->process(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
|
|
abstract protected function process(); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return ContainerBuilder |
88
|
|
|
*/ |
89
|
|
|
protected function getContainer() |
90
|
|
|
{ |
91
|
|
|
if (is_null(self::$oContainer)) |
92
|
|
|
{ |
93
|
|
|
$_oContainer = new ContainerBuilder(); |
94
|
|
|
|
95
|
|
|
// load local parameters |
96
|
|
|
if (file_exists($this->getHomeDir() . DIRECTORY_SEPARATOR . 'parameters.yml')) |
97
|
|
|
{ |
98
|
|
|
$_oLoader = new YamlFileLoader($_oContainer, new FileLocator($this->getHomeDir())); |
99
|
|
|
$_oLoader->load('parameters.yml'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// load optional parameter in the current working directory |
103
|
|
|
$_sWorkingDir = getcwd(); |
104
|
|
|
if (file_exists($_sWorkingDir . DIRECTORY_SEPARATOR . '.chapiconfig')) |
105
|
|
|
{ |
106
|
|
|
$_oLoader = new YamlFileLoader($_oContainer, new FileLocator($_sWorkingDir)); |
107
|
|
|
$_oLoader->load('.chapiconfig'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// load services |
111
|
|
|
$_oLoader = new YamlFileLoader($_oContainer, new FileLocator(__DIR__ . self::FOLDER_RESOURCES)); |
112
|
|
|
$_oLoader->load('services.yml'); |
113
|
|
|
|
114
|
|
|
self::$oContainer = $_oContainer; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return self::$oContainer; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
protected function isAppRunable() |
124
|
|
|
{ |
125
|
|
|
if ( |
126
|
|
|
!file_exists($this->getHomeDir() . DIRECTORY_SEPARATOR . 'parameters.yml') |
127
|
|
|
AND !file_exists(getcwd() . DIRECTORY_SEPARATOR . '.chapiconfig') |
|
|
|
|
128
|
|
|
) // one file have to exist |
129
|
|
|
{ |
130
|
|
|
$this->oOutput->writeln(sprintf( |
131
|
|
|
'<error>%s</error>', |
132
|
|
|
'No parameter file found. Please run "configure" command for initial setup or add a local `.chapiconfig` to your working directory.' |
133
|
|
|
)); |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
2 |
|
protected function getHomeDir() |
144
|
|
|
{ |
145
|
2 |
|
if (!empty(self::$sHomeDir)) |
146
|
2 |
|
{ |
147
|
2 |
|
return self::$sHomeDir; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
$_sHomeDir = getenv('CHAPI_HOME'); |
151
|
1 |
|
if (!$_sHomeDir) |
152
|
1 |
|
{ |
153
|
1 |
|
$_sHomeDir = CommandUtils::getOsHomeDir() . DIRECTORY_SEPARATOR . '.chapi'; |
154
|
1 |
|
} |
155
|
|
|
|
156
|
1 |
|
CommandUtils::hasCreateDirectoryIfNotExists($_sHomeDir); |
157
|
|
|
|
158
|
1 |
|
return self::$sHomeDir = $_sHomeDir; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
1 |
|
protected function getCacheDir() |
165
|
|
|
{ |
166
|
1 |
|
$_sCacheDir = $this->getHomeDir() . DIRECTORY_SEPARATOR . 'cache'; |
167
|
1 |
|
CommandUtils::hasCreateDirectoryIfNotExists($_sCacheDir); |
168
|
|
|
|
169
|
1 |
|
return $_sCacheDir; |
170
|
|
|
} |
171
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.