Skip to content

Commit c58f323

Browse files
committed
Chore:
- Minor adjustments to composer.json file; - Cleaned up WebpackEncorePlugin, TagRenderer and EntrypointLookup classes;
1 parent afa169f commit c58f323

File tree

6 files changed

+34
-80
lines changed

6 files changed

+34
-80
lines changed

composer.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
"description": "This is a Micro plugin for webpack-encore support",
44
"type": "library",
55
"version": "0.1",
6-
"minimum-stability": "stable",
76
"license": "MIT",
87
"require": {
9-
"php": "^8.0|^8.1|^8.2",
10-
"micro/kernel-app": "^1",
11-
"micro/kernel-boot-dependency": "^1",
12-
"micro/kernel-boot-configuration": "^1",
138
"micro/plugin-twig": "^1"
149
},
1510
"autoload": {

src/Asset/EntrypointLookup.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
namespace Boo\MicroPlugin\WebpackEncore\Asset;
66

77
use Boo\MicroPlugin\WebpackEncore\Exception\EntrypointNotFoundException;
8+
use Boo\MicroPlugin\WebpackEncore\WebpackEncorePluginConfigurationInterface;
89

910
class EntrypointLookup implements EntrypointLookupInterface
1011
{
1112
private mixed $entriesData = null;
1213

1314
private array $returnedFiles = [];
1415

15-
public function __construct(private readonly string $entrypointJsonPath)
16+
public function __construct(private readonly WebpackEncorePluginConfigurationInterface $configuration)
1617
{
1718
}
1819

@@ -68,7 +69,7 @@ private function validateEntryName(string $entryName)
6869
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s". Try "%s" instead (without the extension).', $entryName, $withoutExtension));
6970
}
7071

71-
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s" in "%s". Found: %s.', $entryName, $this->entrypointJsonPath, implode(', ', array_keys($entriesData['entrypoints']))));
72+
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s" in "%s". Found: %s.', $entryName, $this->getEntrypointJsonPath(), implode(', ', array_keys($entriesData['entrypoints']))));
7273
}
7374
}
7475

@@ -78,20 +79,25 @@ private function getEntriesData(): array
7879
return $this->entriesData;
7980
}
8081

81-
if (!file_exists($this->entrypointJsonPath)) {
82-
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->entrypointJsonPath));
82+
if (!file_exists($this->getEntrypointJsonPath())) {
83+
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->getEntrypointJsonPath()));
8384
}
8485

85-
$this->entriesData = json_decode(file_get_contents($this->entrypointJsonPath), true);
86+
$this->entriesData = json_decode(file_get_contents($this->getEntrypointJsonPath()), true);
8687

8788
if (null === $this->entriesData) {
88-
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->entrypointJsonPath));
89+
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->getEntrypointJsonPath()));
8990
}
9091

9192
if (!isset($this->entriesData['entrypoints'])) {
92-
throw new \InvalidArgumentException(sprintf('Could not find an "entrypoints" key in the "%s" file', $this->entrypointJsonPath));
93+
throw new \InvalidArgumentException(sprintf('Could not find an "entrypoints" key in the "%s" file', $this->getEntrypointJsonPath()));
9394
}
9495

9596
return $this->entriesData;
9697
}
98+
99+
private function getEntrypointJsonPath(): string
100+
{
101+
return $this->configuration->getOutputPath().'/entrypoints.json';
102+
}
97103
}

