Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Commands/RequireCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ final public function __construct(
protected Composer $composer,
protected string $package,
protected bool $dryRun = false,
protected bool $dev = false
protected bool $dev = false,
protected bool $noUpdate = false,
protected bool $noScripts = false,
) {
parent::__construct($composer);
}
Expand All @@ -35,6 +37,14 @@ public function arguments(): array
$arguments['--dev'] = true;
}

if ($this->noUpdate) {
$arguments['--no-update'] = true;
}

if ($this->noScripts) {
$arguments['--no-scripts'] = true;
}

$arguments['packages'] = [$this->package];

return $arguments;
Expand Down
30 changes: 29 additions & 1 deletion src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Winter\Packager\Package\Constraint;
use Winter\Packager\Package\DetailedPackage;
use Winter\Packager\Package\DetailedVersionedPackage;
use Winter\Packager\Package\InstalledFile;
use Winter\Packager\Package\LockFile;
use Winter\Packager\Package\Package;
use Winter\Packager\Package\Packagist;
Expand All @@ -27,7 +28,7 @@
* @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
* @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
* @method \Winter\Packager\Commands\Remove remove(string $package, bool $dryRun = false, bool $dev = false) Remove command
* @method \Winter\Packager\Commands\RequireCommand require(string $package, bool $dryRun = false, bool $dev = false) Require command
* @method \Winter\Packager\Commands\RequireCommand require(string $package, bool $dryRun = false, bool $dev = false, bool $noUpdate = false, bool $noScripts = false) Require command
* @method \Winter\Packager\Package\Collection search(string $query, ?string $type = null, SearchLimitTo $limitTo = SearchLimitTo::ALL, bool $returnArray = false) Search command
* @method \Winter\Packager\Package\Collection|\Winter\Packager\Package\Package|null show(ShowMode $mode = ShowMode::INSTALLED, ?string $package = null, bool $noDev = false) Show command
* @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
Expand Down Expand Up @@ -60,6 +61,11 @@ class Composer
*/
protected ?LockFile $lockFileInstance = null;

/**
* An instance of the installed file class.
*/
protected ?InstalledFile $installedFileInstance = null;

/**
* The name of the dependency directory.
*/
Expand Down Expand Up @@ -303,6 +309,18 @@ public function getLockFile(): LockFile
return $this->lockFileInstance;
}

/**
* Gets an instance of the InstalledFile class to read the Composer lock file.
*/
public function getInstalledFile(): InstalledFile
{
if (!isset($this->installedFileInstance)) {
$this->installedFileInstance = new InstalledFile($this);
}

return $this->installedFileInstance;
}

/**
* Gets the name for the vendor package directory.
*
Expand All @@ -324,6 +342,16 @@ public function setVendorDir(string $vendorDir): static
return $this;
}

/**
* Gets the directory for the composer vendor directory
*/
public function getComposerVendorDir(): string
{
return rtrim($this->getVendorDir(), DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR
. 'composer';
}

/**
* Gets the timeout for a Composer command.
*
Expand Down
75 changes: 75 additions & 0 deletions src/Package/InstalledFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Winter\Packager\Package;

use Winter\Packager\Composer;

/**
* Installed file class.
*
* This class provides functionality for reading the Composer installed.json. This is used to determine
* details about the packages that are installed in the current project.
*
* @author Luke Towers
* @since 0.4.2
*/
class InstalledFile
{
protected bool $exists = false;

/**
* @var array<string, array<string, string>> Collated package information.
*/
public array $packages = [];

public function __construct(
protected Composer $composer,
) {
if (file_exists($this->getFilePath())) {
$this->exists = true;
$this->collatePackageInfo();
}
}

protected function getFilePath(): string
{
return $this->composer->getComposerVendorDir()
. DIRECTORY_SEPARATOR
. 'installed.json';
}

public function exists(): bool
{
return $this->exists;
}

public function getVersion(string $namespace, string $name): ?string
{
if (!array_key_exists($namespace . '/' . $name, $this->packages)) {
return null;
}

return $this->packages[$namespace . '/' . $name]['version'];
}

public function getType(string $namespace, string $name): ?string
{
if (!array_key_exists($namespace . '/' . $name, $this->packages)) {
return null;
}

return $this->packages[$namespace . '/' . $name]['type'];
}

protected function collatePackageInfo(): void
{
$lockFile = json_decode(
file_get_contents($this->getFilePath()),
flags: JSON_OBJECT_AS_ARRAY
);

foreach ($lockFile['packages'] as $package) {
$this->packages[$package['name']] = $package;
}
}
}
2 changes: 1 addition & 1 deletion src/Package/LockFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
protected Composer $composer,
) {
if (file_exists(
rtrim($this->composer->getworkDir(), DIRECTORY_SEPARATOR)
rtrim($this->composer->getWorkDir(), DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR
. $this->composer->getLockFilename()
)) {
Expand Down
Loading