Assert the project language and type are valid

This commit is contained in:
Oliver Davies 2024-02-28 10:35:41 +00:00
parent d541f064a5
commit e499d42fa5
6 changed files with 54 additions and 0 deletions

View file

@ -32,6 +32,11 @@ final class BuildCommand extends AbstractCommand
workingDir: $workingDir,
))->getLanguage();
assert(
assertion: ProjectLanguage::isValid($language),
description: sprintf('%s is not a supported language.', $language),
);
// Attempt to prepopulate some of the options, such as the project type
// based on its dependencies.
// TODO: move this logic to a service so it can be tested.
@ -60,6 +65,11 @@ final class BuildCommand extends AbstractCommand
// with the option value if there is one.
$projectType = $input->getOption('type') ?? $projectType;
assert(
assertion: ProjectType::isValid($projectType),
description: sprintf('%s is not a supported project type.', $projectType),
);
$isDockerCompose = $this->filesystem->exists($workingDir . '/docker-compose.yaml');
switch ($language) {

View file

@ -32,6 +32,11 @@ final class InstallCommand extends AbstractCommand
))->getLanguage();
}
assert(
assertion: ProjectLanguage::isValid($language),
description: sprintf('%s is not a supported language.', $language),
);
// TODO: Composer in Docker Compose?
$process = Process::create(
args: explode(separator: ' ', string: strval($args)),

View file

@ -39,6 +39,11 @@ final class PackageInstallCommand extends AbstractCommand
workingDir: $workingDir,
))->getLanguage();
assert(
assertion: ProjectLanguage::isValid($language),
description: sprintf('%s is not a supported language.', $language),
);
switch ($language) {
case ProjectLanguage::PHP->value:
$process = Process::create(

View file

@ -32,6 +32,11 @@ final class RunCommand extends AbstractCommand
workingDir: $workingDir,
))->getLanguage();
assert(
assertion: ProjectLanguage::isValid($language),
description: sprintf('%s is not a supported language.', $language),
);
// Attempt to prepopulate some of the options, such as the project type
// based on its dependencies.
// TODO: move this logic to a service so it can be tested.
@ -60,6 +65,11 @@ final class RunCommand extends AbstractCommand
// with the option value if there is one.
$projectType = $input->getOption('type') ?? $projectType;
assert(
assertion: ProjectType::isValid($projectType),
description: sprintf('%s is not a supported project type.', $projectType),
);
$filesystem = new Filesystem();
$isDockerCompose = $filesystem->exists($workingDir . '/docker-compose.yaml');

View file

@ -8,4 +8,16 @@ enum ProjectLanguage: string
{
case JavaScript = 'javascript';
case PHP = 'php';
/**
* @param non-empty-string $language
*/
public static function isValid(string $language): bool
{
return in_array(
haystack: array_column(self::cases(), 'value'),
needle: $language,
strict: true,
);
}
}

View file

@ -13,4 +13,16 @@ enum ProjectType: string
case Drupal = 'drupal';
case Sculpin = 'sculpin';
case Symfony = 'symfony';
/**
* @param non-empty-string $projectType
*/
public static function isValid(string $projectType): bool
{
return in_array(
haystack: array_column(self::cases(), 'value'),
needle: $projectType,
strict: true,
);
}
}