Skip to content

Commit 7931b97

Browse files
authoredMar 10, 2023
Merge pull request #154 from totten/master-cms-cvvar
Improve interoperability of `Bootstrap.php`/`CmsBootstrap.php`
2 parents 830f89c + 13163db commit 7931b97

File tree

7 files changed

+92
-5
lines changed

7 files changed

+92
-5
lines changed
 

Diff for: ‎bin/cv

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ $autoloaders = array(
1818
foreach ($autoloaders as $autoloader) {
1919
if (file_exists($autoloader)) {
2020
require_once $autoloader;
21+
define('CV_AUTOLOAD', $autoloader);
2122
$found = 1;
2223
break;
2324
}

Diff for: ‎src/BuildkitReader.php

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Civi\Cv;
33

4+
use Civi\Cv\Util\Filesystem;
5+
46
class BuildkitReader {
57

68
/**
@@ -10,6 +12,8 @@ class BuildkitReader {
1012
* @return null|string
1113
*/
1214
public static function findShFile($settingsFile) {
15+
$fs = new Filesystem();
16+
$settingsFile = $fs->toAbsolutePath($settingsFile);
1317
$parts = explode('/', str_replace('\\', '/', $settingsFile));
1418
while (!empty($parts)) {
1519
$last = array_pop($parts);

Diff for: ‎src/CmsBootstrap.php

+35
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ public function __construct($options = array()) {
103103
$this->addOptions($options);
104104
}
105105

106+
/**
107+
* Export bootstrap logic.
108+
*
109+
* @param array $actions
110+
* List of bootstrap actions to include.
111+
* Ex: ['bootCms', 'bootCivi']
112+
* @return string
113+
* PHP code to `CmsBootstrap` in a new process
114+
*/
115+
public function generate(array $actions = []): string {
116+
$instanceExpr = '\\' . get_class($this) . '::singleton()';
117+
$code = '';
118+
$code .= sprintf("require_once %s;\n", var_export(CV_AUTOLOAD, TRUE));
119+
$code .= sprintf("%s->addOptions(%s);\n", $instanceExpr, var_export($this->getOptions(), TRUE));
120+
foreach ($actions as $action) {
121+
$code .= sprintf("%s->%s()->bootCivi();\n", $instanceExpr, $action);
122+
}
123+
return $code;
124+
}
125+
106126
/**
107127
* Bootstrap the CiviCRM runtime.
108128
*
@@ -218,9 +238,24 @@ public function bootCivi() {
218238
\CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
219239
}
220240

241+
$GLOBALS['_CV'] = $this->buildCv();
242+
221243
return $this;
222244
}
223245

246+
protected function buildCv(): array {
247+
$settings = constant('CIVICRM_SETTINGS_PATH');
248+
if ($settings && class_exists('Civi\Cv\SiteConfigReader')) {
249+
$this->writeln("Load supplemental configuration for \"$settings\"", OutputInterface::VERBOSITY_DEBUG);
250+
$reader = new SiteConfigReader($settings);
251+
return $reader->compile(array('buildkit', 'home'));
252+
}
253+
else {
254+
$this->writeln("Warning: Not loading supplemental configuration for \"$settings\". SiteConfigReader is missing.", OutputInterface::VERBOSITY_DEBUG);
255+
return [];
256+
}
257+
}
258+
224259
/**
225260
*/
226261
protected function loginStandaloneUser() {

Diff for: ‎src/Command/BootCommand.php

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ protected function execute(InputInterface $input, OutputInterface $output) {
3636
. '\CRM_Utils_System::loadBootStrap(array(), FALSE);';
3737
break;
3838

39+
case 'cms-full':
40+
$code = \Civi\Cv\CmsBootstrap::singleton()->generate(['bootCms', 'bootCivi']);
41+
break;
42+
3943
case 'none':
4044
break;
4145

Diff for: ‎src/Util/Filesystem.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,34 @@ public static function exists(?string $path): bool {
99
return $path !== NULL && file_exists($path);
1010
}
1111

12+
/**
13+
* @return false|string
14+
*/
15+
public function pwd() {
16+
// exec(pwd) works better with symlinked source trees, but it's
17+
// probably not portable to Windows.
18+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
19+
return getcwd();
20+
}
21+
else {
22+
exec('pwd', $output);
23+
return trim(implode("\n", $output));
24+
}
25+
}
26+
1227
/**
1328
* @param string $path
1429
* @return string updated $path
1530
*/
1631
public function toAbsolutePath($path) {
1732
if (empty($path)) {
18-
$res = getcwd();
33+
$res = $this->pwd();
1934
}
2035
elseif ($this->isAbsolutePath($path)) {
2136
$res = $path;
2237
}
2338
else {
24-
$res = getcwd() . DIRECTORY_SEPARATOR . $path;
39+
$res = $this->pwd() . DIRECTORY_SEPARATOR . $path;
2540
}
2641
if (is_dir($res)) {
2742
return realpath($res);

Diff for: ‎tests/Command/BootCommandTest.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,31 @@ public function setUp(): void {
1313
parent::setUp();
1414
}
1515

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

2020
$helloPhp = escapeshellarg($phpBoot->getOutput()
2121
. 'printf("count is %s\n", CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_contact"));'
22+
. 'printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);'
2223
);
2324
$phpRun = Process::runOk(new \Symfony\Component\Process\Process("php -r $helloPhp"));
24-
$this->assertRegExp('/^count is [0-9]+$/', $phpRun->getOutput());
25+
$this->assertRegExp('/^count is [0-9]+\n/', $phpRun->getOutput());
26+
$this->assertRegExp('/my admin is \w+\n/', $phpRun->getOutput());
27+
}
28+
29+
public function testBootCmsFull() {
30+
$phpBoot = Process::runOk($this->cv("php:boot --level=cms-full"));
31+
$this->assertRegExp(';BEGINPHP;', $phpBoot->getOutput());
32+
$this->assertRegExp(';ENDPHP;', $phpBoot->getOutput());
33+
34+
$helloPhp = escapeshellarg($phpBoot->getOutput()
35+
. 'printf("count is %s\n", CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_contact"));'
36+
. 'printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);'
37+
);
38+
$phpRun = Process::runOk(new \Symfony\Component\Process\Process("php -r $helloPhp"));
39+
$this->assertRegExp('/^count is [0-9]+\n/', $phpRun->getOutput());
40+
$this->assertRegExp('/my admin is \w+\n/', $phpRun->getOutput());
2541
}
2642

2743
public function testBootClassLoader() {

Diff for: ‎tests/Command/EvalCommandTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public function testPhpEval_ExitCodeError() {
4040
$this->assertEquals(255, $p->getExitCode());
4141
}
4242

43+
public function testPhpEval_CvVar_Full() {
44+
$helloPhp = escapeshellarg('printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);');
45+
$p = Process::runOk($this->cv("ev --level=full $helloPhp"));
46+
$this->assertRegExp('/^my admin is \w+\s*$/', $p->getOutput());
47+
}
48+
49+
public function testPhpEval_CvVar_CmsFull() {
50+
$helloPhp = escapeshellarg('printf("my admin is %s\n", $GLOBALS["_CV"]["ADMIN_USER"]);');
51+
$p = Process::runOk($this->cv("ev --level=cms-full $helloPhp"));
52+
$this->assertRegExp('/^my admin is \w+\s*$/', $p->getOutput());
53+
}
54+
4355
public function testBoot() {
4456
$checkBoot = escapeshellarg('echo (function_exists("drupal_add_js") || function_exists("wp_redirect") || class_exists("JFactory") || class_exists("Drupal")) ? "found" : "not-found";');
4557

0 commit comments

Comments
 (0)