Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
69 changes: 69 additions & 0 deletions src/Commands/Remove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Winter\Packager\Commands;

use Winter\Packager\Composer;
use Winter\Packager\Exceptions\CommandException;

class Remove extends BaseCommand
{
/**
* Command constructor.
*/
final public function __construct(
protected Composer $composer,
protected string $package,
protected bool $dryRun = false,
protected bool $dev = false
) {
parent::__construct($composer);
}

/**
* @inheritDoc
*/
public function arguments(): array
{
$arguments = [];

if ($this->dryRun) {
$arguments['--dry-run'] = true;
}

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

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

return $arguments;
}

public function execute()
{
$output = $this->runComposerCommand();
$message = implode(PHP_EOL, $output['output']);

if ($output['code'] !== 0) {
throw new CommandException($message);
}

return $message;
}

/**
* @inheritDoc
*/
protected function requiresWorkDir(): bool
{
return true;
}

/**
* @inheritDoc
*/
public function getCommandName(): string
{
return 'remove';
}
}
74 changes: 74 additions & 0 deletions src/Commands/RequireCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Winter\Packager\Commands;

use Winter\Packager\Composer;
use Winter\Packager\Exceptions\CommandException;
use Winter\Packager\Exceptions\WorkDirException;

class RequireCommand extends BaseCommand
{
/**
* Command constructor.
*/
final public function __construct(
protected Composer $composer,
protected string $package,
protected bool $dryRun = false,
protected bool $dev = false
) {
parent::__construct($composer);
}

/**
* @inheritDoc
*/
public function arguments(): array
{
$arguments = [];

if ($this->dryRun) {
$arguments['--dry-run'] = true;
}

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

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

return $arguments;
}

/**
* @throws CommandException
* @throws WorkDirException
*/
public function execute(): string
{
$output = $this->runComposerCommand();
$message = implode(PHP_EOL, $output['output']);

if ($output['code'] !== 0) {
throw new CommandException($message);
}

return $message;
}

/**
* @inheritDoc
*/
protected function requiresWorkDir(): bool
{
return true;
}

/**
* @inheritDoc
*/
public function getCommandName(): string
{
return 'require';
}
}
11 changes: 9 additions & 2 deletions src/Commands/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ class Search extends BaseCommand
* @param string $query The search query to find packages.
* @param string|null $type The type of package to search for.
* @param SearchLimitTo $limitTo Limit the returned results.
* @param bool $returnArray Return the search results as an array.
*/
final public function __construct(
Composer $composer,
public string $query,
public ?string $type = null,
public SearchLimitTo $limitTo = SearchLimitTo::ALL
public SearchLimitTo $limitTo = SearchLimitTo::ALL,
public bool $returnArray = false,
) {
parent::__construct($composer);
}
Expand All @@ -44,7 +46,12 @@ public function execute()
throw new CommandException(implode(PHP_EOL, $output['output']));
}

$results = json_decode(implode(PHP_EOL, $output['output']), true);
$results = json_decode(implode(PHP_EOL, $output['output']), JSON_OBJECT_AS_ARRAY) ?? [];

if ($this->returnArray) {
return $results;
}

$packages = [];

