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

[WIP] Revamp Plugin Manager #501

Merged
merged 17 commits into from
Jun 3, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improved type hinting and conditional logic
jaxwilko committed May 30, 2022
commit 1716ec502232ba1e9d517e05668e7ff36d274ac9
80 changes: 33 additions & 47 deletions modules/system/classes/PluginManager.php
Original file line number Diff line number Diff line change
@@ -11,8 +11,10 @@
use Config;
use Schema;
use SystemException;
use FilesystemIterator;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use System\Models\PluginVersion;
use Winter\Storm\Support\ClassLoader;
use Backend\Classes\NavigationManager;

@@ -382,11 +384,7 @@ public function bootAll($force = false)
*/
public function bootPlugin(PluginBase $plugin): void
{
if (
!$plugin
|| (self::$noInit && !$plugin->elevated)
|| $this->isDisabled($plugin)
) {
if ((self::$noInit && !$plugin->elevated) || $this->isDisabled($plugin)) {
return;
}

@@ -398,12 +396,7 @@ public function bootPlugin(PluginBase $plugin): void
*/
public function getPluginPath(string|PluginBase $plugin): ?string
{
$path = null;
$plugin = $this->findByIdentifier($plugin, true);
if ($plugin) {
$path = $plugin->getPluginPath();
}
return $path;
return $this->findByIdentifier($plugin, true)?->getPluginPath();
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
}

/**
@@ -412,7 +405,7 @@ public function getPluginPath(string|PluginBase $plugin): ?string
* @param string $id Plugin identifier, eg: Namespace.PluginName
* @return bool
*/
public function exists(string|PluginBase $plugin): bool
public function exists(PluginBase|string $plugin): bool
{
return $this->findByIdentifier($plugin) && !$this->isDisabled($plugin);
}
@@ -453,7 +446,7 @@ public function findByNamespace($namespace)
/**
* Returns a plugin registration class based on its identifier (Author.Plugin).
*/
public function findByIdentifier(string|PluginBase $identifier, bool $ignoreReplacements = false): ?PluginBase
public function findByIdentifier(PluginBase|string $identifier, bool $ignoreReplacements = false): ?PluginBase
{
if ($identifier instanceof PluginBase) {
return $identifier;
@@ -473,7 +466,7 @@ public function findByIdentifier(string|PluginBase $identifier, bool $ignoreRepl
/**
* Checks to see if a plugin has been registered.
*/
public function hasPlugin(string|PluginBase $plugin): bool
public function hasPlugin(PluginBase|string $plugin): bool
{
$normalized = $this->getNormalizedIdentifier($plugin);

@@ -513,13 +506,13 @@ public function getVendorAndPluginNames(): array
}

$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS)
new RecursiveDirectoryIterator($dirPath, FilesystemIterator::FOLLOW_SYMLINKS)
);
$it->setMaxDepth(2);
$it->rewind();

while ($it->valid()) {
if (($it->getDepth() > 1) && $it->isFile() && (strtolower($it->getFilename()) == "plugin.php")) {
if (($it->getDepth() > 1) && $it->isFile() && (strtolower($it->getFilename()) === "plugin.php")) {
$filePath = dirname($it->getPathname());
$pluginName = basename($filePath);
$vendorName = basename(dirname($filePath));
@@ -536,7 +529,7 @@ public function getVendorAndPluginNames(): array
* Resolves a plugin identifier (Author.Plugin) from a plugin class name
* (Author\Plugin) or PluginBase instance.
*/
public function getIdentifier(string|PluginBase $plugin): string
public function getIdentifier(PluginBase|string $plugin): string
{
$namespace = Str::normalizeClassName($plugin);
if (strpos($namespace, '\\') === null) {
@@ -555,20 +548,17 @@ public function getIdentifier(string|PluginBase $plugin): string
* (Author\Plugin\Classes\Example), identifier (Author.Plugin), or
* PluginBase instance.
*/
public function getNamespace(string|PluginBase $plugin): string
public function getNamespace(PluginBase|string $plugin): string
{
if (
is_object($plugin)
|| (is_string($plugin) && strpos($plugin, '.') === null)
) {
return Str::normalizeClassName($plugin);
}
if (is_string($plugin) && strpos($plugin, '.') !== null) {
$parts = explode('.', $plugin);
$slice = array_slice($parts, 0, 2);
$namespace = implode('\\', $slice);

$parts = explode('.', $plugin);
$slice = array_slice($parts, 0, 2);
$namespace = implode('\\', $slice);
return Str::normalizeClassName($namespace);
}

return Str::normalizeClassName($namespace);
return Str::normalizeClassName($plugin);
}

/**
@@ -579,18 +569,14 @@ public function getNamespace(string|PluginBase $plugin): string
public function normalizeIdentifier(string $code): string
{
$code = strtolower($code);
if (isset($this->normalizedMap[$code])) {
return $this->normalizedMap[$code];
}

return $code;
return $this->normalizedMap[$code] ?? $code;
}

/**
* Returns the normalized identifier (i.e. Winter.Blog) from the provided
* string or PluginBase instance.
*/
public function getNormalizedIdentifier(string|PluginBase $plugin): string
public function getNormalizedIdentifier(PluginBase|string $plugin): string
{
return $this->normalizeIdentifier($this->getIdentifier($plugin));
}
@@ -626,7 +612,7 @@ public function getRegistrationMethodValues(string $methodName): array
/**
* Sets the provided flag on the provided plugin
*/
protected function flagPlugin(string|PluginBase $plugin, string $flag): void
protected function flagPlugin(PluginBase|string $plugin, string $flag): void
{
$code = $this->getNormalizedIdentifier($plugin);
$this->pluginFlags[$code][$flag] = true;
@@ -635,7 +621,7 @@ protected function flagPlugin(string|PluginBase $plugin, string $flag): void
/**
* Removes the provided flag from the provided plugin
*/
protected function unflagPlugin(string|PluginBase $plugin, string $flag): void
protected function unflagPlugin(PluginBase|string $plugin, string $flag): void
{
$code = $this->getNormalizedIdentifier($plugin);
unset($this->pluginFlags[$code][$flag]);
@@ -669,7 +655,7 @@ protected function loadDisabled(): void
* Determines if a plugin is disabled by looking at the meta information
* or the application configuration.
*/
public function isDisabled(string|PluginBase $plugin): bool
public function isDisabled(PluginBase|string $plugin): bool
{
$code = $this->getNormalizedIdentifier($plugin);

@@ -688,12 +674,11 @@ public function getReplacementMap(): array
/**
* Returns the actively replaced plugins defined in $this->activeReplacementMap
*/
public function getActiveReplacementMap(string|PluginBase|null $plugin = null): array|string|null
public function getActiveReplacementMap(PluginBase|string $plugin = null): array|string|null
{
if (!$plugin) {
return $this->activeReplacementMap;
}
return $this->activeReplacementMap[$this->getNormalizedIdentifier($plugin)] ?? null;
return $plugin
? $this->activeReplacementMap[$this->getNormalizedIdentifier($plugin)] ?? null
: $this->activeReplacementMap;
}

/**
@@ -760,16 +745,17 @@ protected function aliasPluginAs(string $namespace, string $alias): void
*
* @throws InvalidArgumentException if unable to find the requested plugin record in the database
*/
protected function getPluginRecord(string|PluginBase $plugin): PluginVersion
protected function getPluginRecord(PluginBase|string $plugin): PluginVersion
{
$plugin = $this->getNormalizedIdentifier($plugin);
if (isset($this->pluginRecords[$plugin])) {
return $this->pluginRecords[$plugin];
}

$record = PluginVersion::where('code', $plugin)->first();

if (!$record) {
throw new InvalidArgumentException("$plugin was not found in the database.");
throw new \InvalidArgumentException("$plugin was not found in the database.");
}

return $this->pluginRecords[$plugin] = $record;
@@ -778,7 +764,7 @@ protected function getPluginRecord(string|PluginBase $plugin): PluginVersion
/**
* Flags the provided plugin as "frozen" (updates cannot be downloaded / installed)
*/
public function freezePlugin(string|PluginBase $plugin): void
public function freezePlugin(PluginBase|string $plugin): void
{
$record = $this->getPluginRecord($plugin);
$record->is_frozen = true;
@@ -788,7 +774,7 @@ public function freezePlugin(string|PluginBase $plugin): void
/**
* "Unfreezes" the provided plugin, allowing for updates to be performed
*/
public function unfreezePlugin(string|PluginBase $plugin): void
public function unfreezePlugin(PluginBase|string $plugin): void
{
$record = $this->getPluginRecord($plugin);
$record->is_frozen = false;
@@ -798,7 +784,7 @@ public function unfreezePlugin(string|PluginBase $plugin): void
/**
* Disables the provided plugin using the provided flag (defaults to static::DISABLED_BY_USER)
*/
public function disablePlugin(string|PluginBase $plugin, $flag = self::DISABLED_BY_USER): bool
public function disablePlugin(PluginBase|string $plugin, string|bool $flag = self::DISABLED_BY_USER): bool
{
// $flag used to be (bool) $byUser
if ($flag === true) {