Skip to content

Commit

Permalink
Added method for unpacking a zip and returning all sources inside
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Jan 4, 2025
1 parent 43acf5c commit 5de0809
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions modules/system/classes/extensions/source/LocalSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace System\Classes\Extensions\Source;

use Cms\Classes\ThemeManager;
use Illuminate\Support\Facades\File;
use System\Classes\Extensions\PluginManager;
use System\Traits\InteractsWithZip;
use Winter\Storm\Exception\ApplicationException;

class LocalSource extends ExtensionSource
Expand All @@ -17,4 +21,43 @@ public function __construct(
) {
parent::__construct(static::SOURCE_LOCAL, $type, $code, $composerPackage, $path);
}

/**
* @throws ApplicationException
*/
public static function fromZip(string $path): array
{
if (!File::exists($path)) {
throw new ApplicationException('Zip not found');
}

$dir = temp_path(time());

if (!File::exists($dir)) {
File::makeDirectory($dir);
}

(new class {
use InteractsWithZip;
})->extractArchive($path, $dir);

$plugins = PluginManager::instance()->findPluginsInPath($dir);
$themes = ThemeManager::instance()->findThemesInPath($dir);

if (!count($plugins) && !count($themes)) {
throw new ApplicationException('Could not detect any plugins or themes in zip');
}

$sources = [];

foreach ($plugins as $code => $path) {
$sources = new static(static::TYPE_PLUGIN, code: $code, path: $path);
}

foreach ($themes as $code => $path) {
$sources = new static(static::TYPE_THEME, code: $code, path: $path);
}

return $sources;
}
}

0 comments on commit 5de0809

Please sign in to comment.