-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial composer support for plugin management
- Loading branch information
Showing
8 changed files
with
441 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace System\Classes\Packager; | ||
|
||
use System\Classes\Packager\Commands\RemoveCommand; | ||
use System\Classes\Packager\Commands\ShowCommand; | ||
use System\Classes\Packager\Commands\UpdateCommand; | ||
use System\Classes\Packager\Commands\RequireCommand; | ||
use Winter\Packager\Composer as PackagerComposer; | ||
|
||
/** | ||
* @class Composer | ||
* @method static i(): array | ||
* @method static install(): array | ||
* @method static search(string $query, ?string $type = null, bool $onlyNames = false, bool $onlyVendors = false): \Winter\Packager\Commands\Search | ||
* @method static show(?string $mode = 'installed', string $package = null, bool $noDev = false, bool $path = false): object | ||
* @method static update(bool $includeDev = true, bool $lockFileOnly = false, bool $ignorePlatformReqs = false, string $installPreference = 'none', bool $ignoreScripts = false, bool $dryRun = false, ?string $package = null): \Winter\Packager\Commands\Update | ||
* @method static remove(?string $package = null, bool $dryRun = false): array | ||
* @method static require(?string $package = null, bool $dryRun = false, bool $dev = false): array | ||
* @method static version(string $detail = 'version'): array<string, string>|string | ||
*/ | ||
class Composer | ||
{ | ||
public const COMPOSER_CACHE_KEY = 'winter.system.composer'; | ||
|
||
protected static PackagerComposer $composer; | ||
|
||
public static function make(bool $fresh = false): PackagerComposer | ||
{ | ||
if (!$fresh && isset(static::$composer)) { | ||
return static::$composer; | ||
} | ||
|
||
static::$composer = new PackagerComposer(); | ||
static::$composer->setWorkDir(base_path()); | ||
|
||
static::$composer->setCommand('show', new ShowCommand(static::$composer)); | ||
static::$composer->setCommand('update', new UpdateCommand(static::$composer)); | ||
static::$composer->setCommand('remove', new RemoveCommand(static::$composer)); | ||
static::$composer->setCommand('require', new RequireCommand(static::$composer)); | ||
|
||
return static::$composer; | ||
} | ||
|
||
public static function __callStatic(string $name, array $args = []): mixed | ||
{ | ||
if (!isset(static::$composer)) { | ||
static::make(); | ||
} | ||
|
||
return static::$composer->{$name}(...$args); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
modules/system/classes/packager/commands/RemoveCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace System\Classes\Packager\Commands; | ||
|
||
use Cache; | ||
use System\Classes\Packager\ComposerFactory; | ||
use Winter\Packager\Commands\BaseCommand; | ||
use Winter\Packager\Exceptions\CommandException; | ||
|
||
class RemoveCommand extends BaseCommand | ||
{ | ||
protected ?string $package = null; | ||
protected bool $dryRun = false; | ||
|
||
/** | ||
* Command handler. | ||
* | ||
* @param string|null $package | ||
* @param boolean $dryRun | ||
* @return void | ||
* @throws CommandException | ||
*/ | ||
public function handle(?string $package = null, bool $dryRun = false): void | ||
{ | ||
if (!$package) { | ||
throw new CommandException('Must provide a package'); | ||
} | ||
|
||
$this->package = $package; | ||
$this->dryRun = $dryRun; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function arguments(): array | ||
{ | ||
$arguments = []; | ||
|
||
if ($this->dryRun) { | ||
$arguments['--dry-run'] = 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); | ||
} | ||
|
||
Cache::forget(ComposerFactory::COMPOSER_CACHE_KEY); | ||
|
||
return $message; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getCommandName(): string | ||
{ | ||
return 'remove'; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
modules/system/classes/packager/commands/RequireCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace System\Classes\Packager\Commands; | ||
|
||
use Cache; | ||
use System\Classes\Packager\ComposerFactory; | ||
use Winter\Packager\Commands\BaseCommand; | ||
use Winter\Packager\Exceptions\CommandException; | ||
|
||
class RequireCommand extends BaseCommand | ||
{ | ||
protected ?string $package = null; | ||
protected bool $dryRun = false; | ||
protected bool $dev = false; | ||
|
||
/** | ||
* Command handler. | ||
* | ||
* @param string|null $package | ||
* @param boolean $dryRun | ||
* @param boolean $dev | ||
* @return void | ||
* @throws CommandException | ||
*/ | ||
public function handle(?string $package = null, bool $dryRun = false, bool $dev = false): void | ||
{ | ||
if (!$package) { | ||
throw new CommandException('Must provide a package'); | ||
} | ||
|
||
$this->package = $package; | ||
$this->dryRun = $dryRun; | ||
$this->dev = $dev; | ||
} | ||
|
||
/** | ||
* @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); | ||
} | ||
|
||
Cache::forget(ComposerFactory::COMPOSER_CACHE_KEY); | ||
|
||
return $message; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getCommandName(): string | ||
{ | ||
return 'require'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace System\Classes\Packager\Commands; | ||
|
||
use Winter\Packager\Commands\Show; | ||
use Winter\Packager\Exceptions\CommandException; | ||
|
||
class ShowCommand extends Show | ||
{ | ||
protected bool $path = false; | ||
|
||
/** | ||
* Command handler. | ||
* | ||
* The mode can be one of the following: | ||
* - `installed`: Show installed packages | ||
* - `locked`: Show locked packages | ||
* - `platform`: Show platform requirements | ||
* - `available`: Show all available packages | ||
* - `self`: Show the current package | ||
* - `path`: Show the package path | ||
* - `tree`: Show packages in a dependency tree | ||
* - `outdated`: Show only outdated packages | ||
* - `direct`: Show only direct dependencies | ||
* | ||
* @param string|null $mode | ||
* @param string|null $package | ||
* @param boolean $noDev | ||
* @param boolean $path | ||
* @return void | ||
*/ | ||
public function handle(?string $mode = 'installed', string $package = null, bool $noDev = false, bool $path = false): void | ||
{ | ||
parent::handle($mode, $package, $noDev); | ||
|
||
$this->path = $path; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function arguments(): array | ||
{ | ||
$arguments = []; | ||
|
||
if (!empty($this->package)) { | ||
$arguments['package'] = $this->package; | ||
} | ||
|
||
if ($this->mode !== 'installed') { | ||
$arguments['--' . $this->mode] = true; | ||
} | ||
|
||
if ($this->noDev) { | ||
$arguments['--no-dev'] = true; | ||
} | ||
|
||
if ($this->path) { | ||
$arguments['--path'] = true; | ||
} | ||
|
||
$arguments['--format'] = 'json'; | ||
|
||
return $arguments; | ||
} | ||
} |
Oops, something went wrong.