Skip to content

Issue #25: Add compatibility with statistics contrib module. #26

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

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
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
71 changes: 69 additions & 2 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\InstalledVersions;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
Expand Down Expand Up @@ -50,7 +51,6 @@ class Installer {
'index.php',
'core/install.php',
'core/rebuild.php',
'core/modules/statistics/statistics.php',
];

/**
Expand Down Expand Up @@ -108,6 +108,8 @@ public function __construct(Composer $composer, IOInterface $io) {

$this->setAssetFileTypes();

$this->getStatisticsPath();

$this->excludes = $this->getConfig('excludes', []);
}

Expand All @@ -125,7 +127,7 @@ public function __construct(Composer $composer, IOInterface $io) {
public function getConfig($name, $default = NULL) {
$extra = $this->composer->getPackage()->getExtra();

// TODO: Backward compatibility for old configs. Remove on stable version.
// @todo Backward compatibility for old configs. Remove on stable version.
$legacyConfigs = [
'drupal-app-dir',
'drupal-web-dir',
Expand Down Expand Up @@ -434,4 +436,69 @@ public function setAssetFileTypes() {
$this->assetFileTypes = $event->getAssetFileTypes();
}

/**
* Get the path to the Composer root directory.
*/
public function getComposerRoot(): string|false {
foreach (InstalledVersions::getAllRawData() as $data) {
if (isset($data['versions']['drupal/core'])) {
return realpath($data['root']['install_path']);
}
}
$root = InstalledVersions::getRootPackage();
return realpath($root['install_path']);
}

/**
* Return module path from appDir.
*
* @param string $module
* The PHP file from the app directory.
*
* @return string|null
* Module Path.
*/
protected function getModulePath(string $module): ?string {
$composerRoot = $this->getComposerRoot();
$extra = $this->composer->getPackage()->getExtra();
$fs = new SymfonyFilesystem();
$installerPaths = $extra['installer-paths'];
$searchType = 'type:drupal-module';
$modulePath = NULL;

foreach ($installerPaths as $key => $types) {
if (in_array($searchType, $types, TRUE)) {
$modulePath = (string) str_replace('{$name}', $module, $key);
break;
}
}
if (!$fs->exists($composerRoot . '/' . $modulePath) || !$modulePath) {
return NULL;
}

$moduleBasePath = str_replace($this->appDir . '/', '', $modulePath);
return $moduleBasePath;
}

/**
* Get path of Statistics module.
*/
protected function getStatisticsPath(): void {
$composerRoot = $this->getComposerRoot();
$fs = new SymfonyFilesystem();
if (!$fs->exists($composerRoot)) {
$this->io->writeError('> drupal-paranoia: Composer root path not found.');
return;
}
$statisticsPath = $this->getModulePath('statistics');
$statisticsCorePath = 'core/modules/statistics/statistics.php';

if ($fs->exists($composerRoot . '/' . $this->appDir . '/' . $statisticsCorePath)) {
array_push($this->frontControllers, $statisticsCorePath);
}
elseif ($statisticsPath) {
array_push($this->frontControllers, $statisticsPath . '/statistics.php');
}
}

}