|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer\Console; |
|
9
|
|
|
|
|
10
|
|
|
use Deployer\Component\PharUpdate\Console\Command as PharUpdateCommand; |
|
11
|
|
|
use Deployer\Component\PharUpdate\Console\Helper as PharUpdateHelper; |
|
12
|
|
|
use Symfony\Component\Console\Application as Console; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
15
|
|
|
|
|
16
|
|
|
class Application extends Console |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Input definition for user specific arguments and options. |
|
20
|
|
|
* |
|
21
|
|
|
* @var InputDefinition |
|
22
|
|
|
*/ |
|
23
|
|
|
private $userDefinition; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
36 |
|
protected function getDefaultInputDefinition() |
|
29
|
|
|
{ |
|
30
|
36 |
|
$inputDefinition = parent::getDefaultInputDefinition(); |
|
31
|
|
|
|
|
32
|
36 |
|
$inputDefinition->addOption( |
|
33
|
36 |
|
new InputOption('--file', '-f', InputOption::VALUE_OPTIONAL, 'Specify Deployer file') |
|
34
|
36 |
|
); |
|
35
|
|
|
|
|
36
|
36 |
|
return $inputDefinition; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
36 |
|
protected function getDefaultCommands() |
|
43
|
|
|
{ |
|
44
|
36 |
|
$commands = parent::getDefaultCommands(); |
|
45
|
36 |
|
|
|
46
|
36 |
|
if ($this->isPharArchive()) { |
|
47
|
|
|
$commands[] = $this->selfUpdateCommand(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $commands; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
36 |
|
|
|
53
|
|
|
/** |
|
54
|
36 |
|
* {@inheritdoc} |
|
55
|
36 |
|
*/ |
|
56
|
36 |
|
private function selfUpdateCommand() |
|
57
|
36 |
|
{ |
|
58
|
|
|
$selfUpdate = new PharUpdateCommand('self-update'); |
|
59
|
|
|
$selfUpdate->setDescription('Updates deployer.phar to the latest version'); |
|
60
|
|
|
$selfUpdate->setManifestUri('https://deployer.org/manifest.json'); |
|
61
|
|
|
return $selfUpdate; |
|
62
|
|
|
} |
|
63
|
36 |
|
|
|
64
|
|
|
/** |
|
65
|
36 |
|
* {@inheritdoc} |
|
66
|
36 |
|
*/ |
|
67
|
36 |
|
protected function getDefaultHelperSet() |
|
68
|
|
|
{ |
|
69
|
|
|
$helperSet = parent::getDefaultHelperSet(); |
|
70
|
|
|
|
|
71
|
|
|
if ($this->isPharArchive()) { |
|
72
|
|
|
$helperSet->set(new PharUpdateHelper()); |
|
73
|
14 |
|
} |
|
74
|
|
|
return $helperSet; |
|
75
|
14 |
|
} |
|
76
|
14 |
|
|
|
77
|
14 |
|
/** |
|
78
|
|
|
* @return InputDefinition |
|
79
|
14 |
|
*/ |
|
80
|
|
|
public function getUserDefinition() |
|
81
|
|
|
{ |
|
82
|
|
|
if (null === $this->userDefinition) { |
|
83
|
|
|
$this->userDefinition = new InputDefinition(); |
|
84
|
|
|
} |
|
85
|
14 |
|
|
|
86
|
|
|
return $this->userDefinition; |
|
87
|
14 |
|
} |
|
88
|
14 |
|
|
|
89
|
14 |
|
/** |
|
90
|
|
|
* Add user definition arguments and options to definition. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function addUserArgumentsAndOptions() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->getDefinition()->addArguments($this->getUserDefinition()->getArguments()); |
|
95
|
|
|
$this->getDefinition()->addOptions($this->getUserDefinition()->getOptions()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function isPharArchive() |
|
99
|
|
|
{ |
|
100
|
|
|
return 'phar:' === substr(__FILE__, 0, 5); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.