Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 28 additions & 44 deletions system/Commands/Worker/WorkerInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,23 @@

namespace CodeIgniter\Commands\Worker;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;

/**
* Install Worker Mode for FrankenPHP.
*
* This command sets up the necessary files to run CodeIgniter 4
* in FrankenPHP worker mode for improved performance.
* Installs the files needed to run CodeIgniter 4 in FrankenPHP worker mode.
*/
class WorkerInstall extends BaseCommand
#[Command(
name: 'worker:install',
description: 'Install FrankenPHP worker mode by creating necessary configuration files',
group: 'Worker Mode',
)]
class WorkerInstall extends AbstractCommand
{
protected $group = 'Worker Mode';
protected $name = 'worker:install';
protected $description = 'Install FrankenPHP worker mode by creating necessary configuration files';
protected $usage = 'worker:install [options]';
protected $options = [
'--force' => 'Overwrite existing files',
];

/**
* Template file mappings (template => destination path)
* Template file mappings (template => destination path).
*
* @var array<string, string>
*/
Expand All @@ -42,9 +38,18 @@ class WorkerInstall extends BaseCommand
'Caddyfile.tpl' => 'Caddyfile',
];

public function run(array $params)
protected function configure(): void
{
$this->addOption(new Option(
name: 'force',
shortcut: 'f',
description: 'Overwrite existing files.',
));
}

protected function execute(array $arguments, array $options): int
{
$force = array_key_exists('force', $params) || CLI::getOption('force');
$force = $options['force'] === true;

CLI::write('Setting up FrankenPHP Worker Mode', 'yellow');
CLI::newLine();
Expand All @@ -53,63 +58,46 @@ public function run(array $params)

$created = [];

// Process each template
foreach ($this->templates as $template => $destination) {
$source = SYSTEMPATH . 'Commands/Worker/Views/' . $template;
$target = ROOTPATH . $destination;

$isFile = is_file($target);

// Skip if file exists and not forcing overwrite
if (! $force && $isFile) {
continue;
}

// Read template content
$content = file_get_contents($source);

if ($content === false) {
CLI::error(
"Failed to read template: {$template}",
'light_gray',
'red',
);
CLI::newLine();
CLI::error(sprintf('Failed to read template: %s', $template), 'light_gray', 'red');

return EXIT_ERROR;
}

// Write file to destination
if (! write_file($target, $content)) {
CLI::error(
'Failed to create file: ' . clean_path($target),
'light_gray',
'red',
);
CLI::newLine();
CLI::error(sprintf('Failed to create file: %s', clean_path($target)), 'light_gray', 'red');

return EXIT_ERROR;
}

if ($force && $isFile) {
CLI::write(' File overwritten: ' . clean_path($target), 'yellow');
CLI::write(sprintf(' File overwritten: %s', clean_path($target)), 'yellow');
} else {
CLI::write(' File created: ' . clean_path($target), 'green');
CLI::write(sprintf(' File created: %s', clean_path($target)), 'green');
}

$created[] = $destination;
}

// No files were created
if ($created === []) {
CLI::newLine();
CLI::write('Worker mode files already exist.', 'yellow');
CLI::write('Use --force to overwrite existing files.', 'yellow');
CLI::newLine();

return EXIT_ERROR;
}

// Success message
CLI::newLine();
CLI::write('Worker mode files created successfully!', 'green');
CLI::newLine();
Expand All @@ -119,10 +107,7 @@ public function run(array $params)
return EXIT_SUCCESS;
}

/**
* Display next steps to the user
*/
protected function showNextSteps(): void
private function showNextSteps(): void
{
CLI::write('Next Steps:', 'yellow');
CLI::newLine();
Expand All @@ -133,6 +118,5 @@ protected function showNextSteps(): void

CLI::write('2. Test your application:', 'white');
CLI::write(' curl http://localhost:8080/', 'green');
CLI::newLine();
}
}
118 changes: 69 additions & 49 deletions system/Commands/Worker/WorkerUninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,23 @@

namespace CodeIgniter\Commands\Worker;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;

