Skip to content

Commit

Permalink
Merge pull request #20091 from totten/master-ext-instclass
Browse files Browse the repository at this point in the history
Extensions - Setup `<classloader>` during installation
  • Loading branch information
eileenmcnaughton authored Apr 19, 2021
2 parents 04be790 + d1f58c6 commit 6c9b26d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
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

0 comments on commit 6c9b26d

Please sign in to comment.