1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Discovery\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Composer\Command\BaseCommand; |
8
|
|
|
use Composer\Factory; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use TheCodingMachine\Discovery\Asset; |
14
|
|
|
use TheCodingMachine\Discovery\AssetOperation; |
15
|
|
|
use TheCodingMachine\Discovery\AssetsBuilder; |
16
|
|
|
use TheCodingMachine\Discovery\AssetType; |
17
|
|
|
use TheCodingMachine\Discovery\DiscoveryFileLoader; |
18
|
|
|
use TheCodingMachine\Discovery\Dumper; |
19
|
|
|
|
20
|
|
|
class AddAssetCommand extends BaseCommand |
21
|
|
|
{ |
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
|
|
$this->setName('discovery:add') |
25
|
|
|
->setDescription('Add an asset') |
26
|
|
|
->addArgument('asset-type', InputArgument::REQUIRED, 'The asset type') |
27
|
|
|
->addArgument('value', InputArgument::REQUIRED, 'The asset to add') |
28
|
|
|
->addOption('priority', 'p', InputOption::VALUE_REQUIRED, 'The priority', 0.0); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
32
|
|
|
{ |
33
|
|
|
$assetTypeName = $input->getArgument('asset-type'); |
34
|
|
|
$assetValue = $input->getArgument('value'); |
35
|
|
|
|
36
|
|
|
$priority = $input->getOption('priority'); |
37
|
|
|
|
38
|
|
|
if (!is_numeric($priority)) { |
39
|
|
|
$output->writeln('<error>The priority must be a numeric value.</error>'); |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$loader = new DiscoveryFileLoader(); |
44
|
|
|
|
45
|
|
View Code Duplication |
if (file_exists('discovery.json')) { |
|
|
|
|
46
|
|
|
$assetOperationTypes = $loader->loadDiscoveryFile(new \SplFileInfo('discovery.json'), '', ''); |
47
|
|
|
} else { |
48
|
|
|
$assetOperationTypes = []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$assetOperationTypes[$assetTypeName][] = new AssetOperation(AssetOperation::ADD, new Asset($assetValue, '', '', $priority, null)); |
52
|
|
|
|
53
|
|
|
$loader->saveDiscoveryFile($assetOperationTypes, new \SplFileObject('discovery.json', 'w+')); |
54
|
|
|
|
55
|
|
|
$dumper = new Dumper($this->getComposer(), $this->getIO()); |
|
|
|
|
56
|
|
|
$dumper->dumpDiscoveryFiles(); |
57
|
|
|
|
58
|
|
|
$output->writeln('discovery.json file successfully modified.'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.