-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Twig functions to get variants args and blocks
- Loading branch information
1 parent
0e47d00
commit cabcda4
Showing
6 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the UI Storia project. | ||
* | ||
* (c) Loïc Sapone <loic@sapone.fr> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IQ2i\StoriaBundle\Twig; | ||
|
||
use IQ2i\StoriaBundle\Config\ViewConfiguration; | ||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\Yaml\Yaml; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class ViewExtension extends AbstractExtension | ||
{ | ||
public function __construct( | ||
private readonly string $defaultPath, | ||
) { | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('iq2i_storia_variant_args', [$this, 'getVariantArgs']), | ||
new TwigFunction('iq2i_storia_variant_blocks', [$this, 'getVariantBlocks']), | ||
]; | ||
} | ||
|
||
public function getVariantArgs(string $view, string $variant): array | ||
{ | ||
$yaml = Yaml::parse(file_get_contents($this->defaultPath.'/components/'.$view.'.yaml')); | ||
$config = (new Processor())->processConfiguration(new ViewConfiguration(), [$yaml]); | ||
|
||
if (!\array_key_exists($variant, $config['variants'])) { | ||
throw new \InvalidArgumentException('Unknown variant: '.$variant); | ||
} | ||
|
||
return $config['variants'][$variant]['args']; | ||
} | ||
|
||
public function getVariantBlocks(string $view, string $variant): array | ||
{ | ||
$yaml = Yaml::parse(file_get_contents($this->defaultPath.'/components/'.$view.'.yaml')); | ||
$config = (new Processor())->processConfiguration(new ViewConfiguration(), [$yaml]); | ||
|
||
if (!\array_key_exists($variant, $config['variants'])) { | ||
throw new \InvalidArgumentException('Unknown variant: '.$variant); | ||
} | ||
|
||
return $config['variants'][$variant]['blocks']; | ||
} | ||
} |
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 | ||
|
||
/* | ||
* This file is part of the UI Storia project. | ||
* | ||
* (c) Loïc Sapone <loic@sapone.fr> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IQ2i\StoriaBundle\Tests\Twig; | ||
|
||
use IQ2i\StoriaBundle\Twig\ViewExtension; | ||
use Twig\Test\IntegrationTestCase; | ||
|
||
class ViewExtensionTest extends IntegrationTestCase | ||
{ | ||
protected function getFixturesDir(): string | ||
{ | ||
return __DIR__.'/fixtures'; | ||
} | ||
|
||
protected function getExtensions(): iterable | ||
{ | ||
yield new ViewExtension(\dirname(__DIR__).'/TestApplication/storia'); | ||
} | ||
} |
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,8 @@ | ||
--TEST-- | ||
"iq2i_storia_variant_args" function | ||
--TEMPLATE-- | ||
{{ iq2i_storia_variant_args('button/button', 'plain_icon_right').class }} | ||
--DATA-- | ||
return [] | ||
--EXPECT-- | ||
text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center me-2 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800 |
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,8 @@ | ||
--TEST-- | ||
"iq2i_storia_variant_blocks" function | ||
--TEMPLATE-- | ||
{{ iq2i_storia_variant_blocks('button/button', 'plain_icon_right').content }} | ||
--DATA-- | ||
return [] | ||
--EXPECT-- | ||
Choose plan |