Skip to content

Commit d96f4fd

Browse files
committed
Make --autoload mandatory when running as PHAR
When phparkitect is used as a PHAR, no project autoloader is loaded automatically, so --autoload must be explicitly provided to avoid silent class-resolution failures. https://claude.ai/code/session_01DHLEWujytAcAhyLyxELTuS
1 parent 9498feb commit d96f4fd

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/CLI/Command/Check.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ protected function configure(): void
108108
);
109109
}
110110

111+
protected function isRunningAsPhar(): bool
112+
{
113+
return \Phar::running() !== '';
114+
}
115+
111116
protected function execute(InputInterface $input, OutputInterface $output): int
112117
{
113118
ini_set('memory_limit', '-1');
@@ -130,6 +135,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130135
$stdOut = $output;
131136
$output = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
132137

138+
if ($this->isRunningAsPhar() && null === $input->getOption(self::AUTOLOAD_PARAM)) {
139+
$output->writeln('❌ The --autoload option is required when running phparkitect as a PHAR');
140+
141+
return self::ERROR_CODE;
142+
}
143+
133144
$this->printHeadingLine($output);
134145

135146
$config = ConfigBuilder::loadFromFile($rulesFilename)

tests/E2E/Cli/CheckCommandTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Arkitect\Tests\E2E\Cli;
66

7+
use Arkitect\CLI\Command\Check;
78
use Arkitect\CLI\PhpArkitectApplication;
89
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Console\Application;
911
use Symfony\Component\Console\Tester\ApplicationTester;
1012

1113
class CheckCommandTest extends TestCase
@@ -252,6 +254,30 @@ public function test_gitlab_format_output_no_errors(): void
252254
self::assertJsonStringEqualsJsonString($expectedJson, $cmdTester->getDisplay());
253255
}
254256

257+
public function test_autoload_is_required_when_running_as_phar(): void
258+
{
259+
$pharCheck = new class extends Check {
260+
protected function isRunningAsPhar(): bool
261+
{
262+
return true;
263+
}
264+
};
265+
266+
$app = new Application();
267+
$app->setAutoExit(false);
268+
$addMethod = method_exists($app, 'addCommand') ? 'addCommand' : 'add';
269+
$app->$addMethod($pharCheck);
270+
271+
$appTester = new ApplicationTester($app);
272+
$appTester->run(
273+
['check', '--config' => __DIR__.'/../_fixtures/configMvcWithoutErrors.php'],
274+
['capture_stderr_separately' => true]
275+
);
276+
277+
self::assertCommandExitedWithError($appTester);
278+
self::assertStringContainsString('--autoload', $appTester->getErrorOutput());
279+
}
280+
255281
public function test_autoload_file(): void
256282
{
257283
$configFilePath = __DIR__.'/../_fixtures/autoload/phparkitect.php';

0 commit comments

Comments
 (0)