foreach ($results as $result) {
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function execute()
description: $result['description'] ?? '',
keywords: $result['keywords'] ?? [],
type: $result['type'] ?? 'library',
path: $result['path'] ?? null,
homepage: $result['homepage'] ?? '',
authors: $result['authors'] ?? [],
licenses: $result['licenses'] ?? [],
Expand All @@ -158,6 +159,7 @@ public function execute()
description: $result['description'] ?? '',
keywords: $result['keywords'] ?? [],
type: $result['type'] ?? 'library',
path: $result['path'] ?? null,
homepage: $result['homepage'] ?? '',
authors: $result['authors'] ?? [],
licenses: $result['licenses'] ?? [],
Expand All @@ -178,16 +180,18 @@ public function execute()
$name,
$result['description'] ?? '',
$result['type'] ?? '',
$result['path'] ?? null,
$result['version'],
$result['latest'] ?? '',
VersionStatus::tryFrom($result['latest-status'] ?? '') ?? VersionStatus::UP_TO_DATE
VersionStatus::tryFrom($result['latest-status'] ?? '') ?? VersionStatus::UP_TO_DATE,
);
} else {
return Composer::newPackage(
$namespace,
$name,
$result['description'] ?? '',
$result['type'] ?? '',
$result['path'] ?? null
);
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/Commands/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Update extends BaseCommand
* @param boolean $ignorePlatformReqs Ignore platform reqs when running the update.
* @param string $installPreference Set an install preference - must be one of "none", "dist", "source"
* @param boolean $ignoreScripts Ignores scripts that run after Composer events.
* @param ?string $package Specify a specific package to update.
* @return void
*/
final public function __construct(
Expand All @@ -74,18 +75,15 @@ final public function __construct(
protected bool $ignorePlatformReqs = false,
protected string $installPreference = 'none',
protected bool $ignoreScripts = false,
protected bool $dryRun = false
protected bool $dryRun = false,
protected ?string $package = null
) {
parent::__construct($composer);

$this->includeDev = $includeDev;
$this->lockFileOnly = $lockFileOnly;
$this->ignorePlatformReqs = $ignorePlatformReqs;
$this->ignoreScripts = $ignoreScripts;
$this->dryRun = $dryRun;

if (in_array($installPreference, [self::PREFER_NONE, self::PREFER_DIST, self::PREFER_SOURCE])) {
$this->installPreference = $installPreference;
if (!in_array($this->installPreference, [self::PREFER_NONE, self::PREFER_DIST, self::PREFER_SOURCE])) {
throw new \InvalidArgumentException(
'installPreference is not an allowed value `' . $this->installPreference . '`. See: "none", "dist", "source"'
);
}
}

Expand Down Expand Up @@ -337,6 +335,10 @@ protected function arguments(): array
$arguments['--prefer-' . $this->installPreference] = true;
}

if ($this->package) {
$arguments['packages'] = [$this->package];
}

return $arguments;
}
}
12 changes: 9 additions & 3 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Throwable;
use Winter\Packager\Commands\Command;
use Winter\Packager\Enums\SearchLimitTo;
use Winter\Packager\Enums\ShowMode;
use Winter\Packager\Package\Collection;
use Winter\Packager\Package\Constraint;
use Winter\Packager\Package\DetailedPackage;
Expand All @@ -23,8 +25,10 @@
* @since 0.1.0
* @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\Package\Collection search() Search command
* @method \Winter\Packager\Package\Collection|\Winter\Packager\Package\Package|null show() Show 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\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
* @method string version(string $detail = 'version') Version command
*/
Expand Down Expand Up @@ -79,9 +83,11 @@ class Composer
* @var array<string, string|Command> A list of supported commands
*/
protected array $commands = [
'list' => \Winter\Packager\Commands\ListCommand::class,
'i' => \Winter\Packager\Commands\Install::class,
'install' => \Winter\Packager\Commands\Install::class,
'list' => \Winter\Packager\Commands\ListCommand::class,
'require' => \Winter\Packager\Commands\RequireCommand::class,
'remove' => \Winter\Packager\Commands\Remove::class,
'search' => \Winter\Packager\Commands\Search::class,
'show' => \Winter\Packager\Commands\Show::class,
'update' => \Winter\Packager\Commands\Update::class,
Expand Down
3 changes: 2 additions & 1 deletion src/Package/DetailedPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
string $name,
string $description = '',
protected string $type = 'library',
protected ?string $path = null,
protected array $keywords = [],
protected string $homepage = '',
protected array $authors = [],
Expand All @@ -42,7 +43,7 @@ public function __construct(
protected array $replaces = [],
protected string $readme = '',
) {
parent::__construct($namespace, $name, $description, $type);
parent::__construct($namespace, $name, $description, $type, $path);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Package/DetailedVersionedPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct(
string $name,
string $description = '',
protected string $type = 'library',
protected ?string $path = null,
protected array $keywords = [],
protected string $homepage = '',
protected array $authors = [],
Expand All @@ -56,6 +57,7 @@ public function __construct(
$name,
$description,
$type,
$path,
$keywords,
$homepage,
$authors,
Expand Down
11 changes: 11 additions & 0 deletions src/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(
protected string $name,
protected string $description = '',
protected string $type = '',
protected ?string $path = null,
) {
}

Expand Down Expand Up @@ -74,6 +75,16 @@ public function setType(string $type): void
$this->type = $type;
}

public function getPath(): ?string
{
return $this->path;
}

public function setPath(string $path): void
{
$this->path = $path;
}

public function toDetailed(): DetailedPackage
{
$details = Packagist::getPackage($this->namespace, $this->name);
Expand Down
2 changes: 1 addition & 1 deletion src/Package/Packagist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Packagist
protected const PACKAGIST_API_URL = 'https://packagist.org/';
protected const PACKAGIST_REPO_URL = 'https://repo.packagist.org/p2/';

protected static string $agent = 'Winter Packager <no-reply@example.com>';
protected static string $agent = 'Winter Packager <hello@wintercms.com>';

protected static ?Storage $storage = null;

Expand Down
3 changes: 2 additions & 1 deletion src/Package/VersionedPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function __construct(
string $name,
string $description = '',
string $type = '',
string $path = '',
protected string $version = '',
protected string $latestVersion = '',
protected VersionStatus $updateStatus = VersionStatus::UP_TO_DATE,
) {
parent::__construct($namespace, $name, $description, $type);
parent::__construct($namespace, $name, $description, $type, $path);

$this->versionNormalized = $this->normalizeVersion($this->version);
}
Expand Down
Loading