Skip to content

Commit 393bc94

Browse files
committed
Initial work on library
0 parents  commit 393bc94

File tree

12 files changed

+856
-0
lines changed

12 files changed

+856
-0
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.github export-ignore
2+
/tests export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore
5+
.php-cs-fixer.dist.php export-ignore
6+
phpstan.neon.dist export-ignore
7+
phpunit.xml.dist export-ignore

.github/workflows/ci.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: CI
2+
3+
on:
4+
pull_request: ~
5+
push:
6+
branches: ['main']
7+
schedule:
8+
- cron: '0 */12 * * *'
9+
10+
jobs:
11+
test:
12+
uses: SymfonyCasts/.github/.github/workflows/phpunit.yaml@main
13+
with:
14+
php-version-matrix: '["8.1", "8.2"]'
15+
php-version-lowest: '8.1'
16+
17+
test_windows:
18+
uses: SymfonyCasts/.github/.github/workflows/phpunit.yaml@main
19+
with:
20+
php-version-matrix: '["8.2"]'
21+
php-version-lowest: '8.2'
22+
runs-on: windows-latest
23+
24+
composer-validate:
25+
uses: SymfonyCasts/.github/.github/workflows/composer-validate.yaml@main
26+
27+
cs:
28+
uses: SymfonyCasts/.github/.github/workflows/php-cs-fixer.yaml@main
29+
30+
sca:
31+
uses: SymfonyCasts/.github/.github/workflows/phpstan.yaml@main

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
composer.lock
3+
.php-cs-fixer.cache
4+
.phpunit.result.cache

.php-cs-fixer.dist.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
if (!file_exists(__DIR__.'/src') || !file_exists(__DIR__.'/tests')) {
4+
exit(0);
5+
}
6+
7+
$finder = (new \PhpCsFixer\Finder())
8+
->in([__DIR__.'/src', __DIR__.'/tests'])
9+
;
10+
11+
return (new \PhpCsFixer\Config())
12+
->setRules(array(
13+
'@Symfony' => true,
14+
'@Symfony:risky' => true,
15+
'phpdoc_to_comment' => false,
16+
'header_comment' => [
17+
'header' => <<<EOF
18+
This file is part of the SymfonyCasts DynamicForms package.
19+
Copyright (c) SymfonyCasts <https://symfonycasts.com/>
20+
For the full copyright and license information, please view the LICENSE
21+
file that was distributed with this source code.
22+
EOF
23+
]
24+
))
25+
->setRiskyAllowed(true)
26+
->setFinder($finder)
27+
;

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) SymfonyCasts <https://symfonycasts.com/>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Dynamic Symfony Forms! Woh!
2+
3+
TODO

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "symfonycasts/dynamic-forms",
3+
"description": "Add dynamic/dependent fields to Symfony forms",
4+
"license": "MIT",
5+
"type": "library",
6+
"keywords": ["symfony", "forms"],
7+
"authors": [
8+
{
9+
"name": "Ryan Weaver",
10+
"homepage": "https://symfonycasts.com"
11+
}
12+
],
13+
"require": {
14+
"php": ">=8.1",
15+
"symfony/form": "^6.3"
16+
},
17+
"require-dev": {
18+
"symfony/framework-bundle": "^6.3",
19+
"symfony/phpunit-bridge": "^6.3",
20+
"phpstan/phpstan": "1.11.x-dev"
21+
},
22+
"minimum-stability": "dev",
23+
"autoload": {
24+
"psr-4": {
25+
"Symfonycasts\\DynamicForms\\": "src"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Symfonycasts\\DynamicForms\\Tests\\": "tests/"
31+
}
32+
}
33+
}

phpstan.neon.dist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
level: 4
3+
paths:
4+
- src
5+
ignoreErrors:
6+
-
7+
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\:\\:scalarNode\\(\\)\\.$#"
8+
count: 1
9+
path: src/DependencyInjection/TailwindExtension.php

phpunit.xml.dist

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="vendor/autoload.php"
9+
convertDeprecationsToExceptions="false"
10+
>
11+
<php>
12+
<ini name="display_errors" value="1" />
13+
<ini name="error_reporting" value="-1" />
14+
<server name="KERNEL_CLASS" value="Symfonycasts\TailwindBundle\Tests\fixtures\TailwindTestKernel" />
15+
<server name="APP_ENV" value="test" force="true" />
16+
<server name="SHELL_VERBOSITY" value="-1" />
17+
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
18+
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
19+
</php>
20+
21+
<testsuites>
22+
<testsuite name="Bundle Test Suite">
23+
<directory>tests</directory>
24+
</testsuite>
25+
</testsuites>
26+
27+
<coverage processUncoveredFiles="true">
28+
<include>
29+
<directory suffix=".php">src</directory>
30+
</include>
31+
</coverage>
32+
33+
<listeners>
34+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
35+
</listeners>
36+
</phpunit>

src/DependentFieldConfig.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Symfonycasts\DynamicForms;
4+
5+
use Symfony\Component\Form\FormEvents;
6+
7+
/**
8+
* Holds the configuration for a dynamic field & what listeners have been executed.
9+
*/
10+
class DependentFieldConfig
11+
{
12+
public array $callbackExecuted = [
13+
FormEvents::PRE_SET_DATA => false,
14+
FormEvents::POST_SUBMIT => false,
15+
];
16+
17+
public function __construct(
18+
public string $name,
19+
public array $dependencies,
20+
public \Closure $callback,
21+
)
22+
{
23+
}
24+
25+
public function isReady(array $availableDependencyData, string $eventName): bool
26+
{
27+
if ($this->callbackExecuted[$eventName]) {
28+
return false;
29+
}
30+
31+
foreach ($this->dependencies as $dependency) {
32+
if (!array_key_exists($dependency, $availableDependencyData)) {
33+
return false;
34+
}
35+
}
36+
37+
return true;
38+
}
39+
40+
public function execute(array $availableDependencyData, string $eventName): DynamicField
41+
{
42+
$configurableFormBuilder = new DynamicField();
43+
44+
$this->callbackExecuted[$eventName] = true;
45+
$dependencyData = array_map(fn (string $dependency) => $availableDependencyData[$dependency], $this->dependencies);
46+
$this->callback->__invoke($configurableFormBuilder, ...$dependencyData);
47+
48+
return $configurableFormBuilder;
49+
}
50+
}

0 commit comments

Comments
 (0)