Skip to content

Commit 7b16f68

Browse files
committed
Allow package implementation using parent class functions
1 parent 78234ef commit 7b16f68

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/StaticPHP/Package/PackageLoader.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ public static function loadFromClass(mixed $class): void
143143
}
144144
self::$loaded_classes[$class_name] = true;
145145

146-
$instance_class = $refClass->newInstance();
147-
148146
$attributes = $refClass->getAttributes();
149147
foreach ($attributes as $attribute) {
150148
$pkg = null;
@@ -160,6 +158,19 @@ public static function loadFromClass(mixed $class): void
160158
if ($package_type === null) {
161159
throw new WrongUsageException("Package [{$attribute_instance->name}] not defined in config, please check your config files.");
162160
}
161+
162+
// if class has parent class and matches the attribute instance, use custom class
163+
if ($refClass->getParentClass() !== false) {
164+
if (is_a($class_name, Package::class, true)) {
165+
self::$packages[$attribute_instance->name] = new $class_name($attribute_instance->name, $package_type);
166+
$instance_class = self::$packages[$attribute_instance->name];
167+
}
168+
}
169+
170+
if (!isset($instance_class)) {
171+
$instance_class = $refClass->newInstance();
172+
}
173+
163174
$pkg = self::$packages[$attribute_instance->name];
164175

165176
// validate package type matches
@@ -272,9 +283,6 @@ private static function bindCustomPhpConfigureArg(Package $pkg, object $attr, ca
272283

273284
private static function addBuildFunction(Package $pkg, object $attr, callable $fn): void
274285
{
275-
if (!$pkg instanceof LibraryPackage) {
276-
throw new ValidationException("Class [{$pkg->getName()}] must implement LibraryPackage for BuildFor attribute.");
277-
}
278286
$pkg->addBuildFunction($attr->os, $fn);
279287
}
280288
}

0 commit comments

Comments
 (0)