Skip to content

Commit 7692708

Browse files
committed
Allow creating diffs from array and shift URL generation to DiffEntry
1 parent 347c63c commit 7692708

19 files changed

+560
-459
lines changed

src/Command/DiffCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ protected function handle(InputInterface $input, OutputInterface $output)
147147
$this->gitlabDomains = array_merge($this->gitlabDomains, $input->getOption('gitlab-domains'));
148148

149149
$urlGenerators = new GeneratorContainer($this->gitlabDomains);
150-
$formatters = new FormatterContainer($output, $urlGenerators);
150+
$formatters = new FormatterContainer($output);
151151
$formatter = $formatters->getFormatter($input->getOption('format'));
152152

153+
$this->packageDiff->setUrlGenerator($urlGenerators);
154+
153155
$prodOperations = new DiffEntries(array());
154156
$devOperations = new DiffEntries(array());
155157

src/Diff/DiffEntry.php

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Composer\DependencyResolver\Operation\OperationInterface;
77
use Composer\DependencyResolver\Operation\UninstallOperation;
88
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use Composer\Package\CompletePackageInterface;
910
use Composer\Package\PackageInterface;
11+
use IonBazan\ComposerDiff\Url\UrlGenerator;
1012

1113
class DiffEntry
1214
{
@@ -25,14 +27,25 @@ class DiffEntry
2527
/** @var string */
2628
private $type;
2729

30+
/** @var string|null */
31+
private $compareUrl;
32+
33+
/** @var string|null */
34+
private $projectUrl;
35+
2836
/**
29-
* @param bool $direct
37+
* @param UrlGenerator|null $urlGenerator
38+
* @param bool $direct
3039
*/
31-
public function __construct(OperationInterface $operation, $direct = false)
40+
public function __construct(OperationInterface $operation, $urlGenerator = null, $direct = false)
3241
{
3342
$this->operation = $operation;
3443
$this->direct = $direct;
3544
$this->type = $this->determineType();
45+
46+
if ($urlGenerator instanceof UrlGenerator) {
47+
$this->setUrls($urlGenerator);
48+
}
3649
}
3750

3851
/**
@@ -100,7 +113,15 @@ public function isChange()
100113
}
101114

102115
/**
103-
* @return PackageInterface|null
116+
* @return string
117+
*/
118+
public function getPackageName()
119+
{
120+
return $this->getPackage()->getName();
121+
}
122+
123+
/**
124+
* @return PackageInterface
104125
*/
105126
public function getPackage()
106127
{
@@ -114,9 +135,116 @@ public function getPackage()
114135
return $operation->getPackage();
115136
}
116137

138+
throw new \InvalidArgumentException('Invalid operation');
139+
}
140+
141+
/**
142+
* @return string[]
143+
*/
144+
public function getLicenses()
145+
{
146+
$package = $this->getPackage();
147+
148+
if (!$package instanceof CompletePackageInterface) {
149+
return array();
150+
}
151+
152+
return $package->getLicense();
153+
}
154+
155+
/**
156+
* @return array{
157+
* name: string,
158+
* direct: bool,
159+
* operation: string,
160+
* version_base: string|null,
161+
* version_target: string|null,
162+
* licenses: string[],
163+
* compare: string|null,
164+
* link: string|null,
165+
* }
166+
*/
167+
public function toArray()
168+
{
169+
return array(
170+
'name' => $this->getPackageName(),
171+
'direct' => $this->isDirect(),
172+
'operation' => $this->getType(),
173+
'version_base' => $this->getBaseVersion(),
174+
'version_target' => $this->getTargetVersion(),
175+
'licenses' => $this->getLicenses(),
176+
'compare' => $this->getUrl(),
177+
'link' => $this->getProjectUrl(),
178+
);
179+
}
180+
181+
/**
182+
* @return string|null
183+
*/
184+
public function getBaseVersion()
185+
{
186+
if ($this->operation instanceof UpdateOperation) {
187+
return $this->operation->getInitialPackage()->getFullPrettyVersion();
188+
}
189+
190+
if ($this->operation instanceof UninstallOperation) {
191+
return $this->operation->getPackage()->getFullPrettyVersion();
192+
}
193+
117194
return null;
118195
}
119196

197+
/**
198+
* @return string|null
199+
*/
200+
public function getTargetVersion()
201+
{
202+
if ($this->operation instanceof UpdateOperation) {
203+
return $this->operation->getTargetPackage()->getFullPrettyVersion();
204+
}
205+
206+
if ($this->operation instanceof InstallOperation) {
207+
return $this->operation->getPackage()->getFullPrettyVersion();
208+
}
209+
210+
return null;
211+
}
212+
213+
/**
214+
* @return string|null
215+
*/
216+
public function getUrl()
217+
{
218+
return $this->compareUrl;
219+
}
220+
221+
/**
222+
* @return string|null
223+
*/
224+
public function getProjectUrl()
225+
{
226+
return $this->projectUrl;
227+
}
228+
229+
/**
230+
* @return void
231+
*/
232+
private function setUrls(UrlGenerator $generator)
233+
{
234+
$package = $this->getPackage();
235+
$this->projectUrl = $generator->getProjectUrl($package);
236+
237+
$operation = $this->getOperation();
238+
239+
if ($operation instanceof UpdateOperation) {
240+
$this->compareUrl = $generator->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
241+
242+
return;
243+
}
244+
245+
$this->compareUrl = $generator->getReleaseUrl($package);
246+
}
247+
120248
/**
121249
* @return string
122250
*/

