Skip to content

Commit

Permalink
AddEntityCommand - Stop generating APIv3
Browse files Browse the repository at this point in the history
It's been 5 years since this command was switched to v4 as the default.
Api3 is officially deprecated in core. It's time.
  • Loading branch information
colemanw committed Sep 26, 2024
1 parent b8c94fa commit 60f782c
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions src/CRM/CivixBundle/Command/AddEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Exception;

class AddEntityCommand extends AbstractCommand {
const API_VERSION = 3;
const API_VERSION = 4;

protected function configure() {
parent::configure();
Expand All @@ -27,7 +27,6 @@ protected function configure() {
->setDescription('Add a new API/BAO/GenCode entity to a CiviCRM Module-Extension (*EXPERIMENTAL*)')
->addArgument('<EntityName>', InputArgument::REQUIRED, 'The brief, unique name of the entity")')
->addOption('table-name', NULL, InputOption::VALUE_OPTIONAL, 'The SQL table name. (see usage)')
->addOption('api-version', 'A', InputOption::VALUE_REQUIRED, 'Comma-separated list of versions (3,4)', '4')
->setHelp('Add a new API/BAO/GenCode entity to a CiviCRM Module-Extension.
This command is experimental. Developer discretion is advised.
Expand All @@ -50,10 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {

$this->assertCurrentFormat();

$apiVersions = explode(',', $input->getOption('api-version'));
if (!empty(array_diff($apiVersions, ['3', '4']))) {
throw new Exception("In --api-versions, found unrecognized versions. Expected: '3' and/or '4'");
}
$apiVersions = [self::API_VERSION];

Civix::generator()->addUpgrader('if-forced');

Expand All @@ -71,27 +67,18 @@ protected function execute(InputInterface $input, OutputInterface $output) {

$ctx['entityNameCamel'] = ucfirst($input->getArgument('<EntityName>'));
$ctx['tableName'] = $input->getOption('table-name') ? $input->getOption('table-name') : Naming::createTableName($input->getArgument('<EntityName>'));
if (function_exists('_civicrm_api_get_entity_name_from_camel')) {
$ctx['apiFunctionPrefix'] = 'civicrm_api' . self::API_VERSION . '_' . _civicrm_api_get_entity_name_from_camel($ctx['entityNameCamel']) . '_';
}
else {
throw new Exception("Failed to determine proper API function name. Perhaps the API internals have changed?");
}

$mixins = new Mixins($info, $basedir->string('mixin'), ['[email protected]']);
$mixins->save($ctx, $output);
$info->save($ctx, $output);

$ctx['apiFile'] = $basedir->string('api', 'v3', $ctx['entityNameCamel'] . '.php');
$ctx['api4File'] = $basedir->string('Civi', 'Api4', $ctx['entityNameCamel'] . '.php');
$ctx['daoClassName'] = strtr($ctx['namespace'], '/', '_') . '_DAO_' . $input->getArgument('<EntityName>');
$ctx['daoClassFile'] = $basedir->string(strtr($ctx['daoClassName'], '_', '/') . '.php');
$ctx['baoClassName'] = strtr($ctx['namespace'], '/', '_') . '_BAO_' . $input->getArgument('<EntityName>');
$ctx['baoClassFile'] = $basedir->string(strtr($ctx['baoClassName'], '_', '/') . '.php');
$ctx['entityTypeFile'] = $basedir->string('schema', $input->getArgument('<EntityName>') . '.entityType.php');
$ctx['extensionName'] = $info->getExtensionName();
$ctx['testApi3ClassName'] = 'api_v3_' . $ctx['entityNameCamel'] . 'Test';
$ctx['testApi3ClassFile'] = $basedir->string('tests', 'phpunit', strtr($ctx['testApi3ClassName'], '_', '/') . '.php');

$ext = new Collection();
$ext->builders['dirs'] = new Dirs([
Expand All @@ -100,13 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$ext->builders['dirs']->save($ctx, $output);

$hasPhpUnit = FALSE;
if (in_array('3', $apiVersions)) {
$ext->builders['dirs']->addPath(dirname($ctx['apiFile']));
$ext->builders['api.php'] = new Template('entity-api.php.php', $ctx['apiFile'], FALSE, Civix::templating());
$ext->builders['dirs']->addPath(dirname($ctx['testApi3ClassFile']));
$ext->builders['test.php'] = new Template('entity-api3-test.php.php', $ctx['testApi3ClassFile'], FALSE, Civix::templating());
$hasPhpUnit = TRUE;
}

if (in_array('4', $apiVersions)) {
$ext->builders['dirs']->addPath(dirname($ctx['api4File']));
$ext->builders['api4.php'] = new Template('entity-api4.php.php', $ctx['api4File'], FALSE, Civix::templating());
Expand All @@ -133,14 +114,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {

Civix::generator()->updateModuleCivixPhp();

if (count($apiVersions) >= 2) {
$output->writeln('<comment>Generated API skeletons for APIv3 and APIv4.</comment>');
}
elseif ($apiVersions == ['3']) {
$output->writeln('<comment>Generated API skeletons for APIv3. To generate APIv4, specify <info>--api-version=4</info></comment>');
}
elseif ($apiVersions == ['4']) {
$output->writeln('<comment>Generated API skeletons for APIv4. To generate APIv3, specify <info>--api-version=3</info></comment>');
if ($apiVersions == [4]) {
$output->writeln('<comment>Generated API skeletons for APIv4.</comment>');
}

$output->writeln('<comment>Note: no changes have been made to the database. You can update the database by uninstalling and re-enabling the extension.</comment>');
Expand Down

0 comments on commit 60f782c

Please sign in to comment.