Skip to content

Commit

Permalink
added ConfigurationHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Apr 12, 2021
1 parent c169b77 commit 5dbec2c
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.11.0-DEV] - 2021-04-11
- added ConfigurationHelper class

## [1.10.0] - 2021-03-08
This release enhances the possibilities to include encore in custom routes. See developer documentation for more information.

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"symfony/webpack-encore-bundle": "^1.0",
"heimrichhannot/contao-multi-column-editor-bundle": "^1.0|^2.0",
"heimrichhannot/contao-utils-bundle": "^2.90",
"twig/twig": "^1.38.3|^2.7"
"twig/twig": "^1.38.3|^2.7",
"webmozart/path-util": "^2.3"
},
"require-dev": {
"contao/test-case": "1.1.*",
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/EncoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class EncoreExtension extends Extension implements PrependExtensionInterface

private $encoreCacheEnabled = false;

private $outputPath = '';

public function getAlias()
{
return 'huh_encore';
Expand All @@ -35,8 +37,10 @@ public function prepend(ContainerBuilder $container)
$processedConfigs = $this->processConfiguration(new \Symfony\WebpackEncoreBundle\DependencyInjection\Configuration(), $configs);

$this->encoreCacheEnabled = $processedConfigs['cache'];

if (false !== $processedConfigs['output_path']) {
$this->entrypointsJsons[] = $processedConfigs['output_path'].'/entrypoints.json';
$this->outputPath = $processedConfigs['output_path'];
} else {
// TODO: multiple builds are not supported yet
throw new FeatureNotSupportedException('Multiple encore builds are currently not supported by the Contao Encore Bundle');
Expand All @@ -57,6 +61,7 @@ public function load(array $configs, ContainerBuilder $container)

$processedConfig['entrypoints_jsons'] = $this->entrypointsJsons;
$processedConfig['encore_cache_enabled'] = $this->encoreCacheEnabled;
$processedConfig['outputPath'] = $this->outputPath;

$container->setParameter('huh_encore', $processedConfig);

Expand Down
94 changes: 94 additions & 0 deletions src/Helper/ConfigurationHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\EncoreBundle\Helper;

use Contao\PageModel;
use HeimrichHannot\UtilsBundle\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use Symfony\Component\HttpFoundation\RequestStack;
use Webmozart\PathUtil\Path;

class ConfigurationHelper
{
/**
* @var ContainerUtil
*/
protected $containerUtil;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var ModelUtil
*/
protected $modelUtil;
/**
* @var array
*/
protected $bundleConfig;
/**
* @var string
*/
protected $webDir;

public function __construct(ContainerUtil $containerUtil, RequestStack $requestStack, ModelUtil $modelUtil, array $bundleConfig, string $webDir)
{
$this->containerUtil = $containerUtil;
$this->requestStack = $requestStack;
$this->modelUtil = $modelUtil;
$this->bundleConfig = $bundleConfig;
$this->webDir = $webDir;
}

/**
* Check if encore is enabled on the current page.
*/
public function isEnabledOnCurrentPage(?PageModel $pageModel = null): bool
{
if (!$this->containerUtil->isFrontend()) {
return false;
}

if (null !== $this->requestStack->getParentRequest()) {
return false;
}

if (null === $pageModel) {
global $objPage;
$pageModel = $objPage;
}

if (!$pageModel) {
return false;
}

$layout = $this->modelUtil->findModelInstanceByPk('tl_layout', $pageModel->layoutId);
if (!$layout || !$layout->addEncore) {
return false;
}

return true;
}

/**
* Return the relative path to the encore output folder.
*/
public function getRelativeOutputPath(): string
{
return Path::makeRelative($this->bundleConfig['outputPath'], $this->webDir);
}

/**
* Return the absolute path to the encore output folder.
*/
public function getAbsoluteOutputPath(): string
{
return $this->bundleConfig['outputPath'];
}
}
3 changes: 1 addition & 2 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ services:
$bundleConfig: '%huh_encore%'
$webDir: '%contao.web_dir%'


HeimrichHannot\EncoreBundle\Asset\:
resource: '../../Asset/*'
exclude: '../../Asset/{EntrypointCollection.php,EntrypointsJsonLookup.php}'
Expand Down Expand Up @@ -36,5 +35,5 @@ services:
public: true

HeimrichHannot\EncoreBundle\:
resource: "../../{DataContainer,Dca}/*"
resource: "../../{DataContainer,Dca,Helper}/*"
public: true
128 changes: 128 additions & 0 deletions tests/Helper/ConfigurationHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\EncoreBundle\Test\Helper;

use Contao\LayoutModel;
use Contao\PageModel;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\EncoreBundle\Helper\ConfigurationHelper;
use HeimrichHannot\UtilsBundle\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class ConfigurationHelperTest extends ContaoTestCase
{
public function createTestInstance(array $parameters = [])
{
$containerUtil = $parameters['containerUtil'] ?? $this->createMock(ContainerUtil::class);
$requestStack = $parameters['requestStack'] ?? $this->createMock(RequestStack::class);
$modelUtil = $parameters['modelUtil'] ?? $this->createMock(ModelUtil::class);
$bundleConfig = $parameters['bundleConfig'] ?? [];
$webDir = $parameters['webDir'] ?? '';

$instance = new ConfigurationHelper(
$containerUtil,
$requestStack,
$modelUtil,
$bundleConfig,
$webDir
);

return $instance;
}

public function testIsEnabledOnCurrentPage()
{
$containerUtil = $this->createMock(ContainerUtil::class);
$containerUtil->method('isFrontend')->willReturn(false);
$instance = $this->createTestInstance([
'containerUtil' => $containerUtil,
]);
$this->assertFalse($instance->isEnabledOnCurrentPage());

$containerUtil = $this->createMock(ContainerUtil::class);
$containerUtil->method('isFrontend')->willReturn(true);
$requestStack = $this->createMock(RequestStack::class);
$requestStack->method('getParentRequest')->willReturn(new Request());
$instance = $this->createTestInstance([
'containerUtil' => $containerUtil,
'requestStack' => $requestStack,
]);
$this->assertFalse($instance->isEnabledOnCurrentPage());

$containerUtil = $this->createMock(ContainerUtil::class);
$containerUtil->method('isFrontend')->willReturn(true);
$requestStack = $this->createMock(RequestStack::class);
$requestStack->method('getParentRequest')->willReturn(null);
$instance = $this->createTestInstance([
'containerUtil' => $containerUtil,
'requestStack' => $requestStack,
]);
$pageModel = $this->mockClassWithProperties(PageModel::class, []);
$this->assertFalse($instance->isEnabledOnCurrentPage($pageModel));

$modelUtil = $this->createMock(ModelUtil::class);
$modelUtil->method('findModelInstanceByPk')->willReturnCallback(function ($table, $id) {
switch ($id) {
case '1':
return null;
case '2':
return $this->mockClassWithProperties(LayoutModel::class, ['addEncore' => '']);
case '3':
return $this->mockClassWithProperties(LayoutModel::class, ['addEncore' => '1']);
}
});
$instance = $this->createTestInstance([
'containerUtil' => $containerUtil,
'requestStack' => $requestStack,
'modelUtil' => $modelUtil,
]);
$pageModel = $this->mockClassWithProperties(PageModel::class, ['layoutId' => 2]);
$this->assertFalse($instance->isEnabledOnCurrentPage($pageModel));

$pageModel = $this->mockClassWithProperties(PageModel::class, ['layoutId' => 3]);
$this->assertTrue($instance->isEnabledOnCurrentPage($pageModel));

$this->assertFalse($instance->isEnabledOnCurrentPage());

$GLOBALS['objPage'] = $this->mockClassWithProperties(PageModel::class, ['layoutId' => 3]);
$this->assertTrue($instance->isEnabledOnCurrentPage());

unset($GLOBALS['objPage']);
}

public function testGetRelativeOutputPath()
{
$instance = $this->createTestInstance([
'bundleConfig' => ['outputPath' => ''],
]);
$this->assertEmpty($instance->getRelativeOutputPath());

$instance = $this->createTestInstance([
'bundleConfig' => ['outputPath' => '/a/b/c/d'],
'webDir' => '/a/b/c',
]);
$this->assertSame('d', $instance->getRelativeOutputPath());
}

public function testGetAbsoluteOutputPath()
{
$instance = $this->createTestInstance([
'bundleConfig' => ['outputPath' => ''],
]);
$this->assertEmpty($instance->getAbsoluteOutputPath());

$instance = $this->createTestInstance([
'bundleConfig' => ['outputPath' => '/a/b/c/d'],
'webDir' => '/a/b/c',
]);
$this->assertSame('/a/b/c/d', $instance->getAbsoluteOutputPath());
}
}

0 comments on commit 5dbec2c

Please sign in to comment.