Skip to content

Commit

Permalink
Merge pull request #402 from jolicode/fix-phar-2
Browse files Browse the repository at this point in the history
Fix phar
  • Loading branch information
pyrech authored Apr 9, 2024
2 parents a4fbe8e + cc1506f commit 0a58ca6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 39 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ parameters:
count: 1
path: examples/args.php

-
message: "#^Class RepackedApplication not found\\.$#"
count: 1
path: src/Console/ApplicationFactory.php

-
message: "#^Default value of the parameter \\#1 \\$data \\(array\\{\\}\\) of method Castor\\\\Context\\:\\:__construct\\(\\) is incompatible with type array\\{name\\: string, production\\: bool, foo\\?\\: string\\}\\.$#"
count: 1
Expand Down
27 changes: 17 additions & 10 deletions src/Console/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,24 @@ public static function create(): SymfonyApplication
{
$errorHandler = self::configureDebug();

try {
$rootDir = PathHelper::getRoot();
} catch (\RuntimeException $e) {
return new CastorFileNotFoundApplication($e);
if (class_exists(\RepackedApplication::class)) {
$rootDir = \RepackedApplication::ROOT_DIR;
$repacked = true;
} else {
try {
$rootDir = PathHelper::getRoot();
} catch (\RuntimeException $e) {
return new CastorFileNotFoundApplication($e);
}
$repacked = false;
}

$container = self::buildContainer();
$container = self::buildContainer($repacked);
$container->getParameterBag()->add([
'root_dir' => $rootDir,
'cache_dir' => PlatformHelper::getCacheDirectory(),
'event_dispatcher.event_aliases' => ConsoleEvents::ALIASES,
'repacked' => $repacked,
]);
$container->compile();

Expand All @@ -84,7 +91,7 @@ private static function configureDebug(): ErrorHandler
return $errorHandler;
}

private static function buildContainer(): ContainerBuilder
private static function buildContainer(bool $repacked): ContainerBuilder
{
$container = new ContainerBuilder();

Expand All @@ -107,12 +114,12 @@ private static function buildContainer(): ContainerBuilder
$phpLoader = new PhpFileLoader($container, new FileLocator());
$instanceof = [];
$configurator = new ContainerConfigurator($container, $phpLoader, $instanceof, __DIR__, __FILE__);
self::configureContainer($configurator);
self::configureContainer($configurator, $repacked);

return $container;
}

private static function configureContainer(ContainerConfigurator $c): void
private static function configureContainer(ContainerConfigurator $c, bool $repacked): void
{
$services = $c->services();

Expand Down Expand Up @@ -186,7 +193,7 @@ private static function configureContainer(ContainerConfigurator $c): void
->synthetic()
;

$app = $services->set(Application::class, class_exists(\RepackedApplication::class) ? \RepackedApplication::class : null)
$app = $services->set(Application::class, $repacked ? \RepackedApplication::class : null)
->public()
->args([
'$containerBuilder' => service(ContainerInterface::class),
Expand All @@ -195,7 +202,7 @@ private static function configureContainer(ContainerConfigurator $c): void
->call('setDispatcher', [service(EventDispatcherInterface::class)])
->call('setCatchErrors', [true])
;
if (!class_exists(\RepackedApplication::class)) {
if (!$repacked) {
$app
->call('add', [service(RepackCommand::class)])
->call('add', [service(CompileCommand::class)])
Expand Down
5 changes: 4 additions & 1 deletion src/Function/FunctionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Castor\Exception\FunctionConfigurationException;
use Castor\Helper\Slugger;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Process\Exception\ExceptionInterface;
use Symfony\Component\Process\Process;
use Symfony\Contracts\Cache\CacheInterface;
Expand All @@ -28,6 +29,8 @@ public function __construct(
private readonly Slugger $slugger,
private readonly CacheInterface $cache,
private readonly string $rootDir,
#[Autowire('%repacked%')]
private readonly bool $repacked,
) {
}

Expand Down Expand Up @@ -85,7 +88,7 @@ private function doResolveFunctions(array $previousFunctions, array $previousCla
}

// Symfony task in repacked application are not supported
if (class_exists(\RepackedApplication::class)) {
if ($this->repacked) {
return;
}
// Nor in static binary
Expand Down
30 changes: 12 additions & 18 deletions src/Import/Remote/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,15 @@ public function setConfiguration(array $configuration): void

public function update(bool $force = false, bool $displayProgress = true): void
{
$composer = (new ExecutableFinder())->find('composer');

if (!$composer) {
throw new ComposerError('The "composer" executable was not found. In order to use remote import, please make sure that Composer is installed and available in your PATH.');
}

$dir = PathHelper::getRoot() . self::VENDOR_DIR;

$this->filesystem->mkdir($dir);

file_put_contents($dir . '.gitignore', "*\n");
if ($force || !$this->isInstalled($dir)) {
$this->filesystem->mkdir($dir);

$this->writeJsonFile($dir);
file_put_contents($dir . '.gitignore', "*\n");

$ran = false;
$this->writeJsonFile($dir);

if ($force || !$this->isInstalled($dir)) {
$progressIndicator = null;
if ($displayProgress) {
$progressIndicator = new ProgressIndicator($this->output, null, 100, ['', '', '', '', '', '', '', '']);
Expand All @@ -87,11 +79,7 @@ public function update(bool $force = false, bool $displayProgress = true): void
$progressIndicator->finish('<info>Remote packages imported</info>');
}
$this->writeInstalled($dir);

$ran = true;
}

if (!$ran) {
} else {
$this->logger->debug('Packages were already required, no need to run Composer.');
}
}
Expand All @@ -106,13 +94,19 @@ public function remove(): void
*/
private function run(array $args, callable $callback): void
{
$composer = (new ExecutableFinder())->find('composer');

if (!$composer) {
throw new ComposerError('The "composer" executable was not found. In order to use remote import, please make sure that Composer is installed and available in your PATH.');
}

$this->logger->debug('Running Composer command.', [
'args' => implode(' ', $args),
]);

$dir = PathHelper::getRoot() . self::VENDOR_DIR;

$process = new Process(['composer', ...$args, '--working-dir', $dir]);
$process = new Process([$composer, ...$args, '--working-dir', $dir]);
$process->setEnv([
'COMPOSER_VENDOR_DIR' => $dir,
]);
Expand Down
9 changes: 2 additions & 7 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public function boot(InputInterface $input, OutputInterface $output): void
{
$this->eventDispatcher->dispatch(new BeforeBootEvent($this->application));

$functionsRootDir = $this->rootDir;
if (class_exists(\RepackedApplication::class)) {
$functionsRootDir = \RepackedApplication::ROOT_DIR;
}

$this->addMount(new Mount($functionsRootDir));
$this->addMount(new Mount($this->rootDir));

$hasLoadedPackages = false;

Expand Down Expand Up @@ -104,7 +99,7 @@ private function load(

// Apply mounts
foreach ($descriptorsCollection->taskDescriptors as $taskDescriptor) {
if ($mount->path) {
if ($mount->path !== $this->rootDir) {
$taskDescriptor->workingDirectory = $mount->path;
}
if ($mount->namespacePrefix) {
Expand Down
5 changes: 4 additions & 1 deletion src/Listener/GenerateStubsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Castor\Stub\StubsGenerator;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

class GenerateStubsListener
{
public function __construct(
private readonly StubsGenerator $stubsGenerator,
#[Autowire('%repacked%')]
private readonly bool $repacked,
) {
}

Expand All @@ -18,7 +21,7 @@ public function __construct(
#[AsEventListener()]
public function generateStubs(ConsoleCommandEvent $event): void
{
if (class_exists(\RepackedApplication::class)) {
if ($this->repacked) {
return;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Listener/UpdateCastorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Process\Process;
use Symfony\Contracts\Cache\CacheInterface;
Expand All @@ -23,6 +24,8 @@ class UpdateCastorListener
public function __construct(
private readonly CacheItemPoolInterface&CacheInterface $cache,
private readonly HttpClientInterface $httpClient,
#[Autowire('%repacked%')]
private readonly bool $repacked,
private readonly LoggerInterface $logger = new NullLogger(),
) {
}
Expand All @@ -32,7 +35,7 @@ public function __construct(
#[AsEventListener()]
public function checkUpdate(ConsoleCommandEvent $event): void
{
if (class_exists(\RepackedApplication::class)) {
if ($this->repacked) {
return;
}
if (PlatformHelper::getEnv('DISABLE_VERSION_CHECK')) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Slow/RepackCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function hello(): void
'repositories' => [
[
'type' => 'path',
'url' => __DIR__ . '/..',
'url' => __DIR__ . '/../..',
],
],
'require' => [
Expand Down

0 comments on commit 0a58ca6

Please sign in to comment.