src/Twig/ComponentDataAdapterExtension.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Twig\Components\ComponentsMiddleware;
  4. use Twig\Extension\AbstractExtension;
  5. use Twig\TwigFunction;
  6. /**
  7.  * Twig extension with adapt_attribute twig function.
  8.  *
  9.  * adapt_attribute can be used for any attribute of any markups in twig layer. This function is responsible for passing
  10.  * input and output thanks to lazy loading of proper handler.
  11.  * adapt_attribute has 2 parameters:
  12.  *  - concatenation of key of handler (name of microfrontend component) and name of method (name of attribute) to which
  13.  *    data have to be passed written in snake case and separated with "." (eg. download_files.items)
  14.  *  - data which have to be handled.
  15.  *
  16.  * There are a lot of handlers. Single handler responses to single microfrontend component. Methods which are
  17.  * responsible for data handling must be suffixed with "Attribute" for better readability.
  18.  * Handling of common data eg. images, is encapsulated in traits which can be used in handlers eg. ImageTrait is used
  19.  * in many handlers because many components needs images.
  20.  *
  21.  * @example Breadcrumbs microfrontend components has some data to handle for its attribute social-media. So there is
  22.  * need to call adapt_attribute('breadcrumbs.social_media', data). Data will be passed to Breadcrumbs handler to
  23.  * socialMediaAttribute method.
  24.  *
  25.  * @author Michał Wiązek <michal.wiazek@makolab.com>
  26.  */
  27. class ComponentDataAdapterExtension extends AbstractExtension
  28. {
  29.     private ComponentsMiddleware $componentMiddleware;
  30.     /**
  31.      * Constructor.
  32.      */
  33.     public function __construct(ComponentsMiddleware $componentMiddleware)
  34.     {
  35.         $this->componentMiddleware $componentMiddleware;
  36.     }
  37.     /**
  38.      * {@inheritDoc}
  39.      */
  40.     public function getFunctions(): array
  41.     {
  42.         return [
  43.             new TwigFunction('adapt_attribute', [$this'adaptAttribute']),
  44.         ];
  45.     }
  46.     /**
  47.      * Return structure and data fitting to frontend components.
  48.      *
  49.      * @param array $data
  50.      *
  51.      * @return mixed
  52.      */
  53.     public function adaptAttribute(string $componentAttribute, array $data null)
  54.     {
  55.         list($componentKey$attribute) = explode('.'$componentAttribute);
  56.         $method $this->snakeToCamel($attribute).'Attribute';
  57.         $preparedData $this->componentMiddleware->getServiceLocator()->get($componentKey)->$method($data);
  58.         return $preparedData;
  59.     }
  60.     /**
  61.      * Convert snakeCase to camelCase.
  62.      *
  63.      * @param $snakeCase
  64.      */
  65.     private function snakeToCamel($snakeCase): string
  66.     {
  67.         return lcfirst(str_replace(' '''ucwords(str_replace('_'' '$snakeCase))));
  68.     }
  69. }