Skip to content

Commit

Permalink
Merge pull request #327 from totten/master-facade-2
Browse files Browse the repository at this point in the history
(REF) Convert "Upgrader" to "Generator"
  • Loading branch information
totten authored Dec 13, 2023
2 parents 3466265 + 0256e69 commit 15a7589
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 104 deletions.
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="CRM\CivixBundle\CivixTestListener">
<arguments/>
</listener>
</listeners>
</phpunit>
13 changes: 13 additions & 0 deletions src/CRM/CivixBundle/CivixTestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace CRM\CivixBundle;

use PHPUnit\Framework\Test;

class CivixTestListener implements \PHPUnit\Framework\TestListener {
use \PHPUnit\Framework\TestListenerDefaultImplementation;

public function startTest(Test $test): void {
\Civix::reset();
}

}
20 changes: 0 additions & 20 deletions src/CRM/CivixBundle/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use CRM\CivixBundle\Builder\Info;
use Civix;
use CRM\CivixBundle\Upgrader;
use CRM\CivixBundle\Utils\Path;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -27,25 +26,6 @@ public function run(InputInterface $input, OutputInterface $output) {
}
}

/**
* @var \CRM\CivixBundle\Upgrader
*/
private $upgrader;

/**
* @return \Symfony\Component\Console\Style\StyleInterface
*/
protected function getIO() {
return Civix::io();
}

protected function getUpgrader(): Upgrader {
if ($this->upgrader === NULL) {
$this->upgrader = new Upgrader(new Path(\CRM\CivixBundle\Application::findExtDir()));
}
return $this->upgrader;
}

