sauls /
widget
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the sauls/widget package. |
||
| 4 | * |
||
| 5 | * @author Saulius Vaičeliūnas <[email protected]> |
||
| 6 | * @link http://saulius.vaiceliunas.lt |
||
| 7 | * @copyright 2018 |
||
| 8 | * |
||
| 9 | * For the full copyright and license information, please view the LICENSE |
||
| 10 | * file that was distributed with this source code. |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Sauls\Component\Widget\Integration\Twig; |
||
| 14 | |||
| 15 | use Exception; |
||
| 16 | use Sauls\Component\Helper\Exception\PropertyNotAccessibleException; |
||
| 17 | use Sauls\Component\Widget\Factory\WidgetFactoryInterface; |
||
| 18 | use Twig\Extension\AbstractExtension; |
||
| 19 | use Twig\TwigFunction; |
||
| 20 | |||
| 21 | use function Sauls\Component\Helper\array_get_value; |
||
| 22 | |||
| 23 | class TwigExtension extends AbstractExtension |
||
| 24 | { |
||
| 25 | private $widgetFactory; |
||
| 26 | |||
| 27 | 1 | public function __construct(WidgetFactoryInterface $widgetFactory) |
|
| 28 | { |
||
| 29 | 1 | $this->widgetFactory = $widgetFactory; |
|
| 30 | 1 | } |
|
| 31 | |||
| 32 | 1 | public function getFunctions() |
|
| 33 | { |
||
| 34 | return [ |
||
| 35 | 1 | new TwigFunction( |
|
| 36 | 1 | 'widget', |
|
| 37 | 1 | [$this, 'widget'], |
|
| 38 | [ |
||
| 39 | 1 | 'is_safe' => ['html'], |
|
| 40 | ] |
||
| 41 | ) |
||
| 42 | ]; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @throws Exception |
||
| 47 | * @throws PropertyNotAccessibleException |
||
| 48 | */ |
||
| 49 | 1 | public function widget(string $name, array $options = [], array $extenstionOptions = []): string |
|
| 50 | { |
||
| 51 | 1 | $outputErrors = array_get_value($extenstionOptions, 'outputErrors', true); |
|
| 52 | |||
| 53 | try { |
||
| 54 | 1 | return $this->widgetFactory->create($name, $options); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 55 | 1 | } catch (Exception $e) { |
|
| 56 | 1 | if ($outputErrors) { |
|
| 57 | 1 | return $e->getMessage(); |
|
| 58 | } |
||
| 59 | |||
| 60 | 1 | return ''; |
|
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 |