1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the composer-link plugin. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2021-2022 Sander Visser <[email protected]>. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/SanderSander/composer-link |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace ComposerLink\Commands; |
17
|
|
|
|
18
|
|
|
use ComposerLink\LinkedPackage; |
19
|
|
|
use ComposerLink\PathHelper; |
20
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
|
25
|
|
|
class LinkCommand extends Command |
26
|
|
|
{ |
27
|
|
|
protected function configure(): void |
28
|
|
|
{ |
29
|
|
|
$this->setName('link'); |
30
|
|
|
$this->setDescription('Link a package to a local directory'); |
31
|
|
|
$this->addArgument('path', InputArgument::REQUIRED, 'The path of the package'); |
32
|
|
|
$this->addOption( |
33
|
|
|
'only-installed', |
34
|
|
|
null, |
35
|
|
|
InputOption::VALUE_NONE, |
36
|
|
|
'Link only installed packages', |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
44
|
|
|
{ |
45
|
|
|
/** @var bool $onlyInstalled */ |
46
|
|
|
$onlyInstalled = $input->getOption('only-installed'); |
47
|
|
|
$paths = $this->getPaths($input); |
48
|
|
|
|
49
|
|
|
foreach ($paths as $path) { |
50
|
|
|
$package = $this->getPackage($path, $output); |
51
|
|
|
|
52
|
|
|
if (is_null($package)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($onlyInstalled && is_null($package->getOriginalPackage())) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->plugin->getRepository()->store($package); |
61
|
|
|
$this->plugin->getLinkManager()->linkPackage($package); |
62
|
|
|
|
63
|
|
|
// Could be optimized, but for now we persist every package, |
64
|
|
|
// so we know what we have done when a package fails |
65
|
|
|
$this->plugin->getRepository()->persist(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return 0; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getPackage(PathHelper $helper, OutputInterface $output): ?LinkedPackage |
72
|
|
|
{ |
73
|
|
|
$linkedPackage = $this->plugin->getPackageFactory()->fromPath($helper->getNormalizedPath()); |
74
|
|
|
$repository = $this->plugin->getRepository(); |
75
|
|
|
|
76
|
|
|
if (!is_null($repository->findByPath($helper->getNormalizedPath()))) { |
77
|
|
|
$output->writeln( |
78
|
|
|
sprintf('<warning>Package in path "%s" already linked</warning>', $helper->getNormalizedPath()) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$currentLinked = $repository->findByName($linkedPackage->getName()); |
85
|
|
|
if (!is_null($currentLinked)) { |
86
|
|
|
$output->writeln( |
87
|
|
|
sprintf( |
88
|
|
|
'<warning>Package "%s" in "%s" already linked from path "%s"</warning>', |
89
|
|
|
$linkedPackage->getName(), |
90
|
|
|
$linkedPackage->getPath(), |
91
|
|
|
$currentLinked->getPath() |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $linkedPackage; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|