|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
|
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
7
|
|
|
use SilverStripe\Dev\BuildTask; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Question\Question; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A slightly embellished Symfony Command class which is SilverStripe aware |
|
16
|
|
|
* |
|
17
|
|
|
* @package silverstripe-console |
|
18
|
|
|
* @author Robbie Averill <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class SilverStripeCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Add a set of default SilverStripe options to all commands |
|
24
|
|
|
* |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($name = null) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($name); |
|
32
|
|
|
|
|
33
|
|
|
$this->addOption('flush', 'f', null, 'Flush SilverStripe cache and manifest.'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Retrieve an argument from the input interface, or use the Question helper to ask for input |
|
38
|
|
|
* if it wasn't provided. Will automatically hide input for password fields. |
|
39
|
|
|
* |
|
40
|
|
|
* @param InputInterface $input |
|
41
|
|
|
* @param OutputInterface $output |
|
42
|
|
|
* @param string $key The argument key, e.g. "username" |
|
43
|
|
|
* @param string $question The question to ask, e.g. "Which username: " |
|
44
|
|
|
* @return string|null |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getOrAskForArgument(InputInterface $input, OutputInterface $output, $key, $question) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($supplied = $input->getArgument($key)) { |
|
49
|
|
|
return $supplied; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$question = new Question($question); |
|
53
|
|
|
if (stripos($key, 'password') !== false) { |
|
54
|
|
|
$question->setHidden(true); |
|
55
|
|
|
$question->setHiddenFallback(false); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the SilverStripe Injector |
|
63
|
|
|
* |
|
64
|
|
|
* @return Injector |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getInjector() |
|
67
|
|
|
{ |
|
68
|
|
|
return Injector::inst(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the configuration API handler |
|
73
|
|
|
* |
|
74
|
|
|
* @return Config |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getConfig() |
|
77
|
|
|
{ |
|
78
|
|
|
return Config::inst(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: