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

Extensions - Setup <classloader> during installation #20091

Merged
merged 1 commit into from
Apr 19, 2021
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
69 changes: 48 additions & 21 deletions CRM/Extension/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ public function __destruct() {
public function register() {
// In pre-installation environments, don't bother with caching.
if (!defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || \CRM_Utils_System::isInUpgradeMode()) {
return $this->buildClassLoader()->register();
$this->loader = $this->buildClassLoader();
return $this->loader->register();
}

$file = $this->getCacheFile();
if (file_exists($file)) {
$loader = require $file;
$this->loader = require $file;
}
else {
$loader = $this->buildClassLoader();
$ser = serialize($loader);
$this->loader = $this->buildClassLoader();
$ser = serialize($this->loader);
file_put_contents($file,
sprintf("<?php\nreturn unserialize(%s);", var_export($ser, 1))
);
}
return $loader->register();
return $this->loader->register();
}

/**
Expand All @@ -94,22 +95,7 @@ public function buildClassLoader() {
if ($status !== CRM_Extension_Manager::STATUS_INSTALLED) {
continue;
}
$path = $this->mapper->keyToBasePath($key);
$info = $this->mapper->keyToInfo($key);
if (!empty($info->classloader)) {
foreach ($info->classloader as $mapping) {
switch ($mapping['type']) {
case 'psr0':
$loader->add($mapping['prefix'], CRM_Utils_File::addTrailingSlash($path . '/' . $mapping['path']));
break;

case 'psr4':
$loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']);
break;
}
$result[] = $mapping;
}
}
self::loadExtension($loader, $this->mapper->keyToInfo($key), $this->mapper->keyToBasePath($key));
}

return $loader;
Expand All @@ -131,6 +117,47 @@ public function refresh() {
$this->register();
}

/**
* Add a newly installed extension to the active classloader.
*
* NOTE: This is intended for use by CRM/Extension subsystem during installation.
*
* @param \CRM_Extension_Info $info
* @param string $path
*/
public function installExtension(CRM_Extension_Info $info, string $path): void {
$file = $this->getCacheFile();
if (file_exists($file)) {
unlink($file);
}
if ($this->loader) {
self::loadExtension($this->loader, $info, $path);
}
}

/**
* Read the extension metadata configure a classloader.
*
* @param \Composer\Autoload\ClassLoader $loader
* @param \CRM_Extension_Info $info
* @param string $path
*/
private static function loadExtension(\Composer\Autoload\ClassLoader $loader, CRM_Extension_Info $info, string $path): void {
if (!empty($info->classloader)) {
foreach ($info->classloader as $mapping) {
switch ($mapping['type']) {
case 'psr0':
$loader->add($mapping['prefix'], CRM_Utils_File::addTrailingSlash($path . '/' . $mapping['path']));
break;

case 'psr4':
$loader->addPsr4($mapping['prefix'], $path . '/' . $mapping['path']);
break;
}
}
}
}

/**
* @return string
*/
Expand Down
1 change: 1 addition & 0 deletions CRM/Extension/Manager/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(CRM_Extension_Mapper $mapper) {
* @param CRM_Extension_Info $info
*/
public function onPreInstall(CRM_Extension_Info $info) {
CRM_Extension_System::singleton()->getClassLoader()->installExtension($info, dirname($this->mapper->keyToPath($info->key)));
$this->callHook($info, 'install');
$this->callHook($info, 'enable');
}
Expand Down