Skip to content

Commit 458af6a

Browse files
committed
Add build function check for current OS and update validation logic
1 parent 66d3af5 commit 458af6a

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

captainhook.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"enabled": true,
1212
"actions": [
1313
{
14-
"action": "composer cs-fix -- --config=.php-cs-fixer.php --dry-run --diff {$STAGED_FILES|of-type:php}",
14+
"action": ".\\vendor\\bin\\php-cs-fixer fix --config=.php-cs-fixer.php --dry-run --diff {$STAGED_FILES|of-type:php}",
1515
"conditions": [
1616
{
1717
"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",

src/Package/Target/php.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use StaticPHP\Attribute\PatchDescription;
1616
use StaticPHP\Config\PackageConfig;
1717
use StaticPHP\DI\ApplicationContext;
18+
use StaticPHP\Exception\EnvironmentException;
1819
use StaticPHP\Exception\SPCException;
1920
use StaticPHP\Exception\WrongUsageException;
2021
use StaticPHP\Package\Package;
@@ -529,6 +530,12 @@ public function build(TargetPackage $package): void
529530
$package->runStage([$this, 'unixBuildSharedExt']);
530531
}
531532

533+
#[BuildFor('Windows')]
534+
public function buildWin(TargetPackage $package): void
535+
{
536+
throw new EnvironmentException('Not implemented');
537+
}
538+
532539
/**
533540
* Patch phpize and php-config if needed
534541
*/

src/StaticPHP/Package/Package.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ public function hasStage(mixed $name): bool
127127
return false;
128128
}
129129

130+
/**
131+
* Check if the package has a build function for the current OS.
132+
*/
133+
public function hasBuildFunctionForCurrentOS(): bool
134+
{
135+
return isset($this->build_functions[PHP_OS_FAMILY]);
136+
}
137+
130138
/**
131139
* Get the name of the package.
132140
*/

src/StaticPHP/Registry/PackageLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ public static function checkLoadedStageEvents(): void
306306
}
307307
}
308308
// check stage exists
309-
if (!$pkg->hasStage($stage_name)) {
309+
// Skip validation if the package has no build function for current OS
310+
// (e.g., libedit has BeforeStage for 'build' but only BuildFor('Darwin'/'Linux'))
311+
if (!$pkg->hasStage($stage_name) && $pkg->hasBuildFunctionForCurrentOS()) {
310312
throw new RegistryException("Package stage [{$stage_name}] is not registered in package [{$package_name}].");
311313
}
312314
}

tests/StaticPHP/Registry/PackageLoaderTest.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ public function testCheckLoadedStageEventsThrowsExceptionForUnknownStage(): void
377377
$this->createTestPackageConfig('test-lib', 'library');
378378
PackageLoader::initPackageInstances();
379379

380+
// Add a build function for current OS so the stage validation is triggered
381+
$package = PackageLoader::getPackage('test-lib');
382+
$package->addBuildFunction(PHP_OS_FAMILY, fn () => null);
383+
380384
// Manually add a before_stage for non-existent stage
381385
$reflection = new \ReflectionClass(PackageLoader::class);
382386
$property = $reflection->getProperty('before_stages');
@@ -417,6 +421,33 @@ public function testCheckLoadedStageEventsThrowsExceptionForUnknownOnlyWhenPacka
417421
PackageLoader::checkLoadedStageEvents();
418422
}
419423

424+
public function testCheckLoadedStageEventsDoesNotThrowForNonCurrentOSPackage(): void
425+
{
426+
$this->createTestPackageConfig('test-lib', 'library');
427+
PackageLoader::initPackageInstances();
428+
429+
// Add a build function for a different OS (not current OS)
430+
$package = PackageLoader::getPackage('test-lib');
431+
$otherOS = PHP_OS_FAMILY === 'Windows' ? 'Linux' : 'Windows';
432+
$package->addBuildFunction($otherOS, fn () => null);
433+
434+
// Manually add a before_stage for 'build' stage
435+
// This should NOT throw an exception because the package has no build function for current OS
436+
$reflection = new \ReflectionClass(PackageLoader::class);
437+
$property = $reflection->getProperty('before_stages');
438+
$property->setAccessible(true);
439+
$property->setValue(null, [
440+
'test-lib' => [
441+
'build' => [[fn () => null, null]],
442+
],
443+
]);
444+
445+
// This should not throw an exception
446+
PackageLoader::checkLoadedStageEvents();
447+
448+
$this->assertTrue(true); // If we get here, the test passed
449+
}
450+
420451
public function testGetBeforeStageCallbacksReturnsCallbacks(): void
421452
{
422453
PackageLoader::initPackageInstances();
@@ -502,13 +533,13 @@ public function testLoadFromPsr4DirLoadsAllClasses(): void
502533
mkdir($psr4Dir, 0755, true);
503534

504535
// Create test class file
505-
$classContent = '<?php
506-
namespace Test\Package;
507-
508-
use StaticPHP\Attribute\Package\Library;
509-
510-
#[Library("test-lib")]
511-
class TestPackage1 {
536+
$classContent = '<?php
537+
namespace Test\Package;
538+
539+
use StaticPHP\Attribute\Package\Library;
540+
541+
#[Library("test-lib")]
542+
class TestPackage1 {
512543
}';
513544
file_put_contents($psr4Dir . '/TestPackage1.php', $classContent);
514545

0 commit comments

Comments
 (0)