Skip to content

Commit

Permalink
add error notice in layout, added missing english translations, fixes…
Browse files Browse the repository at this point in the history
… some glitches
  • Loading branch information
DDEV User committed Mar 29, 2023
1 parent f56fbeb commit 22d54e2
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## [1.20.0] - 2023-03-29
- Changed: show message in layout when entrypoints not generated
- Changed: added some english translations
- Fixed: throw correct exception in EntryCollection
- Fixed: warning when package.json has no dependencies in prepare command
- Fixed: add entry button in layout has not function if entrypoints not generated

## [1.19.0] - 2023-03-23
- Changed: reduced utils bundle usage ([#28])
- Deprecated: class `ImportsTemplateChoice`
Expand Down
8 changes: 6 additions & 2 deletions src/Collection/EntryCollection.php
Expand Up @@ -33,6 +33,7 @@ public function __construct(ConfigurationCollection $configurationCollection, ar

/**
* Return all encore entries (from webpack config and registered via bundle).
* @throws NoEntrypointsException
*/
public function getEntries(): array
{
Expand Down Expand Up @@ -85,6 +86,9 @@ private function mergeEntries(array $entrypointJsonFiles, array $bundleConfigEnt
return $bundleConfigEntries;
}

/**
* @throws NoEntrypointsException
*/
private function parseEntrypoints(string $entrypointsJson): array
{
$cached = null;
Expand All @@ -100,13 +104,13 @@ private function parseEntrypoints(string $entrypointsJson): array
}

if (!file_exists($entrypointsJson)) {
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints.json: the file "%s" does not exist. Maybe you forgot to run encore command?', $entrypointsJson));
throw new NoEntrypointsException(sprintf('Could not find the entrypoints.json: the file "%s" does not exist. Maybe you forgot to run encore command?', $entrypointsJson));
}

$entriesData = json_decode(file_get_contents($entrypointsJson), true);

if (null === $entriesData) {
throw new \InvalidArgumentException(sprintf('Could not decode the "%s" file', $entrypointsJson));
throw new NoEntrypointsException(sprintf('Could not decode the "%s" file', $entrypointsJson));
}

if (!isset($entriesData['entrypoints'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/PrepareCommand.php
Expand Up @@ -184,7 +184,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$packageData['dependencies'] = array_merge(
['@hundh/encore-entry-dependencies' => 'file:.'.DIRECTORY_SEPARATOR.$encoreAssetsPath.DIRECTORY_SEPARATOR],
$packageData['dependencies']
$packageData['dependencies'] ?? []
);

(new Filesystem())->dumpFile(
Expand Down
45 changes: 33 additions & 12 deletions src/DataContainer/LayoutContainer.php
Expand Up @@ -14,24 +14,31 @@
use Contao\DataContainer;
use Contao\LayoutModel;
use Contao\Message;
use HeimrichHannot\EncoreBundle\Collection\EntryCollection;
use HeimrichHannot\EncoreBundle\Exception\NoEntrypointsException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;

class LayoutContainer
{
protected array $bundleConfig;
protected ContaoFramework $contaoFramework;
private RequestStack $requestStack;
private ScopeMatcher $scopeMatcher;
protected array $bundleConfig;
protected ContaoFramework $contaoFramework;
private RequestStack $requestStack;
private ScopeMatcher $scopeMatcher;
private EntryCollection $entryCollection;
private TranslatorInterface $translator;

/**
* LayoutContainer constructor.
*/
public function __construct(array $bundleConfig, ContaoFramework $contaoFramework, RequestStack $requestStack, ScopeMatcher $scopeMatcher)
public function __construct(array $bundleConfig, ContaoFramework $contaoFramework, RequestStack $requestStack, ScopeMatcher $scopeMatcher, EntryCollection $entryCollection, TranslatorInterface $translator)
{
$this->bundleConfig = $bundleConfig;
$this->contaoFramework = $contaoFramework;
$this->requestStack = $requestStack;
$this->scopeMatcher = $scopeMatcher;
$this->entryCollection = $entryCollection;
$this->translator = $translator;
}

/**
Expand All @@ -41,17 +48,31 @@ public function onLoadCallback(DataContainer $dc = null): void
{
$request = $this->requestStack->getCurrentRequest();

if (!$request
|| !$dc
|| !$this->scopeMatcher->isBackendRequest($request)
|| !isset($this->bundleConfig['use_contao_template_variables'])
|| true !== $this->bundleConfig['use_contao_template_variables']
|| !($layout = $this->contaoFramework->getAdapter(LayoutModel::class)->findByPk($dc->id))) {
if (!$request || !$dc || !$this->scopeMatcher->isBackendRequest($request) || !($layout = $this->contaoFramework->getAdapter(LayoutModel::class)->findByPk($dc->id))) {
return;
}

$messageAdapter = $this->contaoFramework->getAdapter(Message::class);
if ($layout->addEncore) {
if ($messageAdapter->hasMessages('huh.encore.error.noEntryPoints')) {
$messageAdapter->addError($messageAdapter->generateUnwrapped('huh.encore.error.noEntryPoints', true));
} else {
try {
$this->entryCollection->getEntries();
} catch (NoEntrypointsException $e) {
$messageAdapter->addError('[Encore Bundle] '.$this->translator->trans('huh.encore.errors.noEntrypoints').' '.$e->getMessage());
}
}
}

if (!isset($this->bundleConfig['use_contao_template_variables'])
|| true !== $this->bundleConfig['use_contao_template_variables']) {
return;
}

if ($layout->addEncore && $layout->addJQuery && (!isset($this->bundleConfig['unset_jquery']) || true !== $this->bundleConfig['unset_jquery'])) {
$this->contaoFramework->getAdapter(Message::class)->addInfo(($GLOBALS['TL_LANG']['tl_layout']['INFO']['jquery_order_conflict'] ?: ''));
$messageAdapter->addInfo(($GLOBALS['TL_LANG']['tl_layout']['INFO']['jquery_order_conflict'] ?: ''));

}
}

Expand Down
16 changes: 13 additions & 3 deletions src/EventListener/Callback/EncoreEntryOptionListener.php
Expand Up @@ -8,22 +8,32 @@

namespace HeimrichHannot\EncoreBundle\EventListener\Callback;

use Contao\Message;
use HeimrichHannot\EncoreBundle\Collection\EntryCollection;
use HeimrichHannot\EncoreBundle\Exception\NoEntrypointsException;
use Symfony\Contracts\Translation\TranslatorInterface;

class EncoreEntryOptionListener
{
private EntryCollection $entryCollection;
private EntryCollection $entryCollection;
private TranslatorInterface $translator;

public function __construct(EntryCollection $entryCollection)
public function __construct(EntryCollection $entryCollection, TranslatorInterface $translator)
{
$this->entryCollection = $entryCollection;
$this->translator = $translator;
}

public function getEntriesAsOptions(): array
{
$choices = [];

$projectEntries = $this->entryCollection->getEntries();
try {
$projectEntries = $this->entryCollection->getEntries();
} catch (NoEntrypointsException $e) {
$projectEntries = [];
Message::addError('[Encore Bundle] '.$this->translator->trans('huh.encore.errors.noEntrypoints').' '.$e->getMessage(), 'huh.encore.error.noEntryPoints');
}

if (empty($projectEntries)) {
return $choices;
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/messages.de.yml
Expand Up @@ -9,3 +9,5 @@ huh.encore:
encoreEntriesSelect_active:
name: "Aktiv"
description: ""
errors:
noEntrypoints: "Es konnten keine Entrypoints gefunden werden. Folgende Fehlermeldung erhalten:"
13 changes: 13 additions & 0 deletions src/Resources/translations/messages.en.yml
@@ -0,0 +1,13 @@
huh.encore:
fields:
encoreEntriesSelect:
name: "Encore entries"
description: "Choose encore entries to include in the template"
encoreEntriesSelect_entry:
name: "Entry"
description: ""
encoreEntriesSelect_active:
name: "Active"
description: ""
errors:
noEntrypoints: "Could not find encore entrypoints. Got following error message:"
14 changes: 10 additions & 4 deletions tests/DataContainer/LayoutContainerTest.php
Expand Up @@ -14,10 +14,12 @@
use Contao\LayoutModel;
use Contao\Message;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\EncoreBundle\Collection\EntryCollection;
use HeimrichHannot\EncoreBundle\DataContainer\LayoutContainer;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;

class LayoutContainerTest extends ContaoTestCase
{
Expand All @@ -27,20 +29,24 @@ public function createTestInstance(array $parameters = []): LayoutContainer
$contaoFramework = $parameters['contaoFramework'] ?? $this->mockContaoFramework();
$requestStack = $parameters['requestStack'] ?? $this->createMock(RequestStack::class);
$scopeMatcher = $parameters['scopeMatcher'] ?? $this->createMock(ScopeMatcher::class);
$entryCollection = $parameters['entryCollection'] ?? $this->createMock(EntryCollection::class);
$translator = $parameters['translator'] ?? $this->createMock(TranslatorInterface::class);

return new LayoutContainer(
$bundleConfig,
$contaoFramework,
$requestStack,
$scopeMatcher
$scopeMatcher,
$entryCollection,
$translator
);
}

public function testOnLoadCallback()
{
$GLOBALS['TL_LANG']['tl_layout']['INFO']['jquery_order_conflict'] = 'Info';

$messageAdapter = $this->mockAdapter(['addInfo']);
$messageAdapter = $this->mockAdapter(['addError', 'addInfo', 'generateUnwrapped', 'hasMessages']);
$messageAdapter->expects($this->never())->method('addInfo')->willReturn(null);

/** @var ContaoFramework|MockObject $contaoFramework */
Expand Down Expand Up @@ -91,7 +97,7 @@ public function testOnLoadCallback()
'addJQuery' => true,
]));

$messageAdapter = $this->mockAdapter(['addInfo']);
$messageAdapter = $this->mockAdapter(['addError', 'addInfo', 'generateUnwrapped', 'hasMessages']);
$messageAdapter->expects($this->once())->method('addInfo')->willReturn(null);
/** @var ContaoFramework|MockObject $contaoFramework */
$contaoFramework = $this->mockContaoFramework([
Expand All @@ -107,7 +113,7 @@ public function testOnLoadCallback()
$instance->onLoadCallback($dc);

$bundleConfig['unset_jquery'] = true;
$messageAdapter = $this->mockAdapter(['addInfo']);
$messageAdapter = $this->mockAdapter(['addError', 'addInfo', 'generateUnwrapped', 'hasMessages']);
$messageAdapter->expects($this->never())->method('addInfo')->willReturn(null);
/** @var ContaoFramework|MockObject $contaoFramework */
$contaoFramework = $this->mockContaoFramework([
Expand Down
Expand Up @@ -11,14 +11,16 @@
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\EncoreBundle\Collection\EntryCollection;
use HeimrichHannot\EncoreBundle\EventListener\Callback\EncoreEntryOptionListener;
use Symfony\Contracts\Translation\TranslatorInterface;

class EncoreEntryOptionListenerTest extends ContaoTestCase
{
public function createTestInstance(array $parameters = []): EncoreEntryOptionListener
{
$entryCollection = $parameters['entryCollection'] ?? $this->createMock(EntryCollection::class);
$translator = $parameters['translator'] ?? $this->createMock(TranslatorInterface::class);

return new EncoreEntryOptionListener($entryCollection);
return new EncoreEntryOptionListener($entryCollection, $translator);
}

public function testGetEntriesAsOptions()
Expand Down

0 comments on commit 22d54e2

Please sign in to comment.