|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Highlighter |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Contact with author: |
|
10
|
|
|
* Xmpp: [email protected] |
|
11
|
|
|
* E-mail: [email protected] |
|
12
|
|
|
* |
|
13
|
|
|
* From Kadet with love. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\bin; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use Kadet\Highlighter\bin\Commands\FormattersCommand; |
|
20
|
|
|
use Kadet\Highlighter\bin\Commands\HighlightCommand; |
|
21
|
|
|
use Kadet\Highlighter\bin\Commands\LanguagesCommand; |
|
22
|
|
|
use Kadet\Highlighter\KeyLighter; |
|
23
|
|
|
use Symfony\Component\Console\Application as SymfonyApplication; |
|
24
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
27
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
29
|
|
|
|
|
30
|
|
|
class Application extends SymfonyApplication |
|
31
|
|
|
{ |
|
32
|
|
|
public $explicit = true; |
|
33
|
|
|
|
|
34
|
|
|
protected function getCommandName(InputInterface $input) |
|
35
|
|
|
{ |
|
36
|
|
|
$command = $input->getFirstArgument(); |
|
37
|
|
|
if(!$command && !$input->hasParameterOption('--help')) { |
|
38
|
|
|
return 'list'; |
|
39
|
|
|
} elseif ($this->has($command)) { |
|
40
|
|
|
return $command; |
|
41
|
|
|
} else { |
|
42
|
|
|
$this->explicit = false; |
|
43
|
|
|
return 'highlight'; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function getDefaultCommands() |
|
48
|
|
|
{ |
|
49
|
|
|
return array_merge(parent::getDefaultCommands(), [ |
|
|
|
|
|
|
50
|
|
|
new HighlightCommand(), |
|
51
|
|
|
new LanguagesCommand(), |
|
52
|
|
|
new FormattersCommand() |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function getDefaultInputDefinition() |
|
57
|
|
|
{ |
|
58
|
|
|
$input = parent::getDefaultInputDefinition(); |
|
59
|
|
|
$input->setOptions(array_filter($input->getOptions(), function (InputOption $option) { |
|
60
|
|
|
return $option->getShortcut() != 'q'; |
|
61
|
|
|
})); |
|
62
|
|
|
$input->addOption(new InputOption('no-output', 's', InputOption::VALUE_NONE, 'Disables output, useful for debug')); |
|
63
|
|
|
|
|
64
|
|
|
return $input; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
public function __construct() |
|
69
|
|
|
{ |
|
70
|
|
|
parent::__construct('KeyLighter', KeyLighter::VERSION); |
|
71
|
|
|
$this->setDefaultCommand('highlight'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$output = $output ?: new ConsoleOutput(); |
|
77
|
|
|
|
|
78
|
|
|
$output->getFormatter()->setStyle('command', $output->getFormatter()->getStyle('info')); |
|
79
|
|
|
$output->getFormatter()->setStyle('language', new OutputFormatterStyle('magenta')); |
|
80
|
|
|
$output->getFormatter()->setStyle('path', new OutputFormatterStyle('blue')); |
|
81
|
|
|
$output->getFormatter()->setStyle('formatter', new OutputFormatterStyle('yellow')); |
|
82
|
|
|
|
|
83
|
|
|
return parent::run($input, $output); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
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.