|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CRM library |
|
4
|
|
|
* @author Tao <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Slince\Crm; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
class Application extends BaseApplication |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
const VERSION = '1.2.1'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Application name |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
const NAME = 'Composer Registry Manager %s by Tao and contributors.'; |
|
24
|
|
|
|
|
25
|
|
|
protected static $logo = <<<EOT |
|
26
|
|
|
_____ _____ ___ ___ |
|
27
|
|
|
/ ___| | _ \ / |/ | |
|
28
|
|
|
| | | |_| | / /| /| | |
|
29
|
|
|
| | | _ / / / |__/ | | |
|
30
|
|
|
| |___ | | \ \ / / | | |
|
31
|
|
|
\_____| |_| \_\ /_/ |_| |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
EOT; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RegistryManager |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $manager; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(RegistryManager $manager = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->manager = $manager ?: new RegistryManager(); |
|
44
|
|
|
parent::__construct(sprintf(static::NAME, static::VERSION)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
* @codeCoverageIgnore |
|
50
|
|
|
*/ |
|
51
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->initializeConfigFile(); |
|
54
|
|
|
return parent::run($input, $output); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @codeCoverageIgnore |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function initializeConfigFile() |
|
61
|
|
|
{ |
|
62
|
|
|
if (!file_exists(ConfigPath::getUserConfigFile())) { |
|
63
|
|
|
Utils::getFilesystem()->copy(ConfigPath::getDefaultConfigFile(), ConfigPath::getUserConfigFile()); |
|
64
|
|
|
} |
|
65
|
|
|
$this->manager->readRegistriesFromFile(ConfigPath::getUserConfigFile()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getHelp() |
|
72
|
|
|
{ |
|
73
|
|
|
return static::$logo . parent::getHelp(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getDefaultCommands() |
|
80
|
|
|
{ |
|
81
|
|
|
return array_merge([ |
|
|
|
|
|
|
82
|
|
|
new Command\ListCommand($this->manager), |
|
83
|
|
|
new Command\UseCommand($this->manager), |
|
84
|
|
|
new Command\AddCommand($this->manager), |
|
85
|
|
|
new Command\RemoveCommand($this->manager), |
|
86
|
|
|
new Command\ResetCommand($this->manager), |
|
87
|
|
|
], parent::getDefaultCommands()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.