src/Formatter/AbstractFormatter.php

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
namespace IonBazan\ComposerDiff\Formatter;
44

5-
use Composer\DependencyResolver\Operation\InstallOperation;
6-
use Composer\DependencyResolver\Operation\OperationInterface;
7-
use Composer\DependencyResolver\Operation\UninstallOperation;
8-
use Composer\DependencyResolver\Operation\UpdateOperation;
9-
use Composer\Package\CompletePackageInterface;
105
use IonBazan\ComposerDiff\Diff\DiffEntry;
11-
use IonBazan\ComposerDiff\Url\GeneratorContainer;
126
use Symfony\Component\Console\Output\OutputInterface;
137

148
abstract class AbstractFormatter implements Formatter
@@ -18,85 +12,17 @@ abstract class AbstractFormatter implements Formatter
1812
*/
1913
protected $output;
2014

21-
/**
22-
* @var GeneratorContainer
23-
*/
24-
protected $generators;
25-
26-
public function __construct(OutputInterface $output, GeneratorContainer $generators)
15+
public function __construct(OutputInterface $output)
2716
{
2817
$this->output = $output;
29-
$this->generators = $generators;
30-
}
31-
32-
/**
33-
* @return string|null
34-
*/
35-
public function getUrl(DiffEntry $entry)
36-
{
37-
$operation = $entry->getOperation();
38-
39-
if ($operation instanceof UpdateOperation) {
40-
return $this->generators->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
41-
}
42-
43-
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
44-
return $this->generators->getReleaseUrl($operation->getPackage());
45-
}
46-
47-
return null;
48-
}
49-
50-
/**
51-
* @return string|null
52-
*/
53-
public function getLicenses(DiffEntry $entry)
54-
{
55-
if (!$entry->getPackage() instanceof CompletePackageInterface) {
56-
return null;
57-
}
58-
59-
$licenses = $entry->getPackage()->getLicense();
60-
61-
if (empty($licenses)) {
62-
return null;
63-
}
64-
65-
return implode(', ', $licenses);
66-
}
67-
68-
/**
69-
* @return string|null
70-
*/
71-
public function getProjectUrl(OperationInterface $operation)
72-
{
73-
if ($operation instanceof UpdateOperation) {
74-
$package = $operation->getInitialPackage();
75-
}
76-
77-
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
78-
$package = $operation->getPackage();
79-
}
80-
81-
if (!isset($package)) {
82-
return null;
83-
}
84-
85-
return $this->generators->getProjectUrl($package);
8618
}
8719

8820
/**
8921
* @return string
9022
*/
9123
protected function getDecoratedPackageName(DiffEntry $entry)
9224
{
93-
$package = $entry->getPackage();
94-
95-
if (null === $package) {
96-
return '';
97-
}
98-
99-
return $this->terminalLink($this->getProjectUrl($entry->getOperation()), $package->getName());
25+
return $this->terminalLink($entry->getProjectUrl(), $entry->getPackageName());
10026
}
10127

10228
/**

src/Formatter/Formatter.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace IonBazan\ComposerDiff\Formatter;
44

55
use IonBazan\ComposerDiff\Diff\DiffEntries;
6-
use IonBazan\ComposerDiff\Diff\DiffEntry;
76

87
interface Formatter
98
{
@@ -23,14 +22,4 @@ public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withU
2322
* @return void
2423
*/
2524
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses);
26-
27-
/**
28-
* @return string|null
29-
*/
30-
public function getUrl(DiffEntry $entry);
31-
32-
/**
33-
* @return string|null
34-
*/
35-
public function getLicenses(DiffEntry $entry);
3625
}

src/Formatter/FormatterContainer.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace IonBazan\ComposerDiff\Formatter;
44

5-
use IonBazan\ComposerDiff\Url\GeneratorContainer;
65
use Symfony\Component\Console\Output\OutputInterface;
76

87
class FormatterContainer
@@ -14,13 +13,13 @@ class FormatterContainer
1413
*/
1514
private $formatters;
1615

17-
public function __construct(OutputInterface $output, GeneratorContainer $generators)
16+
public function __construct(OutputInterface $output)
1817
{
1918
$this->formatters = array(
20-
'mdtable' => new MarkdownTableFormatter($output, $generators),
21-
'mdlist' => new MarkdownListFormatter($output, $generators),
22-
'github' => new GitHubFormatter($output, $generators),
23-
'json' => new JsonFormatter($output, $generators),
19+
'mdtable' => new MarkdownTableFormatter($output),
20+
'mdlist' => new MarkdownListFormatter($output),
21+
'github' => new GitHubFormatter($output),
22+
'json' => new JsonFormatter($output),
2423
);
2524
}
2625

0 commit comments

Comments
 (0)