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\PathHelper; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
class LinkCommand extends Command |
25
|
|
|
{ |
26
|
|
|
protected function configure(): void |
27
|
|
|
{ |
28
|
|
|
$this->setName('link'); |
29
|
|
|
$this->setDescription('Link a package to a local directory'); |
30
|
|
|
$this->addArgument('path', InputArgument::REQUIRED, 'The path of the package'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
37
|
|
|
{ |
38
|
|
|
$helper = new PathHelper($input->getArgument('path')); |
39
|
|
|
|
40
|
|
|
// When run in global we should transform path to absolute path |
41
|
|
|
if ($this->plugin->isGlobal()) { |
42
|
|
|
/** @var string $working */ |
43
|
|
|
$working = $this->getApplication()->getInitialWorkingDirectory(); |
44
|
|
|
$helper = $helper->toAbsolutePath($working); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$paths = $helper->isWildCard() ? $helper->getPathsFromWildcard() : [$helper]; |
48
|
|
|
foreach ($paths as $path) { |
49
|
|
|
$this->linkPackage($path); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return 0; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function linkPackage(PathHelper $helper): void |
56
|
|
|
{ |
57
|
|
|
$linkedPackage = $this->plugin->getPackageFactory()->fromPath($helper->getNormalizedPath()); |
58
|
|
|
|
59
|
|
|
if (!is_null($this->plugin->getRepository()->findByPath($helper->getNormalizedPath()))) { |
60
|
|
|
throw new InvalidArgumentException( |
61
|
|
|
sprintf('Package in path "%s" already linked', $helper->getNormalizedPath()) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$currentLinked = $this->plugin->getRepository()->findByName($linkedPackage->getName()); |
66
|
|
|
if (!is_null($currentLinked)) { |
67
|
|
|
throw new InvalidArgumentException( |
68
|
|
|
sprintf( |
69
|
|
|
'Package "%s" already linked from path "%s"', |
70
|
|
|
$linkedPackage->getName(), |
71
|
|
|
$currentLinked->getPath() |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->plugin->getRepository()->store($linkedPackage); |
77
|
|
|
$this->plugin->getRepository()->persist(); |
78
|
|
|
$this->plugin->getLinkManager()->linkPackage($linkedPackage); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|