Skip to content

Commit 329f0d7

Browse files
authored
Merge pull request OleksiiBulba#11 from OleksiiBulba/feature/explicit-plugin-dependency
Feature: Explicit plugin dependency
2 parents a7c6ddb + d9d1c1d commit 329f0d7

File tree

12 files changed

+124
-44
lines changed

12 files changed

+124
-44
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
# normal, highest, non-dev installs
16-
php-version: [ '8.1', '8.2' ]
16+
php-version: [ '8.2' ]
1717
dependency-versions: [ 'highest' ]
1818
include:
1919
# testing lowest PHP version with the lowest dependencies
20-
- php-version: '8.1'
20+
- php-version: '8.2'
2121
dependency-versions: 'lowest'
2222

2323
# testing dev versions with the highest PHP

.github/workflows/static.yaml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ on: [pull_request]
22
name: Static analysis
33

44
jobs:
5+
composer-verify:
6+
name: Composer verify
7+
runs-on: ubuntu-22.04
8+
9+
steps:
10+
- name: Setup PHP
11+
uses: shivammathur/setup-php@v2
12+
with:
13+
php-version: 8.2
14+
coverage: none
15+
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: Verify
20+
run: composer validate --strict
21+
522
phpstan:
623
name: PHPStan
724
runs-on: ubuntu-22.04
@@ -13,7 +30,7 @@ jobs:
1330
- name: Setup PHP
1431
uses: shivammathur/setup-php@v2
1532
with:
16-
php-version: 8.1
33+
php-version: 8.2
1734
coverage: none
1835
tools: cs2pr
1936

@@ -37,15 +54,15 @@ jobs:
3754
- name: Setup PHP
3855
uses: shivammathur/setup-php@v2
3956
with:
40-
php-version: 8.1
57+
php-version: 8.2
4158
coverage: none
4259
tools: cs2pr
4360

4461
- name: Install php-cs-fixer
4562
uses: ramsey/composer-install@v2
4663

4764
- name: PHP-CS-Fixer
48-
run: ./vendor/bin/php-cs-fixer fix --verbose --dry-run --format=checkstyle --using-cache=no | cs2pr
65+
run: PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --dry-run --format=checkstyle --using-cache=no | cs2pr
4966

5067
psalm:
5168
name: Psalm
@@ -57,7 +74,7 @@ jobs:
5774
- name: Setup PHP
5875
uses: shivammathur/setup-php@v2
5976
with:
60-
php-version: 8.1
77+
php-version: 8.2
6178
coverage: none
6279
tools: vimeo/psalm:4.11.2
6380

@@ -78,7 +95,7 @@ jobs:
7895
- name: Setup PHP
7996
uses: shivammathur/setup-php@v2
8097
with:
81-
php-version: 8.1
98+
php-version: 8.2
8299
coverage: none
83100
tools: composer-normalize
84101

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ composer.lock
44
.php-cs-fixer.cache
55
test-coverage-report
66
phpunit.xml
7+
.php-cs-fixer.php
8+
phpstan.neon

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## Added
11+
12+
- Explicit dependency on TwigPlugin, from now on it will be loaded automatically by `micro/kernel-boot-plugin-depended` package;
13+
- Added documentation for twig functions in php annotations;
14+
15+
## Changed
16+
17+
- Dropped support for php 8.1;
18+
1019
## [1.1] - 2022-12-20
1120

1221
### Updated

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"description": "This is a Micro plugin for webpack-encore support",
44
"license": "MIT",
55
"type": "micro-plugin",
6-
"version": "1.1",
76
"authors": [
87
{
98
"name": "Oleksii Bulba",
@@ -13,7 +12,9 @@
1312
}
1413
],
1514
"require": {
16-
"php": "^8.1 || ^8.2",
15+
"php": "^8.2",
16+
"micro/kernel-boot-configuration": "^1",
17+
"micro/kernel-boot-plugin-depended": "^1",
1718
"micro/plugin-twig": "^1",
1819
"symfony/serializer": "^6.2"
1920
},
@@ -52,13 +53,16 @@
5253
"php-cs-try": "./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
5354
"phpstan": "./vendor/bin/phpstan analyze --no-progress",
5455
"phpunit": "./vendor/bin/phpunit",
56+
"psalm": "./vendor/bin/psalm --no-progress --show-info=true",
5557
"statics": [
5658
"@phpstan",
57-
"@php-cs-try"
59+
"@php-cs-try",
60+
"@psalm"
5861
],
5962
"test": [
6063
"@statics",
61-
"@phpunit",
64+
"composer validate --strict",
65+
"composer normalize",
6266
"@coverage"
6367
]
6468
}

