Skip to content

Commit 7ae60ad

Browse files
committed
Allow base package, constraint and collection classes to be overridden
1 parent 1c082d6 commit 7ae60ad

File tree

3 files changed

+106
-15
lines changed

3 files changed

+106
-15
lines changed

src/Commands/Search.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Winter\Packager\Commands;
44

5+
use Winter\Packager\Composer;
56
use Winter\Packager\Exceptions\CommandException;
6-
use Winter\Packager\Package\Package;
7-
use Winter\Packager\Package\Collection;
87

98
/**
109
* Search command.
@@ -72,14 +71,14 @@ public function execute()
7271
foreach ($results as $result) {
7372
[$namespace, $name] = preg_split('/\//', $result['name'], 2);
7473

75-
$packages[] = new Package(
74+
$packages[] = Composer::newPackage(
7675
$namespace,
7776
$name,
7877
$result['description'] ?? ''
7978
);
8079
}
8180

82-
return new Collection($packages);
81+
return Composer::newCollection($packages);
8382
}
8483

8584
/**

src/Commands/Show.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
namespace Winter\Packager\Commands;
44

5+
use Winter\Packager\Composer;
56
use Winter\Packager\Enums\VersionStatus;
67
use Winter\Packager\Exceptions\CommandException;
7-
use Winter\Packager\Package\Collection;
8-
use Winter\Packager\Package\DetailedPackage;
9-
use Winter\Packager\Package\Package;
10-
use Winter\Packager\Package\VersionedPackage;
118

129
/**
1310
* Show command.
@@ -115,7 +112,7 @@ public function execute()
115112
[$namespace, $name] = $this->nameSplit($result['name']);
116113

117114
if (isset($result['version'])) {
118-
$packages[] = new VersionedPackage(
115+
$packages[] = Composer::newVersionedPackage(
119116
$namespace,
120117
$name,
121118
$result['description'] ?? '',
@@ -124,34 +121,34 @@ public function execute()
124121
VersionStatus::tryFrom($result['latest-status'] ?? '') ?? VersionStatus::UP_TO_DATE
125122
);
126123
} else {
127-
$packages[] = new Package(
124+
$packages[] = Composer::newPackage(
128125
$namespace,
129126
$name,
130127
$result['description'] ?? '',
131128
);
132129
}
133130
}
134131

135-
return new Collection($packages);
132+
return Composer::newCollection($packages);
136133
} elseif (is_null($this->package) && $this->mode === 'available') {
137134
// Convert entire available package list into a package collection
138135
foreach ($results['available'] as $result) {
139136
[$namespace, $name] = $this->nameSplit($result['name']);
140137

141-
$packages[] = new Package(
138+
$packages[] = Composer::newPackage(
142139
$namespace,
143140
$name,
144141
$result['description'] ?? '',
145142
);
146143
}
147144

148-
return new Collection($packages);
145+
return Composer::newCollection($packages);
149146
} elseif ($this->mode === 'self') {
150147
$result = $results;
151148
[$namespace, $name] = $this->nameSplit($result['name']);
152149

153150
// Return the current package
154-
return new DetailedPackage(
151+
return Composer::newDetailedPackage(
155152
$namespace,
156153
$name,
157154
$result['description'] ?? '',
@@ -170,7 +167,7 @@ public function execute()
170167
$result = $results;
171168
[$namespace, $name] = $this->nameSplit($result['name']);
172169

173-
return new DetailedPackage(
170+
return Composer::newDetailedPackage(
174171
$namespace,
175172
$name,
176173
$result['description'] ?? '',

src/Composer.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use Winter\Packager\Commands\Command;
66
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;
712

813
/**
914
* Represents a Composer instance.
@@ -68,6 +73,18 @@ class Composer
6873
'version' => \Winter\Packager\Commands\Version::class,
6974
];
7075

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+
7188
/**
7289
* Constructor
7390
*
@@ -347,4 +364,82 @@ public function getAuditAbandoned(): string
347364
{
348365
return $this->auditAbandoned;
349366
}
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+
}
350445
}

0 commit comments

Comments
 (0)