Skip to content

Commit

Permalink
Allow disabling only local or global plugins internally to fix #10935
Browse files Browse the repository at this point in the history
…without side-effects
  • Loading branch information
Seldaek committed Jul 13, 2022
1 parent a481dfc commit 55fe12b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
10 changes: 1 addition & 9 deletions src/Composer/Command/CreateProjectCommand.php
Expand Up @@ -403,15 +403,7 @@ protected function installRootPackage(IOInterface $io, Config $config, $packageN
throw new \InvalidArgumentException('Invalid stability provided ('.$stability.'), must be one of: '.implode(', ', array_keys(BasePackage::$stabilities)));
}

$composerJson = array_merge(
// prevent version guessing from happening
array('version' => '1.0.0'),
$config->all(),
// ensure the vendor dir and its plugins does not get loaded if CWD/vendor has plugins in it
array('config' => array('vendor-dir' => Platform::getDevNull()))
);
$factory = new Factory;
$composer = $factory->createComposer($io, $composerJson, $disablePlugins, Platform::getDevNull(), true, $disableScripts);
$composer = Factory::create($io, $config->all(), $disablePlugins === true ? true : 'local', $disableScripts);
$config = $composer->getConfig();
$rm = $composer->getRepositoryManager();

Expand Down
9 changes: 6 additions & 3 deletions src/Composer/Factory.php
Expand Up @@ -293,7 +293,7 @@ public static function createOutput()
* @param IOInterface $io IO instance
* @param array<string, mixed>|string|null $localConfig either a configuration array or a filename to read from, if null it will
* read from the default filename
* @param bool $disablePlugins Whether plugins should not be loaded
* @param bool|'local'|'global' $disablePlugins Whether plugins should not be loaded, can be set to local or global to only disable local/global plugins
* @param bool $disableScripts Whether scripts should not be run
* @param string|null $cwd
* @param bool $fullLoad Whether to initialize everything or only main project stuff (used when loading the global composer)
Expand Down Expand Up @@ -500,6 +500,9 @@ protected function addLocalRepository(IOInterface $io, RepositoryManager $rm, $v
*/
protected function createGlobalComposer(IOInterface $io, Config $config, $disablePlugins, $disableScripts, $fullLoad = false)
{
// make sure if disable plugins was 'local' it is now turned off
$disablePlugins = $disablePlugins === 'global' || $disablePlugins === true;

$composer = null;
try {
$composer = $this->createComposer($io, $config->get('home') . '/composer.json', $disablePlugins, $config->get('home'), $fullLoad, $disableScripts);
Expand Down Expand Up @@ -579,7 +582,7 @@ public function createArchiveManager(Config $config, Downloader\DownloadManager
* @param IOInterface $io
* @param Composer $composer
* @param Composer $globalComposer
* @param bool $disablePlugins
* @param bool|'local'|'global' $disablePlugins Whether plugins should not be loaded, can be set to local or global to only disable local/global plugins
* @return Plugin\PluginManager
*/
protected function createPluginManager(IOInterface $io, Composer $composer, Composer $globalComposer = null, $disablePlugins = false)
Expand Down Expand Up @@ -635,7 +638,7 @@ protected function loadRootPackage(RepositoryManager $rm, Config $config, Versio
* @param IOInterface $io IO instance
* @param mixed $config either a configuration array or a filename to read from, if null it will read from
* the default filename
* @param bool $disablePlugins Whether plugins should not be loaded
* @param bool|'local'|'global' $disablePlugins Whether plugins should not be loaded, can be set to local or global to only disable local/global plugins
* @param bool $disableScripts Whether scripts should not be run
* @return Composer
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Installer/PluginInstaller.php
Expand Up @@ -53,7 +53,7 @@ public function supports($packageType)
public function prepare($type, PackageInterface $package, PackageInterface $prevPackage = null)
{
// fail install process early if it is going to fail due to a plugin not being allowed
if (($type === 'install' || $type === 'update') && !$this->composer->getPluginManager()->arePluginsDisabled()) {
if (($type === 'install' || $type === 'update') && !$this->composer->getPluginManager()->arePluginsDisabled('local')) {
$this->composer->getPluginManager()->isPluginAllowed($package->getName(), false);
}

Expand Down
53 changes: 26 additions & 27 deletions src/Composer/Plugin/PluginManager.php
Expand Up @@ -47,7 +47,7 @@ class PluginManager
protected $globalComposer;
/** @var VersionParser */
protected $versionParser;
/** @var bool */
/** @var bool|'local'|'global' */
protected $disablePlugins = false;

/** @var array<PluginInterface> */
Expand All @@ -74,7 +74,7 @@ class PluginManager
* @param IOInterface $io
* @param Composer $composer
* @param Composer $globalComposer
* @param bool $disablePlugins
* @param bool|'local'|'global' $disablePlugins Whether plugins should not be loaded, can be set to local or global to only disable local/global plugins
*/
public function __construct(IOInterface $io, Composer $composer, Composer $globalComposer = null, $disablePlugins = false)
{
Expand All @@ -94,15 +94,16 @@ public function __construct(IOInterface $io, Composer $composer, Composer $globa
*/
public function loadInstalledPlugins()
{
if ($this->disablePlugins) {
return;
if (!$this->arePluginsDisabled('local')) {
$repo = $this->composer->getRepositoryManager()->getLocalRepository();
$this->loadRepository($repo, false);
}

$repo = $this->composer->getRepositoryManager()->getLocalRepository();
$globalRepo = $this->globalComposer !== null ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
$this->loadRepository($repo, false);
if ($globalRepo) {
$this->loadRepository($globalRepo, true);
if (!$this->arePluginsDisabled('global')) {
$globalRepo = $this->globalComposer !== null ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
if ($globalRepo !== null) {
$this->loadRepository($globalRepo, true);
}
}
}

Expand All @@ -113,15 +114,16 @@ public function loadInstalledPlugins()
*/
public function deactivateInstalledPlugins()
{
if ($this->disablePlugins) {
return;
if (!$this->arePluginsDisabled('local')) {
$repo = $this->composer->getRepositoryManager()->getLocalRepository();
$this->deactivateRepository($repo, false);
}

$repo = $this->composer->getRepositoryManager()->getLocalRepository();
$globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
$this->deactivateRepository($repo, false);
if ($globalRepo) {
$this->deactivateRepository($globalRepo, true);
if (!$this->arePluginsDisabled('global')) {
$globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
if ($globalRepo !== null) {
$this->deactivateRepository($globalRepo, true);
}
}
}

Expand Down Expand Up @@ -161,7 +163,7 @@ public function getGlobalComposer()
*/
public function registerPackage(PackageInterface $package, $failOnMissingClasses = false, $isGlobalPlugin = false)
{
if ($this->disablePlugins) {
if ($this->arePluginsDisabled($isGlobalPlugin ? 'global' : 'local')) {
return;
}

Expand Down Expand Up @@ -320,10 +322,6 @@ public function registerPackage(PackageInterface $package, $failOnMissingClasses
*/
public function deactivatePackage(PackageInterface $package)
{
if ($this->disablePlugins) {
return;
}

if (!isset($this->registeredPlugins[$package->getName()])) {
return;
}
Expand Down Expand Up @@ -351,10 +349,6 @@ public function deactivatePackage(PackageInterface $package)
*/
public function uninstallPackage(PackageInterface $package)
{
if ($this->disablePlugins) {
return;
}

if (!isset($this->registeredPlugins[$package->getName()])) {
return;
}
Expand Down Expand Up @@ -394,6 +388,10 @@ protected function getPluginApiVersion()
*/
public function addPlugin(PluginInterface $plugin, $isGlobalPlugin = false, PackageInterface $sourcePackage = null)
{
if ($this->arePluginsDisabled($isGlobalPlugin ? 'global' : 'local')) {
return;
}

if ($sourcePackage === null) {
trigger_error('Calling PluginManager::addPlugin without $sourcePackage is deprecated, if you are using this please get in touch with us to explain the use case', E_USER_DEPRECATED);
} elseif (!$this->isPluginAllowed($sourcePackage->getName(), $isGlobalPlugin)) {
Expand Down Expand Up @@ -682,11 +680,12 @@ private function parseAllowedPlugins($allowPluginsConfig, Locker $locker = null)
/**
* @internal
*
* @param 'local'|'global' $type
* @return bool
*/
public function arePluginsDisabled()
public function arePluginsDisabled($type)
{
return $this->disablePlugins;
return $this->disablePlugins === true || $this->disablePlugins === $type;
}

/**
Expand Down

0 comments on commit 55fe12b

Please sign in to comment.