protected function confirm(InputInterface $input, OutputInterface $output, $message, $default = TRUE) {
$message = '<info>' . $message . '</info>'; /* FIXME Let caller stylize */
if ($input->getOption('yes')) {
Expand Down
8 changes: 4 additions & 4 deletions src/CRM/CivixBundle/Command/AddManagedEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// Boot CiviCRM to use api4
Civix::boot(['output' => $output]);

$upgrader = $this->getUpgrader();
$upgrader->addMixins(['[email protected]']);
$gen = \Civix::generator();
$gen->addMixins(['[email protected]']);
if ($entityName === 'Afform') {
$upgrader->exportAfform($entityId);
$gen->exportAfform($entityId);
}
else {
$upgrader->exportMgd($entityName, $entityId);
$gen->exportMgd($entityName, $entityId);
}

return 0;
Expand Down
14 changes: 7 additions & 7 deletions src/CRM/CivixBundle/Command/AddServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output) {
$up = $this->getUpgrader();
$up->addMixins(['[email protected]']);
$gen = \Civix::generator();
$gen->addMixins(['[email protected]']);

$servicePrefix = $up->infoXml->getFile();
$namespace = Naming::coerceNamespace($up->infoXml->getNamespace(), $input->getOption('naming'));
$servicePrefix = $gen->infoXml->getFile();
$namespace = Naming::coerceNamespace($gen->infoXml->getNamespace(), $input->getOption('naming'));

if ($input->isInteractive()) {
$defaultName = $input->getArgument('name') ?? Naming::createServiceName($servicePrefix, 'myService');
$this->getIO()->note([
Civix::io()->note([
'The service name is a short machine name. It may appear in contexts like:',
sprintf('Civi::service("%s")->doSomething()', $defaultName),
sprintf('It is recommended to always have a naming prefix (such as "%s").', $servicePrefix),
]);
$serviceName = $this->getIO()->ask('Service name', $defaultName, function ($answer) {
$serviceName = Civix::io()->ask('Service name', $defaultName, function ($answer) {
if ('' === trim($answer)) {
throw new \Exception('Service name cannot be empty');
}
Expand All @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$baseNameParts = array_map('ucfirst', explode('.', $baseName));
$className = Naming::createClassName($namespace, ...$baseNameParts);

$up->addClass($className, 'service.php.php', [
$gen->addClass($className, 'service.php.php', [
'service' => $serviceName,
]);
}
Expand Down
19 changes: 9 additions & 10 deletions src/CRM/CivixBundle/Command/UpgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use CRM\CivixBundle\Builder\Module;
use Civix;
use CRM\CivixBundle\Upgrader;
use CRM\CivixBundle\Generator;
use CRM\CivixBundle\Utils\Files;
use CRM\CivixBundle\Utils\Naming;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -39,8 +39,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$startVer = $input->getOption('start');
if ($startVer !== 'current') {
$verAliases = ['0' => '13.10.0'];
$upgrader = new Upgrader(new Path(\CRM\CivixBundle\Application::findExtDir()));
$upgrader->updateFormatVersion($verAliases[$startVer] ?? $startVer);
Civix::generator()->updateFormatVersion($verAliases[$startVer] ?? $startVer);
}

$this->executeIncrementalUpgrades();
Expand Down Expand Up @@ -74,10 +73,10 @@ protected function executeIncrementalUpgrades() {
$io->section("Upgrade <info>v{$lastVersion}</info> => <info>v{$upgradeVersion}</info>");
$io->writeln("<info>Executing upgrade script</info> $upgradeFile");

$upgrader = new Upgrader(new Path(\CRM\CivixBundle\Application::findExtDir()));
$gen = Civix::generator();
$func = require $upgradeFile;
$func($upgrader);
$upgrader->updateFormatVersion($upgradeVersion);
$func($gen);
$gen->updateFormatVersion($upgradeVersion);
$lastVersion = $upgradeVersion;
}
}
Expand All @@ -86,10 +85,10 @@ protected function executeGenericUpgrade(): void {
$io = \Civix::io();
$io->title('General upgrade');

$upgrader = new Upgrader(new Path(\CRM\CivixBundle\Application::findExtDir()));
$upgrader->cleanEmptyHooks();
$upgrader->cleanEmptyLines();
$upgrader->reconcileMixins();
$gen = Civix::generator();
$gen->cleanEmptyHooks();
$gen->cleanEmptyLines();
$gen->reconcileMixins();

/**
* @var \CRM\CivixBundle\Builder\Info $info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* The "Upgrader" class is a utility provided to various upgrade-scripts.
* The "Generator" class is a utility provided to various upgrade-scripts.
*/
class Upgrader {
class Generator {

/**
* @var \Symfony\Component\Console\Input\InputInterface
Expand Down
26 changes: 26 additions & 0 deletions src/Civix.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ public static function mixinBackports(): array {
return self::$cache[__FUNCTION__];
}

/**
* Get the generator-object for manipulating the extension.
*
* @param string|Path|null $extDir
* Base path of the extension that we wish to manipulate.
* If null, use the default (per CWD).
*
* @return \CRM\CivixBundle\Generator
*/
public static function generator($extDir = NULL): \CRM\CivixBundle\Generator {
$extDir = ($extDir === NULL) ? Civix::extdir() : Path::for($extDir);
$cacheKey = (string) $extDir;
if (!isset(self::$cache[__FUNCTION__][$cacheKey])) {
self::$cache[__FUNCTION__][$cacheKey] = new \CRM\CivixBundle\Generator($extDir);
}
return self::$cache[__FUNCTION__][$cacheKey];
}

/**
* @return \CRM\CivixBundle\UpgradeList
*/
Expand All @@ -204,4 +222,12 @@ public static function upgradeList(): \CRM\CivixBundle\UpgradeList {
return self::$cache[__FUNCTION__];
}

public static function reset(): void {
$new = [];
if (isset(static::$cache['boot'])) {
$new['boot'] = static::$cache['boot'];
}
static::$cache = $new;
}

}
6 changes: 2 additions & 4 deletions tests/e2e/CRMNamingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace E2E;

use CRM\CivixBundle\Builder\Info;
use CRM\CivixBundle\Upgrader;
use CRM\CivixBundle\Utils\Path;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\NullOutput;

Expand All @@ -15,7 +13,7 @@ class CRMNamingTest extends \PHPUnit\Framework\TestCase {
public static $key = 'civix_crmnaming';

/**
* @var \CRM\CivixBundle\Upgrader
* @var \CRM\CivixBundle\Generator
*/
protected $upgrader;

Expand All @@ -32,7 +30,7 @@ public function setUp(): void {
]);

\Civix::ioStack()->push(new ArgvInput(), new NullOutput());
$this->upgrader = new Upgrader(new Path(static::getExtPath()));
$this->upgrader = \Civix::generator(static::getExtPath());
$this->upgrader->updateInfo(function(Info $info) {
// FIXME: Allow "_" instead of "/"
$info->get()->civix->namespace = 'CRM/NamingTest';
Expand Down
6 changes: 2 additions & 4 deletions tests/e2e/CiviNamingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace E2E;

use CRM\CivixBundle\Builder\Info;
use CRM\CivixBundle\Upgrader;
use CRM\CivixBundle\Utils\Path;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\NullOutput;

Expand All @@ -15,7 +13,7 @@ class CiviNamingTest extends \PHPUnit\Framework\TestCase {
public static $key = 'civix_civinaming';

/**
* @var \CRM\CivixBundle\Upgrader
* @var \CRM\CivixBundle\Generator
*/
protected $upgrader;

Expand All @@ -32,7 +30,7 @@ public function setUp(): void {
]);

\Civix::ioStack()->push(new ArgvInput(), new NullOutput());
$this->upgrader = new Upgrader(new Path(static::getExtPath()));
$this->upgrader = \Civix::generator(static::getExtPath());
$this->upgrader->updateInfo(function(Info $info) {
// FIXME: Allow "\" instead of "/"
$info->get()->civix->namespace = 'Civi/NamingTest';
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/CivixProjectTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace E2E;

use CRM\CivixBundle\Application;
use CRM\CivixBundle\Upgrader;
use CRM\CivixBundle\Generator;
use CRM\CivixBundle\Utils\Files;
use CRM\CivixBundle\Utils\Path;
use ProcessHelper\ProcessHelper as PH;
Expand Down Expand Up @@ -195,14 +195,14 @@ public function civixUpgrade(array $options = []): CommandTester {
/**
* Get the upgrade-utility/helper.
*
* @return \CRM\CivixBundle\Upgrader
* @return \CRM\CivixBundle\Generator
*/
public function civixUpgradeHelper(): Upgrader {
public function civixGeneratorHelper(): Generator {
$input = new ArrayInput([]);
$output = new StreamOutput(fopen('php://memory', 'w', FALSE));
\Civix::ioStack()->push($input, $output);
try {
return new Upgrader(static::getExtPath());
return \Civix::generator(static::getExtPath());
}
finally {
\Civix::ioStack()->pop();
Expand Down
12 changes: 6 additions & 6 deletions upgrades/16.10.0.up.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
*
* At some point in the future, this step could be removed if we configure `info.xml`'s `<upgrader>` option.
*/
return function (\CRM\CivixBundle\Upgrader $upgrader) {
return function (\CRM\CivixBundle\Generator $gen) {
$io = \Civix::io();

if (!empty($upgrader->infoXml->get()->upgrader)) {
if (!empty($gen->infoXml->get()->upgrader)) {
$io->note("Found <upgrader> tag. Skip hook_postInstall.");
return;
}

// Give a notice if the new `CRM/*/Upgrader/Base` has a substantive change.
// Note: The change is actually done in the generic regen. This is just a notice.
$phpBaseClass = \CRM\CivixBundle\Utils\Naming::createClassName($upgrader->infoXml->getNamespace(), 'Upgrader', 'Base');
$phpBaseFile = \CRM\CivixBundle\Utils\Naming::createClassFile($upgrader->infoXml->getNamespace(), 'Upgrader', 'Base');
$phpBaseClass = \CRM\CivixBundle\Utils\Naming::createClassName($gen->infoXml->getNamespace(), 'Upgrader', 'Base');
$phpBaseFile = \CRM\CivixBundle\Utils\Naming::createClassFile($gen->infoXml->getNamespace(), 'Upgrader', 'Base');
if (file_exists($phpBaseFile)) {
$content = file_get_contents($phpBaseFile);
if (preg_match('|CRM_Core_BAO_Setting::setItem\(.revision, *.Extension.|', $content)) {
Expand All @@ -31,11 +31,11 @@
}
}

$upgrader->addHookDelegation('civicrm_postInstall', '',
$gen->addHookDelegation('civicrm_postInstall', '',
"This hook is important for supporting the new version of $phpBaseClass.");
}
else {
$upgrader->addHookDelegation('civicrm_postInstall', '',
$gen->addHookDelegation('civicrm_postInstall', '',
'If you use civix to facilitate database upgrades ("civix generate:upgrader"), then you should enable this stub. Otherwise, it is not needed.');
}

Expand Down
8 changes: 4 additions & 4 deletions upgrades/19.06.2.up.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* To be consistent and forward-compatible, you should consider updating your
* existing unit-tests to use the name base-classes.
*/
return function (\CRM\CivixBundle\Upgrader $upgrader) {
return function (\CRM\CivixBundle\Generator $gen) {
/* @var \Symfony\Component\Console\Style\SymfonyStyle $io */
$io = \Civix::io();

$testFiles = \CRM\CivixBundle\Utils\Files::findFiles($upgrader->baseDir->string('tests'), '*.php');
$upgrader->updateTextFiles($testFiles, function(string $file, string $content) use ($io, $upgrader) {
$testFiles = \CRM\CivixBundle\Utils\Files::findFiles($gen->baseDir->string('tests'), '*.php');
$gen->updateTextFiles($testFiles, function(string $file, string $content) use ($io, $gen) {
$old = 'PHPUnit_Framework_TestCase';
$new = 'PHPUnit\Framework\TestCase';
$relFile = \CRM\CivixBundle\Utils\Files::relativize($file, $upgrader->baseDir->string() . '/');
$relFile = \CRM\CivixBundle\Utils\Files::relativize($file, $gen->baseDir->string() . '/');

if (strpos($content, $old) === FALSE) {
return $content;
Expand Down
10 changes: 5 additions & 5 deletions upgrades/20.06.0.up.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
* If you have a generated `phpunit.xml` or `phpunit.xml.dist` file, it may include the old option `syntaxCheck="false"`.
* You can remove this. The option has been inert and will raise errors in newer versions of PHPUnit.
*/
return function (\CRM\CivixBundle\Upgrader $upgrader) {
return function (\CRM\CivixBundle\Generator $gen) {
/* @var \Symfony\Component\Console\Style\SymfonyStyle $io */
$io = \Civix::io();

$files = array_filter([
$upgrader->baseDir->string('phpunit.xml'),
$upgrader->baseDir->string('phpunit.xml.dist'),
$gen->baseDir->string('phpunit.xml'),
$gen->baseDir->string('phpunit.xml.dist'),
], 'file_exists');
$upgrader->updateTextFiles($files, function(string $file, string $oldContent) use ($io, $upgrader) {
$relFile = \CRM\CivixBundle\Utils\Files::relativize($file, $upgrader->baseDir->string() . '/');
$gen->updateTextFiles($files, function(string $file, string $oldContent) use ($io, $gen) {
$relFile = \CRM\CivixBundle\Utils\Files::relativize($file, $gen->baseDir->string() . '/');

$content = $oldContent;
$content = preg_replace(';(\s+)syntaxCheck="[^\"]+">;', '>', $content);
Expand Down
Loading

0 comments on commit 15a7589

Please sign in to comment.