Skip to content

Commit

Permalink
Merge pull request #3 from p-chess/improve-readme
Browse files Browse the repository at this point in the history
🎨 improve Twig extension
  • Loading branch information
garak authored Jan 5, 2025
2 parents 5668477 + 8dfbb94 commit cde2528
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 48 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,44 @@ on:

jobs:
phpstan:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
name: PHPStan
steps:
- name: Checkout
uses: actions/checkout@v3
- name: PHPStan
uses: docker://oskarstark/phpstan-ga
uses: docker://oskarstark/phpstan-ga:1.8.0
env:
REQUIRE_DEV: true
with:
args: analyse
cs-fixer:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
name: PHP-CS-Fixer
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Fix CS
uses: docker://oskarstark/php-cs-fixer-ga
tests:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
matrix:
php:
- '8.0'
- '8.1'
- '8.2'
- '8.3'
- '8.4'
include:
- description: 'Symfony 6.4'
php: '8.0'
php: '8.1'
composer_option: '--prefer-lowest'
- description: 'Symfony 6.4'
php: '8.2'
symfony: 6.4.*
- description: 'Symfony 7.0'
php: '8.3'
symfony: 7.0.*
symfony: 6.4.*
- description: 'Symfony 7.2'
php: '8.4'
symfony: 7.2.*
name: PHP ${{ matrix.php }} tests (${{ matrix.description }})
steps:
- name: Checkout
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ Run `composer require p-chess/chess-bundle`
## Configuration

Create a service that extends `PChess\ChessBundle\HtmlOutput` and
implements required methods.
You probably want to inject Symfony's router service here, and use it
to return required URLs.
implement the required methods.
You probably want to inject Symfony's router service here and use it
to return the required URLs.
Note that each route can be provided with an identifier for your game.

Create a configuration file, and use a content like the following:
Create a configuration file, and use content like the following:

