1 | <?php |
||
22 | class SeedCreate extends AbstractCommand |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected static $defaultName = 'seed:create'; |
||
28 | |||
29 | /** |
||
30 | * {@inheritDoc} |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | protected function configure() |
||
47 | 35 | ||
48 | /** |
||
49 | 35 | * Get the confirmation question asking if the user wants to create the |
|
50 | 35 | * seeds directory. |
|
51 | 35 | * |
|
52 | 35 | * @return \Symfony\Component\Console\Question\ConfirmationQuestion |
|
53 | 35 | */ |
|
54 | 35 | protected function getCreateSeedDirectoryQuestion() |
|
58 | 35 | ||
59 | /** |
||
60 | * Get the question that allows the user to select which seed path to use. |
||
61 | * |
||
62 | * @param string[] $paths Paths |
||
63 | * |
||
64 | * @return \Symfony\Component\Console\Question\ChoiceQuestion |
||
65 | */ |
||
66 | protected function getSelectSeedPathQuestion(array $paths) |
||
67 | { |
||
68 | return new ChoiceQuestion('Which seeds path would you like to use?', $paths, 0); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Returns the seed path to create the seeder in. |
||
73 | * |
||
74 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
||
75 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
76 | * |
||
77 | * @throws \Exception |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function getSeedPath(InputInterface $input, OutputInterface $output) |
||
82 | { |
||
83 | // First, try the non-interactive option: |
||
84 | $path = $input->getOption('path'); |
||
85 | |||
86 | if (!empty($path)) { |
||
87 | return $path; |
||
|
|||
88 | } |
||
89 | |||
90 | 2 | $paths = $this->getConfig()->getSeedPaths(); |
|
91 | |||
92 | // No paths? That's a problem. |
||
93 | 2 | if (empty($paths)) { |
|
94 | throw new Exception('No seed paths set in your Phinx configuration file.'); |
||
95 | 2 | } |
|
96 | |||
97 | $paths = Util::globAll($paths); |
||
98 | |||
99 | 2 | if (empty($paths)) { |
|
100 | throw new Exception( |
||
101 | 'You probably used curly braces to define seed path in your Phinx configuration file, ' . |
||
102 | 2 | 'but no directories have been matched using this pattern. ' . |
|
103 | 'You need to create a seed directory manually.' |
||
104 | ); |
||
105 | } |
||
106 | 2 | ||
107 | // Only one path set, so select that: |
||
108 | 2 | if (count($paths) === 1) { |
|
109 | return array_shift($paths); |
||
110 | } |
||
111 | |||
112 | /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
||
113 | $helper = $this->getHelper('question'); |
||
114 | $question = $this->getSelectSeedPathQuestion($paths); |
||
115 | |||
116 | return $helper->ask($input, $output, $question); |
||
117 | 2 | } |
|
118 | 2 | ||
119 | /** |
||
120 | * Create the new seeder. |
||
121 | * |
||
122 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
||
123 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
124 | * |
||
125 | * @throws \RuntimeException |
||
126 | * @throws \InvalidArgumentException |
||
127 | * |
||
128 | * @return int 0 on success |
||
129 | */ |
||
130 | protected function execute(InputInterface $input, OutputInterface $output) |
||
195 | } |
||
196 |
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.