Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Implement D8 initialization for civicrm-setup #11

Merged
merged 3 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 1 addition & 44 deletions plugins/init/Drupal.civi-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// Compute settingsPath.
$drupalSystem = new CRM_Utils_System_Drupal();
$cmsPath = $drupalSystem->cmsRootPath();
$siteDir = _drupal_civisetup_getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
$siteDir = \Civi\Setup\FileUtil::getDrupalSiteDir($cmsPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving _drupal_civisetup_getSiteDir() to a shareable class seems like a good idea.

The functionality here is really specific to the Drupal file structure, so it'd be better to call the util \Civi\Setup\DrupalUtil::getSiteDir(...).

$model->settingsPath = implode(DIRECTORY_SEPARATOR,
[$cmsPath, 'sites', $siteDir, 'civicrm.settings.php']);

Expand Down Expand Up @@ -89,46 +89,3 @@ function _drupal_civisetup_getPrivateFiles() {

return $filePrivatePath;
}

/**
* @param $cmsPath
* @param $str
*
* @return string
*/
function _drupal_civisetup_getSiteDir($cmsPath, $str) {
static $siteDir = '';

if ($siteDir) {
return $siteDir;
}

$sites = CIVICRM_DIRECTORY_SEPARATOR . 'sites' . CIVICRM_DIRECTORY_SEPARATOR;
$modules = CIVICRM_DIRECTORY_SEPARATOR . 'modules' . CIVICRM_DIRECTORY_SEPARATOR;
preg_match("/" . preg_quote($sites, CIVICRM_DIRECTORY_SEPARATOR) .
"([\-a-zA-Z0-9_.]+)" .
preg_quote($modules, CIVICRM_DIRECTORY_SEPARATOR) . "/",
$_SERVER['SCRIPT_FILENAME'], $matches
);
$siteDir = isset($matches[1]) ? $matches[1] : 'default';

if (strtolower($siteDir) == 'all') {
// For this case - use drupal's way of finding out multi-site directory
$uri = explode(CIVICRM_DIRECTORY_SEPARATOR, $_SERVER['SCRIPT_FILENAME']);
$server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
for ($i = count($uri) - 1; $i > 0; $i--) {
for ($j = count($server); $j > 0; $j--) {
$dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
if (file_exists($cmsPath . CIVICRM_DIRECTORY_SEPARATOR .
'sites' . CIVICRM_DIRECTORY_SEPARATOR . $dir
)) {
$siteDir = $dir;
return $siteDir;
}
}
}
$siteDir = 'default';
}

return $siteDir;
}
83 changes: 83 additions & 0 deletions plugins/init/Drupal8.civi-setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @file
*
* Determine default settings for Drupal 8.
*/

if (!defined('CIVI_SETUP')) {
exit("Installation plugins must only be loaded by the installer.\n");
}

\Civi\Setup::dispatcher()
->addListener('civi.setup.checkAuthorized', function (\Civi\Setup\Event\CheckAuthorizedEvent $e) {
$model = $e->getModel();
if ($model->cms !== 'Drupal8' || !function_exists('user_access')) {
return;
}

\Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'checkAuthorized'));
$e->setAuthorized(user_access('administer modules'));
});

\Civi\Setup::dispatcher()
->addListener('civi.setup.init', function (\Civi\Setup\Event\InitEvent $e) {
$model = $e->getModel();
$cmsPath = \Drupal::root();
Copy link
Member

@totten totten Feb 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The \Drupal::root() call should be moved down a few lines. The civi.setup.init event will fire in all contexts -- if you tried to run \Drupal::root() on a D7/Backdrop/WordPress/Joomla site, it would cause a hard crash.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right thanks for noticing that. Done

if ($model->cms !== 'Drupal8') {
return;
}
\Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'init'));

// Compute settingsPath.
$siteDir = \Civi\Setup\FileUtil::getDrupalSiteDir($cmsPath);
$model->settingsPath = implode(DIRECTORY_SEPARATOR, [$cmsPath, 'sites', $siteDir, 'civicrm.settings.php']);

