Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve interoperability of Bootstrap.php/CmsBootstrap.php #154

Merged
merged 4 commits into from
Mar 10, 2023
Merged
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
1 change: 1 addition & 0 deletions bin/cv
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $autoloaders = array(
foreach ($autoloaders as $autoloader) {
if (file_exists($autoloader)) {
require_once $autoloader;
define('CV_AUTOLOAD', $autoloader);
$found = 1;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/BuildkitReader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Civi\Cv;

use Civi\Cv\Util\Filesystem;

class BuildkitReader {

/**
Expand All @@ -10,6 +12,8 @@ class BuildkitReader {
* @return null|string
*/
public static function findShFile($settingsFile) {
$fs = new Filesystem();
$settingsFile = $fs->toAbsolutePath($settingsFile);
$parts = explode('/', str_replace('\\', '/', $settingsFile));
while (!empty($parts)) {
$last = array_pop($parts);
Expand Down
35 changes: 35 additions & 0 deletions src/CmsBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ public function __construct($options = array()) {
$this->addOptions($options);
}

/**
* Export bootstrap logic.
*
* @param array $actions
* List of bootstrap actions to include.
* Ex: ['bootCms', 'bootCivi']
* @return string
* PHP code to `CmsBootstrap` in a new process
*/
public function generate(array $actions = []): string {
$instanceExpr = '\\' . get_class($this) . '::singleton()';
$code = '';
$code .= sprintf("require_once %s;\n", var_export(CV_AUTOLOAD, TRUE));
$code .= sprintf("%s->addOptions(%s);\n", $instanceExpr, var_export($this->getOptions(), TRUE));
foreach ($actions as $action) {
$code .= sprintf("%s->%s()->bootCivi();\n", $instanceExpr, $action);
}
return $code;
}

/**
* Bootstrap the CiviCRM runtime.
*
Expand Down Expand Up @@ -218,9 +238,24 @@ public function bootCivi() {
\CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
}

$GLOBALS['_CV'] = $this->buildCv();

return $this;
}

protected function buildCv(): array {
$settings = constant('CIVICRM_SETTINGS_PATH');
if ($settings && class_exists('Civi\Cv\SiteConfigReader')) {
$this->writeln("Load supplemental configuration for \"$settings\"", OutputInterface::VERBOSITY_DEBUG);
$reader = new SiteConfigReader($settings);
return $reader->compile(array('buildkit', 'home'));
}
else {
$this->writeln("Warning: Not loading supplemental configuration for \"$settings\". SiteConfigReader is missing.", OutputInterface::VERBOSITY_DEBUG);
return [];
}
}

/**
*/
protected function loginStandaloneUser() {
Expand Down
4 changes: 4 additions & 0 deletions src/Command/BootCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ protected function execute(InputInterface $input, OutputInterface $output) {
. '\CRM_Utils_System::loadBootStrap(array(), FALSE);';
break;

case 'cms-full':
$code = \Civi\Cv\CmsBootstrap::singleton()->generate(['bootCms', 'bootCivi']);
break;

case 'none':
break;

Expand Down
19 changes: 17 additions & 2 deletions src/Util/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,34 @@ public static function exists(?string $path): bool {
return $path !== NULL && file_exists($path);
}

/**
* @return false|string
*/
public function pwd() {
// exec(pwd) works better with symlinked source trees, but it's
// probably not portable to Windows.
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return getcwd();
}
else {
exec('pwd', $output);
return trim(implode("\n", $output));
}
}

/**
* @param string $path
* @return string updated $path
*/
public function toAbsolutePath($path) {
if (empty($path)) {
$res = getcwd();
$res = $this->pwd();
}
elseif ($this->isAbsolutePath($path)) {
$res = $path;
}
else {
$res = getcwd() . DIRECTORY_SEPARATOR . $path;
$res = $this->pwd() . DIRECTORY_SEPARATOR . $path;
}
if (is_dir($res)) {
return realpath($res);
Expand Down
22 changes: 19 additions & 3 deletions tests/Command/BootCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ public function setUp(): void {
parent::setUp();
}

public function testBootDefault() {
$phpBoot = Process::runOk($this->cv("php:boot"));
public function testBootFull() {
$phpBoot = Process::runOk($this->cv("php:boot --level=full"));
$this->assertRegExp(';CIVICRM_SETTINGS_PATH;', $phpBoot->getOutput());

$helloPhp = escapeshellarg($phpBoot->getOutput()
. 'printf("count is %s\n", CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_contact"));'
. 'printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);'
);
$phpRun = Process::runOk(new \Symfony\Component\Process\Process("php -r $helloPhp"));
$this->assertRegExp('/^count is [0-9]+$/', $phpRun->getOutput());
$this->assertRegExp('/^count is [0-9]+\n/', $phpRun->getOutput());
$this->assertRegExp('/my admin is \w+\n/', $phpRun->getOutput());
}

public function testBootCmsFull() {
$phpBoot = Process::runOk($this->cv("php:boot --level=cms-full"));
$this->assertRegExp(';BEGINPHP;', $phpBoot->getOutput());
$this->assertRegExp(';ENDPHP;', $phpBoot->getOutput());

$helloPhp = escapeshellarg($phpBoot->getOutput()
. 'printf("count is %s\n", CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_contact"));'
. 'printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);'
);
$phpRun = Process::runOk(new \Symfony\Component\Process\Process("php -r $helloPhp"));
$this->assertRegExp('/^count is [0-9]+\n/', $phpRun->getOutput());
$this->assertRegExp('/my admin is \w+\n/', $phpRun->getOutput());
}

public function testBootClassLoader() {
Expand Down
12 changes: 12 additions & 0 deletions tests/Command/EvalCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public function testPhpEval_ExitCodeError() {
$this->assertEquals(255, $p->getExitCode());
}

public function testPhpEval_CvVar_Full() {
$helloPhp = escapeshellarg('printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);');
$p = Process::runOk($this->cv("ev --level=full $helloPhp"));
$this->assertRegExp('/^my admin is \w+\s*$/', $p->getOutput());
}

public function testPhpEval_CvVar_CmsFull() {
$helloPhp = escapeshellarg('printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);');
$p = Process::runOk($this->cv("ev --level=cms-full $helloPhp"));
$this->assertRegExp('/^my admin is \w+\s*$/', $p->getOutput());
}

public function testBoot() {
$checkBoot = escapeshellarg('echo (function_exists("drupal_add_js") || function_exists("wp_redirect") || class_exists("JFactory") || class_exists("Drupal")) ? "found" : "not-found";');

Expand Down