Skip to content

Commit 7124a65

Browse files
committed
Issue #25: Add compatibility with the statistics contrib module.
1 parent 0626c77 commit 7124a65

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

src/Installer.php

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Composer\DependencyResolver\Operation\InstallOperation;
77
use Composer\DependencyResolver\Operation\UninstallOperation;
88
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use Composer\InstalledVersions;
910
use Composer\Installer\PackageEvent;
1011
use Composer\IO\IOInterface;
1112
use Composer\Package\PackageInterface;
@@ -50,7 +51,6 @@ class Installer {
5051
'index.php',
5152
'core/install.php',
5253
'core/rebuild.php',
53-
'core/modules/statistics/statistics.php',
5454
];
5555

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

109109
$this->setAssetFileTypes();
110110

111+
$this->getStatisticsPath();
112+
111113
$this->excludes = $this->getConfig('excludes', []);
112114
}
113115

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

128-
// TODO: Backward compatibility for old configs. Remove on stable version.
130+
// @todo Backward compatibility for old configs. Remove on stable version.
129131
$legacyConfigs = [
130132
'drupal-app-dir',
131133
'drupal-web-dir',
@@ -368,7 +370,7 @@ public function createAssetSymlinks() {
368370
}
369371

370372
/**
371-
* Create a PHP stub file at web directory.
373+
* Create a PHP stub file at we directory.
372374
*
373375
* @param string $path
374376
* The PHP file from the app directory.
@@ -434,4 +436,65 @@ public function setAssetFileTypes() {
434436
$this->assetFileTypes = $event->getAssetFileTypes();
435437
}
436438

439+
/**
440+
* Get the path to the Composer root directory.
441+
*/
442+
public function getComposerRoot(): string|false {
443+
foreach (InstalledVersions::getAllRawData() as $data) {
444+
if (isset($data['versions']['drupal/core'])) {
445+
return realpath($data['root']['install_path']);
446+
}
447+
}
448+
$root = InstalledVersions::getRootPackage();
449+
return realpath($root['install_path']);
450+
}
451+
452+
/**
453+
* Return module path from appDir.
454+
*
455+
* @param string $module
456+
* The PHP file from the app directory.
457+
*
458+
* @return string|null
459+
* Module Path.
460+
*/
461+
protected function getModulePath(string $module): ?string {
462+
$composerRoot = $this->getComposerRoot();
463+
$extra = $this->composer->getPackage()->getExtra();
464+
$fs = new SymfonyFilesystem();
465+
$installerPaths = $extra['installer-paths'];
466+
$searchType = 'type:drupal-module';
467+
$modulePath = NULL;
468+
469+
foreach ($installerPaths as $key => $types) {
470+
if (in_array($searchType, $types, TRUE)) {
471+
$modulePath = (string) str_replace('{$name}', $module, $key);
472+
break;
473+
}
474+
}
475+
if (!$fs->exists($composerRoot . '/' . $modulePath) || !$modulePath) {
476+
return NULL;
477+
}
478+
479+
$moduleBasePath = str_replace($this->appDir . '/', '', $modulePath);
480+
return $moduleBasePath;
481+
}
482+
483+
/**
484+
* Get path of Statistics module.
485+
*/
486+
protected function getStatisticsPath(): void {
487+
$composerRoot = $this->getComposerRoot();
488+
$fs = new SymfonyFilesystem();
489+
$statisticsPath = $this->getModulePath('statistics');
490+
$statisticsCorePath = 'core/modules/statistics/statistics.php';
491+
492+
if ($fs->exists($composerRoot . '/' . $this->appDir . '/' . $statisticsCorePath)) {
493+
array_push($this->frontControllers, $statisticsCorePath);
494+
}
495+
elseif ($statisticsPath) {
496+
array_push($this->frontControllers, $statisticsPath . '/statistics.php');
497+
}
498+
}
499+
437500
}

0 commit comments

Comments
 (0)