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
|
|
|
* Application name |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const NAME = 'Composer Registry Manager'; |
19
|
|
|
|
20
|
|
|
protected static $logo = <<<EOT |
21
|
|
|
_____ _____ ___ ___ |
22
|
|
|
/ ___| | _ \ / |/ | |
23
|
|
|
| | | |_| | / /| /| | |
24
|
|
|
| | | _ / / / |__/ | | |
25
|
|
|
| |___ | | \ \ / / | | |
26
|
|
|
\_____| |_| \_\ /_/ |_| |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
EOT; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RegistryManager |
33
|
|
|
*/ |
34
|
|
|
protected $manager; |
35
|
|
|
|
36
|
|
|
public function __construct(RegistryManager $manager = null) |
37
|
|
|
{ |
38
|
|
|
$this->manager = $manager ?: new RegistryManager(); |
39
|
|
|
parent::__construct(static::NAME); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
* @codeCoverageIgnore |
45
|
|
|
*/ |
46
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
47
|
|
|
{ |
48
|
|
|
$this->initializeConfigFile(); |
49
|
|
|
return parent::run($input, $output); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @codeCoverageIgnore |
54
|
|
|
*/ |
55
|
|
|
protected function initializeConfigFile() |
56
|
|
|
{ |
57
|
|
|
if (!file_exists(ConfigPath::getUserConfigFile())) { |
58
|
|
|
Utils::getFilesystem()->copy(ConfigPath::getDefaultConfigFile(), ConfigPath::getUserConfigFile()); |
59
|
|
|
} |
60
|
|
|
$this->manager->readRegistriesFromFile(ConfigPath::getUserConfigFile()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getHelp() |
67
|
|
|
{ |
68
|
|
|
return static::$logo . parent::getHelp(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getDefaultCommands() |
75
|
|
|
{ |
76
|
|
|
return array_merge([ |
|
|
|
|
77
|
|
|
new Command\ListCommand($this->manager), |
78
|
|
|
new Command\UseCommand($this->manager), |
79
|
|
|
new Command\AddCommand($this->manager), |
80
|
|
|
new Command\RemoveCommand($this->manager), |
81
|
|
|
new Command\ResetCommand($this->manager), |
82
|
|
|
], parent::getDefaultCommands()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.