src/TagRenderer/TagRenderer.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ class TagRenderer implements TagRendererInterface
1010
{
1111
private array $renderedFiles = [];
1212
private array $defaultAttributes = [];
13-
private array $defaultScriptAttributes = [];
13+
private array $defaultScriptAttributes = [
14+
'type' => 'application/javascript'
15+
];
1416
private array $defaultLinkAttributes = [];
1517

1618
public function __construct(private readonly EntrypointLookupInterface $entrypointLookup)
1719
{
1820
}
1921

20-
public function renderWebpackScriptTags(string $entryName, string $packageName = null, array $extraAttributes = []): string
22+
public function renderWebpackScriptTags(string $entryName, array $extraAttributes = []): string
2123
{
2224
$scriptTags = [];
2325

2426
foreach ($this->entrypointLookup->getJavaScriptFiles($entryName) as $filename) {
2527
$attributes = [];
26-
$attributes['src'] = $filename; // $this->getAssetPath($filename, $packageName);
28+
$attributes['src'] = $filename;
2729
$attributes = array_merge($attributes, $this->defaultAttributes, $this->defaultScriptAttributes, $extraAttributes);
2830

2931
$scriptTags[] = sprintf(
@@ -37,14 +39,14 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
3739
return implode('', $scriptTags);
3840
}
3941

40-
public function renderWebpackLinkTags(string $entryName, string $packageName = null, array $extraAttributes = []): string
42+
public function renderWebpackLinkTags(string $entryName, array $extraAttributes = []): string
4143
{
4244
$scriptTags = [];
4345

4446
foreach ($this->entrypointLookup->getCssFiles($entryName) as $filename) {
4547
$attributes = [];
4648
$attributes['rel'] = 'stylesheet';
47-
$attributes['href'] = $filename; // $this->getAssetPath($filename, $packageName);
49+
$attributes['href'] = $filename;
4850
$attributes = array_merge($attributes, $this->defaultAttributes, $this->defaultLinkAttributes, $extraAttributes);
4951

5052
$scriptTags[] = sprintf(

src/TagRenderer/TagRendererInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
interface TagRendererInterface
88
{
9-
public function renderWebpackScriptTags(string $entryName, string $packageName = null, array $extraAttributes = []): string;
9+
public function renderWebpackScriptTags(string $entryName, array $extraAttributes = []): string;
1010

11-
public function renderWebpackLinkTags(string $entryName, string $packageName = null, array $extraAttributes = []): string;
11+
public function renderWebpackLinkTags(string $entryName, array $extraAttributes = []): string;
1212
}

src/WebpackEncorePlugin.php

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace Boo\MicroPlugin\WebpackEncore;
66

7-
use Micro\Component\DependencyInjection\Container;
8-
use Micro\Framework\Kernel\Configuration\PluginConfigurationInterface;
97
use Micro\Framework\Kernel\Plugin\ConfigurableInterface;
10-
use Micro\Framework\Kernel\Plugin\DependencyProviderInterface;
8+
use Micro\Framework\Kernel\Plugin\PluginConfigurationTrait;
119
use Micro\Plugin\Twig\Plugin\TwigExtensionPluginInterface;
1210
use Boo\MicroPlugin\WebpackEncore\Asset\EntrypointLookup;
1311
use Boo\MicroPlugin\WebpackEncore\Asset\EntrypointLookupInterface;
@@ -16,75 +14,30 @@
1614
use Boo\MicroPlugin\WebpackEncore\Twig\Extension\EntryFilesTwigExtension;
1715
use Twig\Extension\ExtensionInterface;
1816

19-
class WebpackEncorePlugin implements DependencyProviderInterface, TwigExtensionPluginInterface, ConfigurableInterface
17+
/**
18+
* @method WebpackEncorePluginConfigurationInterface configuration()
19+
*/
20+
class WebpackEncorePlugin implements TwigExtensionPluginInterface, ConfigurableInterface
2021
{
21-
private ?TagRendererInterface $tagRenderer = null;
22-
23-
private ?EntrypointLookupInterface $entrypointLookup = null;
24-
25-
private WebpackEncorePluginConfigurationInterface $configuration;
26-
27-
public function provideDependencies(Container $container): void
28-
{
29-
$container->register(TagRendererInterface::class, function () {
30-
return $this->createTagRenderer();
31-
});
32-
$container->register(EntrypointLookupInterface::class, function () {
33-
return $this->createEntrypointLookup();
34-
});
35-
}
22+
use PluginConfigurationTrait;
3623

3724
public function provideTwigExtensions(): iterable
3825
{
3926
yield $this->createEntryFilesTwigExtension();
4027
}
4128

42-
/**
43-
* {@inheritDoc}
44-
*/
45-
public function setConfiguration(PluginConfigurationInterface $pluginConfiguration): void
46-
{
47-
if ($pluginConfiguration instanceof WebpackEncorePluginConfigurationInterface) {
48-
$this->configuration = $pluginConfiguration;
49-
50-
return;
51-
}
52-
53-
throw new \InvalidArgumentException(sprintf(
54-
'Plugin configuration should implement %s, provided %s',
55-
WebpackEncorePluginConfigurationInterface::class,
56-
\get_class($pluginConfiguration)
57-
));
58-
}
59-
60-
/**
61-
* {@inheritDoc}
62-
*/
63-
public function configuration(): WebpackEncorePluginConfigurationInterface
64-
{
65-
return $this->configuration;
66-
}
67-
68-
private function createEntryFilesTwigExtension(): ExtensionInterface
29+
protected function createEntryFilesTwigExtension(): ExtensionInterface
6930
{
7031
return new EntryFilesTwigExtension($this->createTagRenderer(), $this->createEntrypointLookup());
7132
}
7233

73-
private function createTagRenderer(): TagRendererInterface
34+
protected function createTagRenderer(): TagRendererInterface
7435
{
75-
if (null === $this->tagRenderer) {
76-
$this->tagRenderer = new TagRenderer($this->createEntrypointLookup());
77-
}
78-
79-
return $this->tagRenderer;
36+
return new TagRenderer($this->createEntrypointLookup());
8037
}
8138

82-
private function createEntrypointLookup(): EntrypointLookupInterface
39+
protected function createEntrypointLookup(): EntrypointLookupInterface
8340
{
84-
if (null === $this->entrypointLookup) {
85-
$this->entrypointLookup = new EntrypointLookup($this->configuration->getOutputPath().'/entrypoints.json');
86-
}
87-
88-
return $this->entrypointLookup;
41+
return new EntrypointLookup($this->configuration());
8942
}
9043
}

src/WebpackEncorePluginConfigurationInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Boo\MicroPlugin\WebpackEncore;
66

7-
use Micro\Framework\Kernel\Configuration\PluginConfigurationInterface;
8-
9-
interface WebpackEncorePluginConfigurationInterface extends PluginConfigurationInterface
7+
interface WebpackEncorePluginConfigurationInterface
108
{
119
public function getOutputPath(): string;
1210
}

0 commit comments

Comments
 (0)