Skip to content

Commit 5bc5fc6

Browse files
authored
Add support for InstalledFile & no-update / no-scripts flags to the Require command (#7)
1 parent 0eed0b4 commit 5bc5fc6

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

src/Commands/RequireCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ final public function __construct(
1515
protected Composer $composer,
1616
protected string $package,
1717
protected bool $dryRun = false,
18-
protected bool $dev = false
18+
protected bool $dev = false,
19+
protected bool $noUpdate = false,
20+
protected bool $noScripts = false,
1921
) {
2022
parent::__construct($composer);
2123
}
@@ -35,6 +37,14 @@ public function arguments(): array
3537
$arguments['--dev'] = true;
3638
}
3739

40+
if ($this->noUpdate) {
41+
$arguments['--no-update'] = true;
42+
}
43+
44+
if ($this->noScripts) {
45+
$arguments['--no-scripts'] = true;
46+
}
47+
3848
$arguments['packages'] = [$this->package];
3949

4050
return $arguments;

src/Composer.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Winter\Packager\Package\Constraint;
1212
use Winter\Packager\Package\DetailedPackage;
1313
use Winter\Packager\Package\DetailedVersionedPackage;
14+
use Winter\Packager\Package\InstalledFile;
1415
use Winter\Packager\Package\LockFile;
1516
use Winter\Packager\Package\Package;
1617
use Winter\Packager\Package\Packagist;
@@ -27,7 +28,7 @@
2728
* @method \Winter\Packager\Commands\Install i(bool $includeDev = true, bool $lockFileOnly = false, bool $ignorePlatformReqs = false, string $installPreference = 'none', bool $ignoreScripts = false, bool $dryRun = false) Install command
2829
* @method \Winter\Packager\Commands\Install install(bool $includeDev = true, bool $lockFileOnly = false, bool $ignorePlatformReqs = false, string $installPreference = 'none', bool $ignoreScripts = false, bool $dryRun = false) Install command
2930
* @method \Winter\Packager\Commands\Remove remove(string $package, bool $dryRun = false, bool $dev = false) Remove command
30-
* @method \Winter\Packager\Commands\RequireCommand require(string $package, bool $dryRun = false, bool $dev = false) Require command
31+
* @method \Winter\Packager\Commands\RequireCommand require(string $package, bool $dryRun = false, bool $dev = false, bool $noUpdate = false, bool $noScripts = false) Require command
3132
* @method \Winter\Packager\Package\Collection search(string $query, ?string $type = null, SearchLimitTo $limitTo = SearchLimitTo::ALL, bool $returnArray = false) Search command
3233
* @method \Winter\Packager\Package\Collection|\Winter\Packager\Package\Package|null show(ShowMode $mode = ShowMode::INSTALLED, ?string $package = null, bool $noDev = false) Show command
3334
* @method \Winter\Packager\Commands\Update update(bool $includeDev = true, bool $lockFileOnly = false, bool $ignorePlatformReqs = false, string $installPreference = 'none', bool $ignoreScripts = false, bool $dryRun = false) Update command
@@ -60,6 +61,11 @@ class Composer
6061
*/
6162
protected ?LockFile $lockFileInstance = null;
6263

64+
/**
65+
* An instance of the installed file class.
66+
*/
67+
protected ?InstalledFile $installedFileInstance = null;
68+
6369
/**
6470
* The name of the dependency directory.
6571
*/
@@ -303,6 +309,18 @@ public function getLockFile(): LockFile
303309
return $this->lockFileInstance;
304310
}
305311

312+
/**
313+
* Gets an instance of the InstalledFile class to read the Composer lock file.
314+
*/
315+
public function getInstalledFile(): InstalledFile
316+
{
317+
if (!isset($this->installedFileInstance)) {
318+
$this->installedFileInstance = new InstalledFile($this);
319+
}
320+
321+
return $this->installedFileInstance;
322+
}
323+
306324
/**
307325
* Gets the name for the vendor package directory.
308326
*
@@ -324,6 +342,16 @@ public function setVendorDir(string $vendorDir): static
324342
return $this;
325343
}
326344

345+
/**
346+
* Gets the directory for the composer vendor directory
347+
*/
348+
public function getComposerVendorDir(): string
349+
{
350+
return rtrim($this->getVendorDir(), DIRECTORY_SEPARATOR)
351+
. DIRECTORY_SEPARATOR
352+
. 'composer';
353+
}
354+
327355
/**
328356
* Gets the timeout for a Composer command.
329357
*

src/Package/InstalledFile.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Winter\Packager\Package;
4+
5+
use Winter\Packager\Composer;
6+
7+
/**
8+
* Installed file class.
9+
*
10+
* This class provides functionality for reading the Composer installed.json. This is used to determine
11+
* details about the packages that are installed in the current project.
12+
*
13+
* @author Luke Towers
14+
* @since 0.4.2
15+
*/
16+
class InstalledFile
17+
{
18+
protected bool $exists = false;
19+
20+
/**
21+
* @var array<string, array<string, string>> Collated package information.
22+
*/
23+
public array $packages = [];
24+
25+
public function __construct(
26+
protected Composer $composer,
27+
) {
28+
if (file_exists($this->getFilePath())) {
29+
$this->exists = true;
30+
$this->collatePackageInfo();
31+
}
32+
}
33+
34+
protected function getFilePath(): string
35+
{
36+
return $this->composer->getComposerVendorDir()
37+
. DIRECTORY_SEPARATOR
38+
. 'installed.json';
39+
}
40+
41+
public function exists(): bool
42+
{
43+
return $this->exists;
44+
}
45+
46+
public function getVersion(string $namespace, string $name): ?string
47+
{
48+
if (!array_key_exists($namespace . '/' . $name, $this->packages)) {
49+
return null;
50+
}
51+
52+
return $this->packages[$namespace . '/' . $name]['version'];
53+
}
54+
55+
public function getType(string $namespace, string $name): ?string
56+
{
57+
if (!array_key_exists($namespace . '/' . $name, $this->packages)) {
58+
return null;
59+
}
60+
61+
return $this->packages[$namespace . '/' . $name]['type'];
62+
}
63+
64+
protected function collatePackageInfo(): void
65+
{
66+
$lockFile = json_decode(
67+
file_get_contents($this->getFilePath()),
68+
flags: JSON_OBJECT_AS_ARRAY
69+
);
70+
71+
foreach ($lockFile['packages'] as $package) {
72+
$this->packages[$package['name']] = $package;
73+
}
74+
}
75+
}

src/Package/LockFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
protected Composer $composer,
3030
) {
3131
if (file_exists(
32-
rtrim($this->composer->getworkDir(), DIRECTORY_SEPARATOR)
32+
rtrim($this->composer->getWorkDir(), DIRECTORY_SEPARATOR)
3333
. DIRECTORY_SEPARATOR
3434
. $this->composer->getLockFilename()
3535
)) {

0 commit comments

Comments
 (0)