src/Asset/EntrypointLookup.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ public function __construct(
3030
}
3131

3232
/**
33-
* {@inheritDoc}
33+
* {@inheritdoc}
3434
*/
3535
public function getJavaScriptFiles(string $entryName): array
3636
{
3737
return $this->getEntryFiles($entryName, 'js');
3838
}
3939

4040
/**
41-
* {@inheritDoc}
41+
* {@inheritdoc}
4242
*/
4343
public function getCssFiles(string $entryName): array
4444
{
@@ -106,20 +106,20 @@ private function getEntriesData(): array
106106
}
107107

108108
try {
109-
$this->entriesData = $this->decoder->decode(file_get_contents($this->getEntrypointJsonPath()), JsonEncoder::FORMAT);
109+
$entriesData = $this->decoder->decode(file_get_contents($this->getEntrypointJsonPath()), JsonEncoder::FORMAT);
110110
} catch (UnexpectedValueException $e) {
111111
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->getEntrypointJsonPath()), 0, $e);
112112
}
113113

114-
if (null === $this->entriesData) {
114+
if (!\is_array($entriesData)) {
115115
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->getEntrypointJsonPath()));
116116
}
117117

118-
if (!isset($this->entriesData['entrypoints'])) {
118+
if (!isset($entriesData['entrypoints'])) {
119119
throw new \InvalidArgumentException(sprintf('Could not find an "entrypoints" key in the "%s" file', $this->getEntrypointJsonPath()));
120120
}
121121

122-
return $this->entriesData;
122+
return $this->entriesData = $entriesData;
123123
}
124124

125125
private function getEntrypointJsonPath(): string

src/Asset/EntrypointLookupInterface.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,37 @@
1616
interface EntrypointLookupInterface
1717
{
1818
/**
19+
* Twig function to get all JavaScript files for an entry.
20+
*
21+
* encore_entry_js_files(string $entryName): array
22+
*
23+
* Usage: {{ encore_entry_js_files('app') }}
24+
*
1925
* @throws EntrypointNotFoundException if an entry name is passed that does not exist in entrypoints.json
2026
*
2127
* @psalm-return array<string>
2228
*/
2329
public function getJavaScriptFiles(string $entryName): array;
2430

2531
/**
32+
* Twig function to get all CSS files for an entry.
33+
*
34+
* encore_entry_css_files(string $entryName): array
35+
*
36+
* Usage: {{ encore_entry_css_files('app') }}
37+
*
2638
* @throws EntrypointNotFoundException if an entry name is passed that does not exist in entrypoints.json
2739
*
2840
* @psalm-return array<string>
2941
*/
3042
public function getCssFiles(string $entryName): array;
3143

44+
/**
45+
* Twig function to check if an entry exists.
46+
*
47+
* encore_entry_exists(string $entryName): bool
48+
*
49+
* Usage: {% if encore_entry_exists('app') %}
50+
*/
3251
public function entryExists(string $entryName): bool;
3352
}

src/TagRenderer/TagRenderer.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
use OleksiiBulba\WebpackEncorePlugin\Asset\EntrypointLookupInterface;
1515

