|
4 | 4 |
|
5 | 5 | use Winter\Packager\Commands\Command; |
6 | 6 | use Throwable; |
| 7 | +use Winter\Packager\Package\Collection; |
| 8 | +use Winter\Packager\Package\Constraint; |
| 9 | +use Winter\Packager\Package\DetailedPackage; |
| 10 | +use Winter\Packager\Package\Package; |
| 11 | +use Winter\Packager\Package\VersionedPackage; |
7 | 12 |
|
8 | 13 | /** |
9 | 14 | * Represents a Composer instance. |
@@ -68,6 +73,18 @@ class Composer |
68 | 73 | 'version' => \Winter\Packager\Commands\Version::class, |
69 | 74 | ]; |
70 | 75 |
|
| 76 | + /** |
| 77 | + * @var array<string, string> Map of classes to use for packages, constraints and collections. This allows for |
| 78 | + * custom classes to be used for these objects, should a developer wish to extend the functionality. |
| 79 | + */ |
| 80 | + protected static $packageClasses = [ |
| 81 | + 'package' => \Winter\Packager\Package\Package::class, |
| 82 | + 'versionedPackage' => \Winter\Packager\Package\VersionedPackage::class, |
| 83 | + 'detailedPackage' => \Winter\Packager\Package\DetailedPackage::class, |
| 84 | + 'collection' => \Winter\Packager\Package\Collection::class, |
| 85 | + 'constraint' => \Winter\Packager\Package\Constraint::class, |
| 86 | + ]; |
| 87 | + |
71 | 88 | /** |
72 | 89 | * Constructor |
73 | 90 | * |
@@ -347,4 +364,82 @@ public function getAuditAbandoned(): string |
347 | 364 | { |
348 | 365 | return $this->auditAbandoned; |
349 | 366 | } |
| 367 | + |
| 368 | + /** |
| 369 | + * Defines the classes to use for packages, constraints and collections. |
| 370 | + * |
| 371 | + * You may either overwrite a single type by providing both a `$type` and `$class` as a string, or change multiple |
| 372 | + * by providing an array of types and classes. |
| 373 | + * |
| 374 | + * It is up to you to ensure that the classes you provide are compatible with the classes they are replacing - at |
| 375 | + * the very least, you should extend the classes used by default. |
| 376 | + * |
| 377 | + * @param string|array<string, string> $type |
| 378 | + * @param string|null $class |
| 379 | + */ |
| 380 | + public static function setPackageClass(string|array $type, ?string $class = null): void |
| 381 | + { |
| 382 | + if (is_array($type)) { |
| 383 | + foreach ($type as $t => $c) { |
| 384 | + static::setPackageClass($t, $c); |
| 385 | + } |
| 386 | + return; |
| 387 | + } |
| 388 | + |
| 389 | + if (!array_key_exists($type, static::$packageClasses)) { |
| 390 | + throw new \Exception( |
| 391 | + sprintf( |
| 392 | + 'Invalid package class type "%s"', |
| 393 | + $type |
| 394 | + ) |
| 395 | + ); |
| 396 | + } |
| 397 | + |
| 398 | + static::$packageClasses[$type] = $class; |
| 399 | + } |
| 400 | + |
| 401 | + /** |
| 402 | + * Create a new package instance. |
| 403 | + */ |
| 404 | + public static function newPackage(mixed ...$arguments): Package |
| 405 | + { |
| 406 | + $class = static::$packageClasses['package']; |
| 407 | + return new $class(...$arguments); |
| 408 | + } |
| 409 | + |
| 410 | + /** |
| 411 | + * Create a new versioned package instance. |
| 412 | + */ |
| 413 | + public static function newVersionedPackage(mixed ...$arguments): VersionedPackage |
| 414 | + { |
| 415 | + $class = static::$packageClasses['versionedPackage']; |
| 416 | + return new $class(...$arguments); |
| 417 | + } |
| 418 | + |
| 419 | + /** |
| 420 | + * Create a new detailed package instance. |
| 421 | + */ |
| 422 | + public static function newDetailedPackage(mixed ...$arguments): DetailedPackage |
| 423 | + { |
| 424 | + $class = static::$packageClasses['detailedPackage']; |
| 425 | + return new $class(...$arguments); |
| 426 | + } |
| 427 | + |
| 428 | + /** |
| 429 | + * Create a new package collection instance. |
| 430 | + */ |
| 431 | + public static function newCollection(mixed ...$arguments): Collection |
| 432 | + { |
| 433 | + $class = static::$packageClasses['collection']; |
| 434 | + return new $class(...$arguments); |
| 435 | + } |
| 436 | + |
| 437 | + /** |
| 438 | + * Create a new constraint instance. |
| 439 | + */ |
| 440 | + public static function newConstraint(mixed ...$arguments): Constraint |
| 441 | + { |
| 442 | + $class = static::$packageClasses['constraint']; |
| 443 | + return new $class(...$arguments); |
| 444 | + } |
350 | 445 | } |
0 commit comments