Repository navigation
Expand file tree
/
Copy pathComposer.php
More file actions
115 lines (95 loc) · 3.9 KB
/
Copy pathComposer.php
File metadata and controls
115 lines (95 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace GrumPHP\Task;
use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Process\ProcessBuilder;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use GrumPHP\Util\Filesystem;
use Symfony\Component\OptionsResolver\OptionsResolver;
use SplFileInfo;
class Composer extends AbstractExternalTask
{
/**
* @var Filesystem
*/
private $filesystem;
public function __construct(
ProcessBuilder $processBuilder,
ProcessFormatterInterface $formatter,
Filesystem $filesystem
) {
parent::__construct($processBuilder, $formatter);
$this->filesystem = $filesystem;
}
public static function getConfigurableOptions(): OptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'file' => './composer.json',
'no_check_all' => false,
'no_check_lock' => false,
'no_check_publish' => false,
'no_local_repository' => false,
'with_dependencies' => false,
'strict' => false,
]);
$resolver->addAllowedTypes('file', ['string']);
$resolver->addAllowedTypes('no_check_all', ['bool']);
$resolver->addAllowedTypes('no_check_lock', ['bool']);
$resolver->addAllowedTypes('no_check_publish', ['bool']);
$resolver->addAllowedTypes('no_local_repository', ['bool']);
$resolver->addAllowedTypes('with_dependencies', ['bool']);
$resolver->addAllowedTypes('strict', ['bool']);
return $resolver;
}
public function canRunInContext(ContextInterface $context): bool
{
return $context instanceof GitPreCommitContext || $context instanceof RunContext;
}
public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfig()->getOptions();
$files = $context->getFiles()
->path(pathinfo($config['file'], PATHINFO_DIRNAME))
->name(pathinfo($config['file'], PATHINFO_BASENAME));
if (0 === \count($files)) {
return TaskResult::createSkipped($this, $context);
}
$arguments = $this->processBuilder->createArgumentsForCommand('composer');
$arguments->add('validate');
$arguments->addOptionalArgument('--no-check-all', $config['no_check_all']);
$arguments->addOptionalArgument('--no-check-lock', $config['no_check_lock']);
$arguments->addOptionalArgument('--no-check-publish', $config['no_check_publish']);
$arguments->addOptionalArgument('--with-dependencies', $config['with_dependencies']);
$arguments->addOptionalArgument('--strict', $config['strict']);
$arguments->addOptionalArgument('%s', $config['file']);
$process = $this->processBuilder->buildProcess($arguments);
$process->run();
if (!$process->isSuccessful()) {
return TaskResult::createFailed($this, $context, $this->formatter->format($process));
}
$composerFile = $files->first();
if ($config['no_local_repository'] && $composerFile && $this->hasLocalRepository($composerFile)) {
return TaskResult::createFailed($this, $context, 'You have at least one local repository declared.');
}
return TaskResult::createPassed($this, $context);
}
private function hasLocalRepository(SplFileInfo $composerFile): bool
{
$json = $this->filesystem->readFromFileInfo($composerFile);
$package = json_decode($json, true);
if (!array_key_exists('repositories', $package)) {
return false;
}
foreach ($package['repositories'] as $repository) {
if ('path' === ($repository['type'] ?? null)) {
return true;
}
}
return false;
}
}