|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the slince/spike package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Slince <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Spike\Server\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Slince\Config\Config; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
class InitCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function configure() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->ignoreValidationErrors(); |
|
28
|
|
|
$this->setName('init') |
|
29
|
|
|
->setDefinition($this->createDefinition()) |
|
30
|
|
|
->setDescription('Create a configuration file in the specified directory'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
37
|
|
|
{ |
|
38
|
|
|
$templateConfigFile = __DIR__.'/../../../resources/spiked-template.json'; |
|
39
|
|
|
$config = new Config($templateConfigFile); |
|
40
|
|
|
$dstPath = $input->getOption('dir'); |
|
41
|
|
|
$extension = $input->getOption('format'); |
|
42
|
|
|
if (!in_array($extension, $this->getSupportedFormats())) { |
|
43
|
|
|
$output->writeln(sprintf('<error>The format "%s" is not supported</error>', $extension)); |
|
44
|
|
|
|
|
45
|
|
|
return false; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
if (!$config->dump("{$dstPath}/spiked.{$extension}")) { |
|
48
|
|
|
$output->writeln('Can not create the configuration file'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
* |
|
55
|
|
|
* @codeCoverageIgnore |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getNativeDefinition() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->createDefinition(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Gets all supported formats. |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getSupportedFormats() |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
'json', 'yaml', 'xml', |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
private function createDefinition() |
|
78
|
|
|
{ |
|
79
|
|
|
return new InputDefinition(array( |
|
80
|
|
|
new InputOption('format', null, InputOption::VALUE_REQUIRED, |
|
81
|
|
|
'The configuration file format, support json,ini,xml and yaml', 'json'), |
|
82
|
|
|
new InputOption('dir', null, InputOption::VALUE_REQUIRED, |
|
83
|
|
|
'The directory', getcwd()), |
|
84
|
|
|
)); |
|
85
|
|
|
} |
|
86
|
|
|
} |
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.