1
|
|
|
<?php |
2
|
|
|
namespace PressCLI\Command; |
3
|
|
|
|
4
|
|
|
use RuntimeException; |
5
|
|
|
use PressCLI\Option\Configuration; |
6
|
|
|
use PressCLI\Option\GlobalConfiguration; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class ConfigureCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Configure create command. |
17
|
|
|
*/ |
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this->setName('configure') |
21
|
|
|
->setDescription('Creates a new project directory and configuration.') |
22
|
|
|
->addArgument('project-name', InputArgument::OPTIONAL, 'The project name.') |
23
|
|
|
->addOption( |
24
|
|
|
'global', |
25
|
|
|
null, |
26
|
|
|
InputOption::VALUE_NONE, |
27
|
|
|
'Creates the global configuration if it does not already exist.' |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute create command. |
33
|
|
|
* |
34
|
|
|
* @param InputInterface $input |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
*/ |
37
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
38
|
|
|
{ |
39
|
|
|
$global = $input->getOption('global'); |
40
|
|
|
if ($global) { |
41
|
|
|
GlobalConfiguration::create($output); |
42
|
|
|
|
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$name = $input->getArgument('project-name'); |
47
|
|
|
if (!$name) { |
48
|
|
|
throw new RuntimeException("Project name is required when not using the --global option!"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Handle directory creation and local configuration. |
52
|
|
|
$directory = getcwd() . "/{$name}"; |
53
|
|
|
$this->createProjectDirectory($directory, $output); |
54
|
|
|
Configuration::create($directory, $name, $input, $output, $this->getHelper('question')); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create project directory. |
59
|
|
|
* |
60
|
|
|
* @return InitCommand |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
protected function createProjectDirectory($directory, OutputInterface $output) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$output->writeln("<info>Creating project directory...</info>"); |
65
|
|
|
|
66
|
|
|
if (file_exists($directory)) { |
67
|
|
|
$output->writeln("<comment>Project directory already exists at {$directory}.</comment>"); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
mkdir($directory, 0755, true); |
73
|
|
|
|
74
|
|
|
$output->writeln("<info>Project directory created at {$directory}!</info>"); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.