16-
class TagRenderer implements TagRendererInterface
16+
readonly class TagRenderer implements TagRendererInterface
1717
{
1818
public function __construct(
19-
private readonly EntrypointLookupInterface $entrypointLookup,
20-
private readonly array $defaultAttributes = [],
21-
private readonly array $defaultScriptAttributes = ['type' => 'application/javascript'],
22-
private readonly array $defaultLinkAttributes = []
19+
private EntrypointLookupInterface $entrypointLookup,
20+
private array $defaultAttributes = [],
21+
private array $defaultScriptAttributes = ['type' => 'application/javascript'],
22+
private array $defaultLinkAttributes = ['rel' => 'stylesheet']
2323
) {
2424
}
2525

@@ -28,13 +28,14 @@ public function renderWebpackScriptTags(string $entryName, array $extraAttribute
2828
$scriptTags = [];
2929

3030
foreach ($this->entrypointLookup->getJavaScriptFiles($entryName) as $filename) {
31-
$attributes = [];
32-
$attributes['src'] = $filename;
33-
$attributes = array_merge($attributes, $this->defaultAttributes, $this->defaultScriptAttributes, $extraAttributes);
34-
3531
$scriptTags[] = sprintf(
3632
/* @lang text */ '<script %s></script>',
37-
$this->convertArrayToAttributes($attributes)
33+
$this->convertArrayToAttributes(array_merge(
34+
['src' => $filename],
35+
$this->defaultAttributes,
36+
$this->defaultScriptAttributes,
37+
$extraAttributes
38+
))
3839
);
3940
}
4041

@@ -46,14 +47,14 @@ public function renderWebpackLinkTags(string $entryName, array $extraAttributes
4647
$scriptTags = [];
4748

4849
foreach ($this->entrypointLookup->getCssFiles($entryName) as $filename) {
49-
$attributes = [];
50-
$attributes['rel'] = 'stylesheet';
51-
$attributes['href'] = $filename;
52-
$attributes = array_merge($attributes, $this->defaultAttributes, $this->defaultLinkAttributes, $extraAttributes);
53-
5450
$scriptTags[] = sprintf(
5551
/* @lang text */ '<link %s>',
56-
$this->convertArrayToAttributes($attributes)
52+
$this->convertArrayToAttributes(array_merge(
53+
['href' => $filename],
54+
$this->defaultAttributes,
55+
$this->defaultLinkAttributes,
56+
$extraAttributes
57+
))
5758
);
5859
}
5960

@@ -68,9 +69,9 @@ private function convertArrayToAttributes(array $attributesMap): string
6869
});
6970

7071
return implode(' ', array_map(
71-
static function ($key, string|null|bool $value) {
72+
static function ($key, string|true|null $value) {
7273
// allows for things like defer: true to only render "defer"
73-
if (\is_bool($value) || null === $value) {
74+
if (true === $value || null === $value) {
7475
return $key;
7576
}
7677

src/TagRenderer/TagRendererInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@
1313

1414
interface TagRendererInterface
1515
{
16+
/**
17+
* Twig function to render script tags for an entry.
18+
*
19+
* encore_entry_script_tags(string $entryName, string $packageName = null): string
20+
*
21+
* Usage: {{ encore_entry_script_tags('app') }}
22+
*/
1623
public function renderWebpackScriptTags(string $entryName, array $extraAttributes = []): string;
1724

25+
/**
26+
* Twig function to render link tags for an entry.
27+
*
28+
* encore_entry_link_tags(string $entryName, string $packageName = null): string
29+
*
30+
* Usage: {{ encore_entry_link_tags('app') }}
31+
*/
1832
public function renderWebpackLinkTags(string $entryName, array $extraAttributes = []): string;
1933
}

src/Twig/Extension/EntryFilesTwigExtension.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@
2121
*/
2222
class EntryFilesTwigExtension extends AbstractExtension
2323
{
24-
private TagRendererInterface $tagRenderer;
25-
private EntrypointLookupInterface $entrypointLookup;
26-
2724
public function __construct(
28-
TagRendererInterface $tagRenderer,
29-
EntrypointLookupInterface $entrypointLookupCollection
25+
private readonly TagRendererInterface $tagRenderer,
26+
private readonly EntrypointLookupInterface $entrypointLookup
3027
) {
31-
$this->tagRenderer = $tagRenderer;
32-
$this->entrypointLookup = $entrypointLookupCollection;
3328
}
3429

30+
/**
31+
* Returns twig functions for registering them in twig environment.
32+
*
33+
* There are 5 available twig functions:
34+
* - encore_entry_js_files
35+
* - encore_entry_css_files
36+
* - encore_entry_script_tags
37+
* - encore_entry_link_tags
38+
* - encore_entry_exists
39+
*
40+
* @return TwigFunction[]
41+
*/
3542
public function getFunctions(): array
3643
{
3744
return [

0 commit comments

Comments
 (0)