1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Console-Kit library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/console-kit |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\ConsoleKit; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand; |
15
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class Application extends BaseApplication |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Dependency injection container. |
24
|
|
|
* |
25
|
|
|
* @var Container |
26
|
|
|
*/ |
27
|
|
|
protected $dic; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates application. |
31
|
|
|
* |
32
|
|
|
* @param Container $container Container. |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Container $container) |
35
|
|
|
{ |
36
|
|
|
$this->dic = $container; |
37
|
|
|
|
38
|
|
|
parent::__construct($this->dic['app_name'], $this->dic['app_version']); |
39
|
|
|
|
40
|
|
|
$helper_set = $this->getHelperSet(); |
41
|
|
|
$helper_set->set($this->dic['container_helper']); |
42
|
|
|
|
43
|
|
|
$that = $this; |
44
|
|
|
$this->dic['helper_set'] = function () use ($that) { |
45
|
|
|
return $that->getHelperSet(); |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
protected function getDefaultCommands() |
53
|
|
|
{ |
54
|
|
|
$default_commands = parent::getDefaultCommands(); |
55
|
|
|
$default_commands[] = new CompletionCommand(); |
56
|
|
|
|
57
|
|
|
return $default_commands; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
64
|
|
|
{ |
65
|
|
|
if ( !isset($input) ) { |
66
|
|
|
$input = $this->dic['input']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( !isset($output) ) { |
70
|
|
|
$output = $this->dic['output']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return parent::run($input, $output); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Detects, when we're inside PHAR file. |
78
|
|
|
* |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
protected function isPharFile() |
82
|
|
|
{ |
83
|
|
|
return strpos(__DIR__, 'phar://') === 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.