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

Added functionality for Apps to provide resources #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 34 additions & 19 deletions src/Pwa/Bundle/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
use Shopware\Core\Framework\App\AppEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
Expand All @@ -28,7 +29,7 @@ class AssetService implements EventSubscriberInterface
/**
* @var string
*/
private $resourcesDirectory = '/Resources/app/pwa';
private $resourcesDirectory = '/src/Resources/app/pwa';

/**
* @var Kernel
Expand All @@ -41,6 +42,11 @@ class AssetService implements EventSubscriberInterface

private $pluginRepository;

/**
* @var EntityRepositoryInterface
*/
private $appRepository;

/**
* @var FormattingHelper
*/
Expand All @@ -51,10 +57,16 @@ class AssetService implements EventSubscriberInterface
*/
private $fileSystem;

public function __construct(Kernel $kernel, EntityRepositoryInterface $pluginRepository, FormattingHelper $helper, FilesystemInterface $fileSystem)
public function __construct(
Kernel $kernel,
EntityRepositoryInterface $pluginRepository,
EntityRepositoryInterface $appRepository,
FormattingHelper $helper,
FilesystemInterface $fileSystem)
{
$this->kernel = $kernel;
$this->pluginRepository = $pluginRepository;
$this->appRepository = $appRepository;
$this->helper = $helper;
$this->fileSystem = $fileSystem;
}
Expand All @@ -73,7 +85,7 @@ public function dumpBundles(): string
$archivePath = $this->kernel->getCacheDir() . '/../../' . $this->assetArtifactDirectory . '.zip';

// Look for assets
list($bundles, $checksum) = $this->getBundles();
list($bundles, $checksum) = $this->getExtensions();

// Zip directory
$this->createAssetsArchive($archivePath, $bundles);
Expand Down Expand Up @@ -119,35 +131,38 @@ private function createAssetsArchive(string $archivePath, array $bundles)
$zip->close();
}

private function getBundles()
private function getExtensions()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract into a seperate service

{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('active', true));

/** @var PluginCollection $plugins */
$plugins = $this->pluginRepository->search($criteria, Context::createDefaultContext());
$pluginNames = $plugins->map(function (PluginEntity $plugin) {
return $plugin->getName();
});
$apps = $this->appRepository->search($criteria, Context::createDefaultContext());

/** @var BundleInterface[] $kernelBundles */
$kernelBundles = $this->kernel->getBundles();
$kernelProjectDir = $this->kernel->getProjectDir();

foreach ($kernelBundles as $kernelBundle)
{
if(!in_array($kernelBundle->getName(), $pluginNames)) {
continue;
}
$extensionMetaData = [];

/** @var $apps iterable<AppEntity> */
foreach($apps as $app) {
$extensionMetaData[] = [
'name' => $app->getName(),
'path' => implode([$kernelProjectDir, $app->getPath()], DIRECTORY_SEPARATOR)
];
}

$bundles[] = [
'name' => $kernelBundle->getName(),
'path' => $kernelBundle->getPath()
/** @var $apps iterable<PluginEntity> */
foreach($plugins as $plugin) {
$extensionMetaData[] = [
'name' => $plugin->getName(),
'path' => implode([$kernelProjectDir, $plugin->getPath()], DIRECTORY_SEPARATOR)
];
}

$checksum = md5(\GuzzleHttp\json_encode($bundles));
$checksum = md5(\GuzzleHttp\json_encode($extensionMetaData));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native JsonEncode & throw on error


return [$bundles, $checksum];
return [$extensionMetaData, $checksum];
}

private function writeToPublicDirectory(string $sourceArchive, string $checksum = null): string
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/bundle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<service id="SwagShopwarePwa\Pwa\Bundle\AssetService">
<argument type="service" id="kernel" />
<argument id="plugin.repository" type="service"/>
<argument id="app.repository" type="service"/>
<argument type="service" id="SwagShopwarePwa\Pwa\Bundle\Helper\FormattingHelper"/>
<argument type="service" id="shopware.filesystem.public"/>
<tag name="kernel.event_subscriber"/>
Expand Down