diff --git a/src/Commands/RequireCommand.php b/src/Commands/RequireCommand.php index 209be1e..7c92658 100644 --- a/src/Commands/RequireCommand.php +++ b/src/Commands/RequireCommand.php @@ -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); } @@ -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; diff --git a/src/Composer.php b/src/Composer.php index 1eff636..5b2d350 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -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; @@ -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 @@ -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. */ @@ -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. * @@ -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. * diff --git a/src/Package/InstalledFile.php b/src/Package/InstalledFile.php new file mode 100644 index 0000000..8e48da9 --- /dev/null +++ b/src/Package/InstalledFile.php @@ -0,0 +1,75 @@ +> 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; + } + } +} diff --git a/src/Package/LockFile.php b/src/Package/LockFile.php index 4b01f4d..c076390 100644 --- a/src/Package/LockFile.php +++ b/src/Package/LockFile.php @@ -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() )) {