```yaml
# config/packages/chess.yaml
chess:
output_service: App\YourOutputService
```
This is an example of how routes can be defined (using an "id" parameter as identifier):
This is an example of how routes can be defined (using an "id" parameter as an identifier):
```yaml
# config/routes.yaml
Expand Down Expand Up @@ -52,11 +52,11 @@ move_end:
You can inject a service implementing `\PChess\ChessBundle\ChessProviderInterface` in your controller, then
implement different actions, using provided `\PChess\Chess\Chess` object.

In your template, you can use Twig function `chess_render(chess)` to render the board.
In your template, you can use the Twig function `chess_render(chess)` to render the board.
If you need to pass an identifier, use `chess_render(chess, identifier)` instead.

The main service you can use is `\PChess\ChessBundle\SessionChessProvider`.
This service allows you to keep chess games in session, providing following methods:
This service allows you to keep chess games in session, providing the following methods:

* `getChess($identifier, $fen)` to get main `\PChess\Chess\Chess` instance (as provided by interface)
* `restart($identifier)` to restart the game
Expand All @@ -68,7 +68,7 @@ Using `$identifier` is not mandatory.

### Styling

You can use provided `_board.scss` file to style the board:
You can use the provided `_board.scss` file to style the board:

`@import '~@p-chess/chess-bundle/scss/board';`

Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
}
],
"require": {
"php": "^8.0",
"p-chess/chess": "^0.4",
"symfony/config": "^5.4 || ^6.4 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.4 || ^7.0",
"symfony/routing": "^5.4 || ^6.4 || ^7.0",
"twig/twig": "^3.0"
"php": "^8.1",
"p-chess/chess": "^0.6",
"symfony/config": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/routing": "^6.4 || ^7.0",
"twig/twig": "^3.18"
},
"require-dev": {
"dg/bypass-finals": "^1.3",
"dg/bypass-finals": "^1.6",
"phpunit/phpunit": "^9.6",
"symfony/phpunit-bridge": "^7.0"
"symfony/phpunit-bridge": "^7.2"
},
"config": {
"sort-packages": true
Expand Down
5 changes: 4 additions & 1 deletion config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
</service>
<service id="PChess\ChessBundle\SimpleChessProvider"/>
<service id="PChess\ChessBundle\Twig\ChessExtension">
<argument type="abstract" key="$output"/>
<tag name="twig.extension"/>
</service>
<service id="PChess\ChessBundle\Twig\ChessRuntime">
<argument type="abstract" key="$output"/>
<tag name="twig.runtime"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion src/ChessProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface ChessProviderInterface
{
public function getChess(mixed $identifier = null, string $fen = null): Chess;
public function getChess(mixed $identifier = null, ?string $fen = null): Chess;
}
4 changes: 2 additions & 2 deletions src/DependencyInjection/CompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PChess\ChessBundle\DependencyInjection;

use PChess\ChessBundle\Twig\ChessExtension as TwigExtension;
use PChess\ChessBundle\Twig\ChessRuntime;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand All @@ -13,6 +13,6 @@ public function process(ContainerBuilder $container): void
/** @var string $service */
$service = $container->getParameter('chess.output_service');
$output = $container->getDefinition($service);
$container->getDefinition(TwigExtension::class)->replaceArgument(0, $output);
$container->getDefinition(ChessRuntime::class)->replaceArgument(0, $output);
}
}
4 changes: 2 additions & 2 deletions src/HtmlOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract public function getCancelUrl(mixed $identifier = null): string;

abstract public function getPromotionUrl(string $from, string $to, mixed $identifier = null): string;

public function generateLinks(Chess $chess, string $from = null, mixed $identifier = null): array
public function generateLinks(Chess $chess, ?string $from = null, mixed $identifier = null): array
{
$links = [];
$allowedMoves = self::getAllowedMoves($chess, $from);
Expand Down Expand Up @@ -58,7 +58,7 @@ public function generateLinks(Chess $chess, string $from = null, mixed $identifi
/**
* @return array<string, array<int, string>>
*/
private static function getAllowedMoves(Chess $chess, string $from = null): array
private static function getAllowedMoves(Chess $chess, ?string $from = null): array
{
$moves = $chess->moves($from ? Board::SQUARES[$from] : null);
$return = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Mover.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class Mover
*
* @return array<string, array<int, string>>
*/
public static function getAllowedMoves(Chess $chess, string $from = null): array
public static function getAllowedMoves(Chess $chess, ?string $from = null): array
{
$moves = $chess->moves($from ? Board::SQUARES[$from] : null);
$return = [];
Expand Down
2 changes: 1 addition & 1 deletion src/SessionChessProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(private RequestStack $requestStack, private string $
{
}

public function getChess(mixed $identifier = null, string $fen = null, History $history = null): Chess
public function getChess(mixed $identifier = null, ?string $fen = null, ?History $history = null): Chess
{
$name = $this->name.$identifier;
$chess = $this->getSession()->has($name) ? $this->getSession()->get($name) : new Chess($fen, $history);
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleChessProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class SimpleChessProvider implements ChessProviderInterface
{
public function getChess(mixed $identifier = null, string $fen = null): Chess
public function getChess(mixed $identifier = null, ?string $fen = null): Chess
{
return new Chess($fen);
}
Expand Down
8 changes: 1 addition & 7 deletions src/Twig/ChessExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@

final class ChessExtension extends AbstractExtension
{
public function __construct(private HtmlOutput $output)
{
}

/**
* @codeCoverageIgnore
*
* @return array<int, TwigFunction>
*/
public function getFunctions(): array
{
return [
new TwigFunction('chess_render', [$this->output, 'render'], ['is_safe' => ['html']]),
new TwigFunction('chess_render', [ChessRuntime::class, 'render'], ['is_safe' => ['html']]),
];
}
}
20 changes: 20 additions & 0 deletions src/Twig/ChessRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PChess\ChessBundle\Twig;

use PChess\Chess\Chess;
use PChess\ChessBundle\HtmlOutput;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class ChessRuntime extends AbstractExtension
{
public function __construct(private HtmlOutput $output)
{
}

public function render(Chess $chess, ?string $from = null, mixed $identifier = null): string
{
return $this->output->render($chess, $from, $identifier);
}
}
4 changes: 2 additions & 2 deletions tests/DependencyInjection/CompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PChess\ChessBundle\Tests\DependencyInjection;

use PChess\ChessBundle\DependencyInjection\CompilerPass;
use PChess\ChessBundle\Twig\ChessExtension;
use PChess\ChessBundle\Twig\ChessRuntime;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -19,7 +19,7 @@ public function testProcess(): void
$container = $this->createMock(ContainerBuilder::class);
$container->expects(self::once())->method('getParameter')->with('chess.output_service')->willReturn('foo');
$container->expects(self::at(1))->method('getDefinition')->with('foo')->willReturn($definition);
$container->expects(self::at(2))->method('getDefinition')->with(ChessExtension::class)->willReturn($definition);
$container->expects(self::at(2))->method('getDefinition')->with(ChessRuntime::class)->willReturn($definition);
$pass = new CompilerPass();
$pass->process($container);
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Twig/ChessExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace PChess\ChessBundle\Tests\Twig;

use PChess\ChessBundle\HtmlOutput;
use PChess\ChessBundle\Twig\ChessExtension;
use PHPUnit\Framework\TestCase;

final class ChessExtensionTest extends TestCase
{
public function testFunctions(): void
{
$output = $this->createMock(HtmlOutput::class);
$ext = new ChessExtension($output);
$ext = new ChessExtension();
self::assertIsArray($ext->getFunctions());
}
}
18 changes: 18 additions & 0 deletions tests/Twig/ChessRuntimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PChess\ChessBundle\Tests\Twig;

use PChess\Chess\Chess;
use PChess\ChessBundle\HtmlOutput;
use PChess\ChessBundle\Twig\ChessRuntime;
use PHPUnit\Framework\TestCase;

final class ChessRuntimeTest extends TestCase
{
public function testRender(): void
{
$output = $this->createMock(HtmlOutput::class);
$runtime = new ChessRuntime($output);
self::assertIsString($runtime->render(new Chess()));
}
}

0 comments on commit cde2528

Please sign in to comment.