// Compute DSN.
global $databases;
$databases = \Drupal\Core\Database\Database::getConnectionInfo();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused -- in D8, is the $databases a global variable, or is it something we lookup via static function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops my bad, earlier I started to work on copied file of plugins/init/Drupal.civi-setup.php so missed to omit this line. Corrected now :)

$model->db = $model->cmsDb = array(
'server' => \Civi\Setup\DbUtil::encodeHostPort($databases['default']['host'], $databases['default']['port'] ?: NULL),
'username' => $databases['default']['username'],
'password' => $databases['default']['password'],
'database' => $databases['default']['database'],
);

// Compute cmsBaseUrl.
global $base_url, $base_path;
$model->cmsBaseUrl = $base_url . $base_path;

// Compute general paths
$model->paths['civicrm.files']['url'] = implode(DIRECTORY_SEPARATOR, [$base_url, \Drupal\Core\StreamWrapper\PublicStream::basePath(), 'civicrm']);
$model->paths['civicrm.files']['path'] = implode(DIRECTORY_SEPARATOR, [_drupal8_civisetup_getPublicFiles(), 'civicrm']);

// Compute templateCompileDir.
$model->templateCompilePath = implode(DIRECTORY_SEPARATOR, [_drupal8_civisetup_getPrivateFiles(), 'civicrm', 'templates_c']);

// Compute default locale.
global $language;
$model->lang = \Civi\Setup\LocaleUtil::pickClosest($language->langcode, $model->getField('lang', 'options'));
});

function _drupal8_civisetup_getPublicFiles() {
$filePublicPath = realpath(\Drupal\Core\StreamWrapper\PublicStream::basePath());

if (!CRM_Utils_File::isAbsolute($filePublicPath)) {
$filePublicPath = \Drupal::root() . DIRECTORY_SEPARATOR . $filePublicPath;
}

return $filePublicPath;
}

function _drupal8_civisetup_getPrivateFiles() {
$filePrivatePath = realpath(\Drupal\Core\StreamWrapper\PrivateStream::basePath());

if (!$filePrivatePath) {
$filePrivatePath = _drupal8_civisetup_getPublicFiles();
}
elseif ($filePrivatePath && !CRM_Utils_File::isAbsolute($filePrivatePath)) {
$filePrivatePath = \Drupal::root() . DIRECTORY_SEPARATOR . $filePrivatePath;
}

return $filePrivatePath;
}
42 changes: 42 additions & 0 deletions src/Setup/FileUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,46 @@ public static function isDeletable($path) {
return is_writable(dirname($path));
}

/**
* @param $cmsPath
*
* @return string
*/
public static function getDrupalSiteDir($cmsPath) {
static $siteDir = '';

if ($siteDir) {
return $siteDir;
}

$sites = CIVICRM_DIRECTORY_SEPARATOR . 'sites' . CIVICRM_DIRECTORY_SEPARATOR;
$modules = CIVICRM_DIRECTORY_SEPARATOR . 'modules' . CIVICRM_DIRECTORY_SEPARATOR;
preg_match("/" . preg_quote($sites, CIVICRM_DIRECTORY_SEPARATOR) .
"([\-a-zA-Z0-9_.]+)" .
preg_quote($modules, CIVICRM_DIRECTORY_SEPARATOR) . "/",
$_SERVER['SCRIPT_FILENAME'], $matches
);
$siteDir = isset($matches[1]) ? $matches[1] : 'default';

if (strtolower($siteDir) == 'all') {
// For this case - use drupal's way of finding out multi-site directory
$uri = explode(CIVICRM_DIRECTORY_SEPARATOR, $_SERVER['SCRIPT_FILENAME']);
$server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
for ($i = count($uri) - 1; $i > 0; $i--) {
for ($j = count($server); $j > 0; $j--) {
$dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
if (file_exists($cmsPath . CIVICRM_DIRECTORY_SEPARATOR .
'sites' . CIVICRM_DIRECTORY_SEPARATOR . $dir
)) {
$siteDir = $dir;
return $siteDir;
}
}
}
$siteDir = 'default';
}

return $siteDir;
}

}