/**
* Uninstall Worker Mode for FrankenPHP.
*
* This command removes the files created by the worker:install command.
* Removes the files created by the worker:install command.
*/
class WorkerUninstall extends BaseCommand
#[Command(
name: 'worker:uninstall',
description: 'Remove FrankenPHP worker mode configuration files',
group: 'Worker Mode',
)]
class WorkerUninstall extends AbstractCommand
{
protected $group = 'Worker Mode';
protected $name = 'worker:uninstall';
protected $description = 'Remove FrankenPHP worker mode configuration files';
protected $usage = 'worker:uninstall [options]';
protected $options = [
'--force' => 'Skip confirmation prompt',
];

/**
* Files to remove (must match Install command)
* Files to remove (must match the worker:install command).
*
* @var list<string>
*/
Expand All @@ -41,80 +38,103 @@ class WorkerUninstall extends BaseCommand
'Caddyfile',
];

public function run(array $params)
protected function configure(): void
{
$force = array_key_exists('force', $params) || CLI::getOption('force');
$this->addOption(new Option(
name: 'force',
shortcut: 'f',
description: 'Skip the confirmation prompt.',
));
}

CLI::write('Uninstalling FrankenPHP Worker Mode', 'yellow');
CLI::newLine();
protected function interact(array &$arguments, array &$options): void
{
if ($this->hasUnboundOption('force', $options)) {
return;
}

// Find existing files
$existing = [];
if ($this->existingFiles() === []) {
return;
}

foreach ($this->files as $file) {
$path = ROOTPATH . $file;
if (is_file($path)) {
$existing[] = $file;
}
if (CLI::prompt('Remove the FrankenPHP worker mode files?', ['y', 'n']) === 'y') {
$options['force'] = null; // simulate the presence of the --force option
}
}

protected function execute(array $arguments, array $options): int
{
$existing = $this->existingFiles();

// No files to remove
if ($existing === []) {
CLI::write('No worker mode files found to remove.', 'yellow');
CLI::newLine();

return EXIT_SUCCESS;
}

// Show files that will be removed
CLI::write('The following files will be removed:', 'yellow');
if ($options['force'] === false) {
if ($this->isInteractive()) {
CLI::write('Uninstall cancelled.', 'yellow');

foreach ($existing as $file) {
CLI::write(' - ' . $file, 'white');
}
CLI::newLine();
return EXIT_SUCCESS;
}

// Confirm deletion unless --force is used
if (! $force) {
$confirm = CLI::prompt('Are you sure you want to remove these files?', ['y', 'n']);
CLI::newLine();
CLI::error('Uninstall aborted: pass --force to remove worker mode files in non-interactive mode.', 'light_gray', 'red');

if ($confirm !== 'y') {
CLI::write('Uninstall cancelled.', 'yellow');
CLI::newLine();
return EXIT_ERROR;
}

return EXIT_ERROR;
}
CLI::newLine();
CLI::write('The following files will be removed:', 'yellow');

foreach ($existing as $file) {
CLI::write(sprintf(' - %s', $file), 'white');
}

CLI::newLine();

$removed = [];

// Remove each file
foreach ($existing as $file) {
$path = ROOTPATH . $file;

if (! @unlink($path)) {
CLI::error('Failed to remove file: ' . clean_path($path), 'light_gray', 'red');
CLI::error(sprintf('Failed to remove file: %s', clean_path($path)), 'light_gray', 'red');

continue;
}

CLI::write(' File removed: ' . clean_path($path), 'green');
CLI::write(sprintf(' File removed: %s', clean_path($path)), 'green');

$removed[] = $file;
}

// Summary
CLI::newLine();

if ($removed === []) {
CLI::error('No files were removed.');
CLI::newLine();
CLI::error('No files were removed.', 'light_gray', 'red');

return EXIT_ERROR;
}

CLI::write('Worker mode files removed successfully!', 'green');
CLI::newLine();

return EXIT_SUCCESS;
}

/**
* @return list<string>
*/
private function existingFiles(): array
{
$existing = [];

foreach ($this->files as $file) {
if (is_file(ROOTPATH . $file)) {
$existing[] = $file;
}
}

return $existing;
}
}
Loading
Loading