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

Add plugin zip import command #558

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Core/Framework/DependencyInjection/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@
<tag name="console.command"/>
</service>

<service id="Shopware\Core\Framework\Plugin\Command\PluginZipImportCommand">
<argument type="service" id="Shopware\Core\Framework\Plugin\PluginManagementService"/>
<argument type="service" id="Shopware\Core\Framework\Plugin\PluginService"/>

<tag name="console.command"/>
</service>

<service id="Shopware\Core\Framework\Plugin\Command\Lifecycle\PluginInstallCommand">
<argument type="service" id="Shopware\Core\Framework\Plugin\PluginLifecycleService"/>
<argument type="service" id="plugin.repository"/>
Expand Down
79 changes: 79 additions & 0 deletions src/Core/Framework/Plugin/Command/PluginZipImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\Plugin\Command;

use Composer\IO\ConsoleIO;
use Shopware\Core\Framework\Adapter\Console\ShopwareStyle;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Exception\NoPluginFoundInZipException;
use Shopware\Core\Framework\Plugin\PluginManagementService;
use Shopware\Core\Framework\Plugin\PluginService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PluginZipImportCommand extends Command
{
protected static $defaultName = 'plugin:zip-import';

/**
* @var PluginManagementService
*/
private $pluginManagementService;

/**
* @var PluginService
*/
private $pluginService;

public function __construct(PluginManagementService $pluginManagementService, PluginService $pluginService)
{
parent::__construct();
$this->pluginManagementService = $pluginManagementService;
$this->pluginService = $pluginService;
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this->setDescription('Import plugin zip file.')
->addArgument('zip-file', InputArgument::REQUIRED, 'Zip file that contains a shopware platform plugin.')
->addOption('no-refresh', null, InputOption::VALUE_OPTIONAL, 'Do not refresh plugin list.')
->addOption('delete', null, InputOption::VALUE_OPTIONAL, 'Delete the zip file after importing successfully.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$zipFile = $input->getArgument('zip-file');
$io = new ShopwareStyle($input, $output);
$io->title('Shopware Plugin Zip Import');

try {
$this->pluginManagementService->extractPluginZip($zipFile, (bool) $input->getOption('delete'));
} catch (NoPluginFoundInZipException $e) {
$io->error($e->getMessage());

return 1;
}

$io->success('Successfully import zip file ' . basename($zipFile));

if (!$input->getOption('no-refresh')) {
$this->pluginService->refreshPlugins(
Context::createDefaultContext(),
new ConsoleIO($input, $output, new HelperSet())
);
$io->success('Plugin list refreshed');
}

return 0;
}
}
6 changes: 4 additions & 2 deletions src/Core/Framework/Plugin/PluginExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(string $pluginDir, Filesystem $filesystem)
/**
* Extracts the provided zip file to the plugin directory
*/
public function extract(\ZipArchive $archive): void
public function extract(\ZipArchive $archive, bool $delete = true): void
{
$destination = $this->pluginDir;

Expand All @@ -47,7 +47,9 @@ public function extract(\ZipArchive $archive): void
$this->filesystem->remove($backupFile);
}

unlink($archive->filename);
if ($delete) {
unlink($archive->filename);
}
} catch (\Exception $e) {
if ($backupFile !== '') {
$this->filesystem->rename($backupFile, $oldFile);
Expand Down
7 changes: 2 additions & 5 deletions src/Core/Framework/Plugin/PluginManagementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ public function __construct(
$this->cacheClearer = $cacheClearer;
}

/**
* @deprecated tag:v6.3.0 - Will be private
*/
public function extractPluginZip(string $file): void
public function extractPluginZip(string $file, bool $delete = true): void
{
$archive = ZipUtils::openZip($file);

if ($this->pluginZipDetector->isPlugin($archive)) {
$this->pluginExtractor->extract($archive);
$this->pluginExtractor->extract($archive, $delete);
} else {
throw new NoPluginFoundInZipException($file);
}
Expand Down
24 changes: 24 additions & 0 deletions src/Core/Framework/Test/Plugin/PluginManagementServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ public function testUploadPlugin(): void
static::assertFileExists(self::PLUGIN_FASHION_THEME_PATH . '/SwagFashionTheme.php');
}

public function testExtractPluginZip(): void
{
$this->getPluginManagementService()->extractPluginZip(__DIR__ . '/_fixture/SwagFashionTheme.zip', true);

$extractedPlugin = $this->filesystem->exists(__DIR__ . '/_fixture/plugins/SwagFashionTheme');
$extractedPluginBaseClass = $this->filesystem->exists(__DIR__ . '/_fixture/plugins/SwagFashionTheme/SwagFashionTheme.php');
$pluginZipExists = $this->filesystem->exists(__DIR__ . '/_fixture/SwagFashionTheme.zip');
static::assertTrue($extractedPlugin);
static::assertTrue($extractedPluginBaseClass);
static::assertFalse($pluginZipExists);
}

public function testExtractPluginZipWithoutDeletion(): void
{
$this->getPluginManagementService()->extractPluginZip(__DIR__ . '/_fixture/SwagFashionTheme.zip', false);

$extractedPlugin = $this->filesystem->exists(__DIR__ . '/_fixture/plugins/SwagFashionTheme');
$extractedPluginBaseClass = $this->filesystem->exists(__DIR__ . '/_fixture/plugins/SwagFashionTheme/SwagFashionTheme.php');
$pluginZipExists = $this->filesystem->exists(__DIR__ . '/_fixture/SwagFashionTheme.zip');
static::assertTrue($extractedPlugin);
static::assertTrue($extractedPluginBaseClass);
static::assertTrue($pluginZipExists);
}

private function createTestCacheDirectory(): string
{
$kernelClass = KernelLifecycleManager::getKernelClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Below you'll find a list of each command and its usage.
| plugin:update | plugins | clearCache | Updates one or multiple plugins |
| plugin:list | N/A | filter | Prints a list of all available plugins filtering with the given filter |
| plugin:refresh | N/A | skipPluginList | Refreshes the plugin list |
| plugin:zip-import | zip-file | no-refresh | Import plugin zip file |

*List of all plugin commands*

Expand Down Expand Up @@ -103,3 +104,33 @@ $ ./bin/console plugin:activate YourPluginWithNamespace ThirdPartyPluginWithName
$ ./bin/console plugin:deactivate YourPluginWithNamespace ThirdPartyPluginWithNamespace
```
*Plugin de-, activate command*

### Importing plugins

There are multiple way to import a plugin that is already existing on the filesystem.
You can either make a composer repository and use composer require:

```js
{
/* ... */
"repositories": [
{
"type": "path",
"url": "YourPluginDirectory",
"options": {
"symlink": true
}
}
]
}
```
```
$ composer require your-plugin-technical-name
$ ./bin/console plugin:refresh
```

Or you can provide a pre-packaged plugin file like they are distributed at the [community store](https://store.shopware.com).

```
$ ./bin/console plugin:zip-import YourPluginFile.zip
```