-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
191 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Tienvx\UX\CollectionJs\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; | ||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
class CollectionJsExtension extends Extension implements PrependExtensionInterface | ||
{ | ||
public function prepend(ContainerBuilder $container) | ||
{ | ||
// Register the Collection Js form theme if TwigBundle is available | ||
$bundles = $container->getParameter('kernel.bundles'); | ||
|
||
if (!isset($bundles['TwigBundle'])) { | ||
return; | ||
} | ||
|
||
$container->prependExtensionConfig('twig', ['form_themes' => ['@CollectionJs/bootstrap_5_layout.html.twig']]); | ||
} | ||
|
||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.php'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Tienvx\UX\CollectionJs\Twig; | ||
|
||
use Twig\Environment; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class CollectionJsTwigExtension extends AbstractExtension | ||
{ | ||
public function getFilters() | ||
{ | ||
return [ | ||
new TwigFilter('collection_js_as_string', [$this, 'representAsString'], ['needs_environment' => true]), | ||
]; | ||
} | ||
|
||
public function representAsString(Environment $environment, $value): string | ||
{ | ||
if ($filter = $environment->getFilter('ea_as_string')) { | ||
return $filter->getCallable()($value); | ||
} | ||
|
||
if (\is_string($value)) { | ||
return $value; | ||
} | ||
|
||
if (\is_object($value) && method_exists($value, '__toString')) { | ||
return (string) $value; | ||
} | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Tienvx\UX\CollectionJs\Tests\Kernel; | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Bundle\TwigBundle\TwigBundle; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
use Tienvx\UX\CollectionJs\CollectionJsBundle; | ||
use Tienvx\UX\CollectionJs\Twig\CollectionJsTwigExtension; | ||
|
||
class TwigAppKernel extends Kernel | ||
{ | ||
use AppKernelTrait; | ||
|
||
public function registerBundles() | ||
{ | ||
return [new FrameworkBundle(), new TwigBundle(), new CollectionJsBundle()]; | ||
} | ||
|
||
public function registerContainerConfiguration(LoaderInterface $loader) | ||
{ | ||
$loader->load(function (ContainerBuilder $container) { | ||
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]); | ||
$container->loadFromExtension('twig', [ | ||
'default_path' => __DIR__ . '/templates', | ||
'strict_variables' => true, | ||
'form_themes' => [ | ||
'form_theme.html.twig', | ||
], | ||
'exception_controller' => null, | ||
'debug' => '%kernel.debug%', | ||
]); | ||
|
||
$container->setAlias('test.collection_js.twig_extension', CollectionJsTwigExtension::class)->setPublic(true); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{# dummy file #} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Tienvx\UX\CollectionJs\Tests\Twig; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tienvx\UX\CollectionJs\Tests\Kernel\TwigAppKernel; | ||
use Twig\Environment; | ||
|
||
class CollectionJsTwigExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider valueDataProvider | ||
*/ | ||
public function testRepresentAsString($value, string $expected) | ||
{ | ||
$kernel = new TwigAppKernel('test', true); | ||
$kernel->boot(); | ||
$container = $kernel->getContainer()->get('test.service_container'); | ||
|
||
$rendered = $container->get('test.collection_js.twig_extension')->representAsString( | ||
$container->get(Environment::class), | ||
$value | ||
); | ||
|
||
$this->assertSame($expected, $rendered); | ||
} | ||
|
||
public function valueDataProvider(): array | ||
{ | ||
$text = 'some text'; | ||
$object = new class($text) { | ||
private string $text; | ||
|
||
public function __construct(string $text) | ||
{ | ||
$this->text = $text; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->text; | ||
} | ||
}; | ||
|
||
return [ | ||
[$text, $text], | ||
[$object, $text], | ||
[123, ''], | ||
[[$text], ''], | ||
[true, ''], | ||
[false, ''], | ||
[new \stdClass(), ''], | ||
]; | ||
} | ||
} |